更新 RSA 帮助类解决加密文本长度
简介
由于旧版本 RSA 帮助类 对加密的文字长度有限制,所以更新代码以加密更长的字符。
代码
RSA 库
依旧是基于 System.Security.Cryptography.RSA 库
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 
 | 
 
 public class RSAHelper
 {
 
 
 
 
 
 public static void GenerateRSAKeys(out string xmlPublicKey, out string xmlPrivateKey)
 {
 try
 {
 using RSA rsa = RSA.Create();
 xmlPublicKey = rsa.ToXmlString(false);
 xmlPrivateKey = rsa.ToXmlString(true);
 }
 catch (Exception ex)
 {
 xmlPublicKey = string.Empty;
 xmlPrivateKey = string.Empty;
 }
 }
 
 
 
 
 
 
 
 public static string RSAEncrypt(string xmlPublicKey, string strPlaintext)
 {
 try
 {
 byte[] originalData = Encoding.UTF8.GetBytes(strPlaintext);
 using RSA rsa = RSA.Create();
 rsa.FromXmlString(xmlPublicKey);
 byte[] encryptedData = rsa.Encrypt(originalData, RSAEncryptionPadding.OaepSHA1);
 return Convert.ToBase64String(encryptedData);
 }
 catch (Exception ex)
 {
 return string.Empty;
 }
 }
 
 
 
 
 
 
 
 public static string RSADecrypt(string xmlPrivateKey, string strCiphertext)
 {
 try
 {
 byte[] encryptedData = Convert.FromBase64String(strCiphertext);
 using RSA rsa = RSA.Create();
 rsa.FromXmlString(xmlPrivateKey);
 byte[] decryptedData = rsa.Decrypt(encryptedData, RSAEncryptionPadding.OaepSHA1);
 return Encoding.UTF8.GetString(decryptedData);
 }
 catch (Exception ex)
 {
 return string.Empty;
 }
 }
 }
 
 | 
Portable.BouncyCastle 库
由于 System.Security.Cryptography.RSA 库只能使用自身生成的 xml 公钥与私钥,而想使用 OpenSSL 生成的 pem 公钥与私钥,则无法读取,使用 Portable.BouncyCastle 库可以读取编解码,但是实测似乎与 C++ OpenSSL 无法互相加密解密。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 
 | 
 
 public class BouncyCastleHelper
 {
 
 
 
 
 
 public static AsymmetricKeyParameter ReadPrivateKeyFile(string privateKeyFilePath)
 {
 using var reader = File.OpenText(privateKeyFilePath);
 var pemReader = new PemReader(reader);
 var keyObject = pemReader.ReadObject();
 
 if (keyObject is AsymmetricCipherKeyPair keyPair)
 {
 return keyPair.Private;
 }
 else if (keyObject is AsymmetricKeyParameter keyParam)
 {
 return keyParam;
 }
 else
 {
 throw new InvalidOperationException("Invalid private key format.");
 }
 }
 
 
 
 
 
 
 public static AsymmetricKeyParameter ReadPublicKeyFile(string publicKeyFilePath)
 {
 using var reader = File.OpenText(publicKeyFilePath);
 var pemReader = new PemReader(reader);
 var keyObject = pemReader.ReadObject();
 
 if (keyObject is AsymmetricKeyParameter keyParam)
 {
 return keyParam;
 }
 else
 {
 throw new InvalidOperationException("Invalid public key format.");
 }
 }
 
 
 
 
 
 
 public static AsymmetricKeyParameter ReadPrivateKey(string privateKey)
 {
 StringReader stringReader = new StringReader(privateKey);
 var pemReader = new PemReader(stringReader);
 var keyObject = pemReader.ReadObject();
 
 if (keyObject is AsymmetricCipherKeyPair keyPair)
 {
 return keyPair.Private;
 }
 else if (keyObject is AsymmetricKeyParameter keyParam)
 {
 return keyParam;
 }
 else
 {
 throw new InvalidOperationException("Invalid private key format.");
 }
 }
 
 
 
 
 
 
 public static AsymmetricKeyParameter ReadPublicKey(string publicKey)
 {
 StringReader stringReader = new StringReader(publicKey);
 var pemReader = new PemReader(stringReader);
 var keyObject = pemReader.ReadObject();
 
 if (keyObject is AsymmetricKeyParameter keyParam)
 {
 return keyParam;
 }
 else
 {
 throw new InvalidOperationException("Invalid public key format.");
 }
 }
 
 
 
 
 
 
 public static void GenerateRSAKeys(AsymmetricKeyParameter rsaKey, out string xmlPublicKey, out string xmlPrivateKey)
 {
 try
 {
 if (rsaKey.IsPrivate)
 {
 using RSA rsa = RSA.Create(DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)rsaKey));
 xmlPublicKey = rsa.ToXmlString(false);
 xmlPrivateKey = rsa.ToXmlString(true);
 }
 else
 {
 using RSA rsa = RSA.Create(DotNetUtilities.ToRSAParameters((RsaKeyParameters)rsaKey));
 xmlPublicKey = rsa.ToXmlString(false);
 xmlPrivateKey = string.Empty;
 }
 }
 catch (Exception ex)
 {
 xmlPublicKey = string.Empty;
 xmlPrivateKey = string.Empty;
 }
 }
 
 
 
 
 
 
 
 public static string RSAEncrypt(AsymmetricKeyParameter publicKey, string strPlaintext)
 {
 try
 {
 byte[] inputData = Encoding.UTF8.GetBytes(strPlaintext);
 IAsymmetricBlockCipher cipher = new RsaEngine();
 cipher.Init(true, publicKey);
 byte[] encryptedData = cipher.ProcessBlock(inputData, 0, inputData.Length);
 return Convert.ToBase64String(encryptedData);
 }
 catch (Exception ex)
 {
 return string.Empty;
 }
 }
 
 
 
 
 
 
 
 public static string RSADecrypt(AsymmetricKeyParameter privateKey, string strCiphertext)
 {
 try
 {
 byte[] encryptedData = Convert.FromBase64String(strCiphertext);
 IAsymmetricBlockCipher cipher = new RsaEngine();
 cipher.Init(false, privateKey);
 byte[] decryptedData = cipher.ProcessBlock(encryptedData, 0, encryptedData.Length);
 return Encoding.UTF8.GetString(decryptedData);
 }
 catch (Exception ex)
 {
 return string.Empty;
 }
 }
 }
 
 |