当前位置:首页 > 技能相关 > C#与C++ > 正文内容

C#创建标准MD5WithRSA算法类库,便于PB调用MD5WithRSA签名

admin1年前 (2023-12-19)C#与C++2500 修订时间:2023-12-19 20:03:10

使用C#开发MD5WithRSA标准算法,用以其他语言调阅查看。创建类库项目后接下来需要安装Portable.BouncyCastle类库。

一、VS安装Portable.BouncyCastle的NuGet包

Portable.BouncyCastle包以来于.NET Framework 4.0 或 .NET Standard 2.0

Portable.BouncyCastle

二、设置类库项目-使程序集COM课件

C#开发的类库需要注册为com类型,才可被其他程序所调阅使用,需要注册为COM

使程序集COM课件

为COM互操作

三、C#实现代码

using System;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;

namespace AESorDES
{
    public class md5withRsa
    {
        public Encoding encoding = Encoding.GetEncoding("GBK");
        public string SignerSymbol = "MD5withRSA";
        public md5withRsa() { }
        public md5withRsa(Encoding e, string s)
        {
            encoding = e;
            SignerSymbol = s;
        }
        private AsymmetricKeyParameter CreateKEY(bool isPrivate, string key)
        {
            byte[] keyInfoByte = Convert.FromBase64String(key);
            if (isPrivate)
                return PrivateKeyFactory.CreateKey(keyInfoByte);
            else
                return PublicKeyFactory.CreateKey(keyInfoByte);
        }
        /// <summary>
        /// 数据签名
        /// </summary>
        /// <param name="content">待加密字符串</param>
        /// <param name="privatekey">私钥</param>
        /// <returns>加密后字符串</returns>
        public string SignMD5withRsa(string content, string privatekey)
        {
            ISigner sig = SignerUtilities.GetSigner(SignerSymbol);
            sig.Init(true, CreateKEY(true, privatekey));
            var bytes = encoding.GetBytes(content);
            sig.BlockUpdate(bytes, 0, bytes.Length);
            byte[] signature = sig.GenerateSignature();
            //使用Base64转换sig,8-bit
            var signedString = Convert.ToBase64String(signature);
            return signedString;
        }
        /// <summary>
        /// 验证签名
        /// </summary>
        /// <param name="content">待签名的字符串</param>
        /// <param name="signData">加密后的文本</param>
        /// <param name="publickey">公钥文本</param>
        /// <returns>是否一致</returns>
        public bool VerifyMD5withRsa(string content, string signData, string publickey)
        {
            ISigner signer = SignerUtilities.GetSigner(SignerSymbol);
            signer.Init(false, CreateKEY(false, publickey));
            var expectedSig = Convert.FromBase64String(signData);
            var msgBytes = encoding.GetBytes(content);
            signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
            return signer.VerifySignature(expectedSig);
        }
    }

}

生成的类库文件需要注册,Portable.BouncyCastle类库也需要同步拷贝到程序根目录

拷贝类库文件

四、类库的注册

使用管理员打开CMD命令行窗口,执行下述语句

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\regasm E:\PB9soft\AESorDES\AESorDES.dll /tlb:E:\PB9soft\AESorDES\AESorDES.tlb /codebase
E:\PB9soft\AESorDES 是程序的目录,也可以是电脑上任意目录,确保文件存在即可。

五、PB调用的方式

//创建OLEObject对象
OLEObject OLEObject 
OLEObject = Create OLEObject
//判定类库方法是否有效引入
long li_status
string ls_str
//创建连接,此处格式为“命名空间名.方法名”,li_status一般为引入错误或未注册类库
li_status= OLEObject.ConnectToNewObject("AESorDES.md5withRsa")
//根据状态
if li_status = 0 then
 //调用方法内的公共函数
 //数据签名,返回值为字符串
 ls_str = OLEObject.AEScriptMD5withRsa(sle_1.text,mle_3.text)
  //数据验签,返回值bool类型
 if OLEObject.VerifyMD5withRsa(sle_1.text,mle_1.text,mle_3.text) then
	messagebox('提示','签名验证成功!')
 end if
end if
//释放资源
OLEObject.DisConnectObject()

附件:

AESorDES.zip

 您阅读本篇文章共花了: 

免责声明
本站内容均为博客主本人日常使用记录的存档,如侵犯你的权益请联系:lifei@zaiheze.com 546262132@qq.com 沟通删除事宜。本站仅带访问端口形式使用,已杜绝搜索引擎爬取。

扫描二维码推送至手机访问。

版权声明:本文由LIFEI - blog发布,如需转载请注明出处。

本文链接:http://www.lifeiai.com/?id=337

分享给朋友:

相关文章

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。