关于C#的RSA加密(用于泛微OA)

由于项目需要,需要用公钥对秘钥进行加密,且对接系统用C#开发,需求是这样的:

注册许可证时返回的公钥spk对秘钥信息secrit进行加密。 

由于公钥spk是字符串,但C#中只认可的xml字符串的公钥才能够进行加密,可以使用以下方法,先进行转换,将公钥字符串转换成XML,再进行加密,就可以得到加密后的字符:

        public static string RSAEncrypt(string spk, string secrit)
        {
            string PublicKey = RSAPublicKey(spk);
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] cipherbytes;
            rsa.FromXmlString(PublicKey);
            cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(secrit), false);

            return Convert.ToBase64String(cipherbytes);
        }

        public static string RSAPublicKey(string publicKey)
        {
            RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
            string XML = string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
            Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
            Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
            return XML;
        }

想要从字符串的pem公钥转为xml公钥,只能依赖于一个第三方库,叫做BouncyCastle,在vs中使用NuGet包管理器安装一下,就可以使用。

RSA(Rivest-Shamir-Adleman)是一种非对称加密算法,用于加密和解密数据。C#中提供了对RSA算法的支持。下面是一个简单的示例代码,展示如何使用RSA算法进行加密和解密: ```csharp using System; using System.Security.Cryptography; using System.Text; public class RSADemo { public static void Main() { // 创建RSA实例 using (RSA rsa = RSA.Create()) { try { // 生成RSA的公钥和私钥对 RSAParameters publicKey; RSAParameters privateKey; rsa.KeySize = 2048; // 设置密钥长度 // 生成密钥对 publicKey = rsa.ExportParameters(false); privateKey = rsa.ExportParameters(true); // 要加密的数据 string dataToEncrypt = "Hello, RSA!"; // 加密数据 byte[] encryptedData = EncryptData(Encoding.UTF8.GetBytes(dataToEncrypt), publicKey); // 解密数据 byte[] decryptedData = DecryptData(encryptedData, privateKey); // 显示结果 Console.WriteLine("原始数据: {0}", dataToEncrypt); Console.WriteLine("加密后的数据: {0}", Convert.ToBase64String(encryptedData)); Console.WriteLine("解密后的数据: {0}", Encoding.UTF8.GetString(decryptedData)); } catch (CryptographicException e) { Console.WriteLine(e.Message); } } } // 使用RSA算法加密数据 public static byte[] EncryptData(byte[] dataToEncrypt, RSAParameters publicKey) { using (RSA rsa = RSA.Create()) { try { rsa.ImportParameters(publicKey); return rsa.Encrypt(dataToEncrypt, RSAEncryptionPadding.OaepSHA256); } catch (CryptographicException e) { Console.WriteLine(e.Message); return null; } } } // 使用RSA算法解密数据 public static byte[] DecryptData(byte[] dataToDecrypt, RSAParameters privateKey) { using (RSA rsa = RSA.Create()) { try { rsa.ImportParameters(privateKey); return rsa.Decrypt(dataToDecrypt, RSAEncryptionPadding.OaepSHA256); } catch (CryptographicException e) { Console.WriteLine(e.Message); return null; } } } } ``` 这个示例代码中,我们使用`RSA.Create()`创建了一个RSA实例,并设置了密钥长度为2048位。然后,调用`ExportParameters()`方法生成公钥和私钥对。接下来,我们使用公钥加密数据,然后再用私钥解密数据。最后,我们将结果打印到控制台上。 需要注意的是,RSA算法对于较大的数据加密耗时较长,因此通常会使用RSA算法加密对称加密算法(如AES)的密钥,从而提高加密效率。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cnwfnyd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值