C#.NET Framework RSA 公鑰加密 私鑰解密 ver:20230609
環境說明:
.NET Framework 4.6 的控制檯程式 。
.NET Framework 對於RSA的支援:
1. .NET Framework 內建只支援XML格式的私鑰/公鑰。如果要用PKCS1,PKCS8格式的,要用到三方庫BouncyCastle。
2. .NET 中預設加密演演算法為「RSA/ECB/PKCS1Padding」 ,要和JAVA互通,JAVA 那邊也得是 RSA/ECB/PKCS1Padding 。
加密解析:
//假設私鑰長度為1024, 1024/8-11=117。
//如果明文的長度小於117,直接全加密,然後轉base64。(data.Length <= maxBlockSize)
//如果明文長度大於117,則每117分一段加密,寫入到另一個Stream中,最後轉base64。while (blockSize > 0)
如果要和其它語言互通,這個分段長度需要和其它語言約定好,不一是 私鑰長度/8-11。
解密解析:
//假設私鑰長度為1024, 1024/8 =128。
//如果明文的長度小於 128,直接全解密。(data.Length <= maxBlockSize)
//如果明文長度大於 128,則每 128 分一段解密,寫入到另一個Stream中,最後 GetString。while (blockSize > 0)
用「支付寶開放平臺開發助手」生成一組公私鑰:
PKCS8私鑰:
MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAMz0Czg6QUtTISa2pUkloeQB/TEpHdqrfyroWpKLW9B/LWFSOGH9nyTk1pPZaeadyEZQ6gay/C0pUAetLraq9bMA/Luxq68b87uG7WX7dKytEO2/87qGpGMRs97H+GlkzWil2QO2KK4cHnAcVicPsmi5aZ72U0BWJFyPhtd+qdmrAgMBAAECgYEAvW67iAbgHt0BASVD9C3iSjpEaVHVlC165o/IVzaTcEx8Bz3Ve0zN8W3JnvIO3ebsG4HiLLr2Nk++9rltOc0eNeGMv7F1e/OFot1wN0ON6s1g4bYh1z5Uz8FcYiMWcqHHICrx+oSFeK9x+I2Zge7enQXcsVnqEhm77ZE5YczSryECQQD9nB58e5efYchF+cYbmURioX18cUMuhQbB9Aq2N55cd689Lg35KZqT8JQTp/8tQSdCJG8d2nU8VKspUKTEAuaDAkEAzuKIIoc9PVJvy90LhIPA9c1S8BP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VnrhNYGE6wTbAQJBAJ9qRLQJY19X6EH3+gi6M2IPyeifUZlRXCeqs9C199OdqjRQUGMPAjBwe1ZFTc7AyAwvC1+bj9qc063Ty3sPqTw=
與之匹配的公鑰:
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDM9As4OkFLUyEmtqVJJaHkAf0xKR3aq38q6FqSi1vQfy1hUjhh/Z8k5NaT2WnmnchGUOoGsvwtKVAHrS62qvWzAPy7sauvG/O7hu1l+3SsrRDtv/O6hqRjEbPex/hpZM1opdkDtiiuHB5wHFYnD7JouWme9lNAViRcj4bXfqnZqwIDAQAB
核心重點是拿到 .NET 的RSACryptoServiceProvider 物件。
nuget 中參照 Portable.BouncyCastle。
工具類:
RsaUtil 的作用: 將私鑰(PKCS1,PKCS8)、公鑰、私鑰證書、公鑰證書轉為.NET RSACryptoServiceProvider 物件。
using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using System; using System.IO; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; namespace CommonUtils { /// <summary> /// 將私鑰(PKCS1,PKCS8)、公鑰、私鑰證書、公鑰證書轉為.NET RSACryptoServiceProvider 物件 /// </summary> public static class RsaUtil { #region 載入私鑰 /// <summary> /// 轉換私鑰字串為RSACryptoServiceProvider /// </summary> /// <param name="privateKeyStr">私鑰字串</param> /// <param name="keyFormat">PKCS8,PKCS1</param> /// <param name="signType">RSA 私鑰長度1024 ,RSA2 私鑰長度2048</param> /// <returns></returns> public static RSACryptoServiceProvider LoadPrivateKey(string privateKeyStr, string keyFormat) { string signType = "RSA"; if (privateKeyStr.Length > 1024) { signType = "RSA2"; } //PKCS8,PKCS1 if (keyFormat == "PKCS1") { return LoadPrivateKeyPKCS1(privateKeyStr, signType); } else { return LoadPrivateKeyPKCS8(privateKeyStr); } } /// <summary> /// PKCS1 格式私鑰轉 RSACryptoServiceProvider 物件 /// </summary> /// <param name="strKey">pcsk1 私鑰的文字內容</param> /// <param name="signType">RSA 私鑰長度1024 ,RSA2 私鑰長度2048 </param> /// <returns></returns> public static RSACryptoServiceProvider LoadPrivateKeyPKCS1(string privateKeyPemPkcs1, string signType) { try { privateKeyPemPkcs1 = privateKeyPemPkcs1.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "").Replace("\r", "").Replace("\n", "").Trim(); privateKeyPemPkcs1 = privateKeyPemPkcs1.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "").Replace("\r", "").Replace("\n", "").Trim(); byte[] data = null; //讀取帶 data = Convert.FromBase64String(privateKeyPemPkcs1); RSACryptoServiceProvider rsa = DecodeRSAPrivateKey(data, signType); return rsa; } catch (Exception ex) { throw ex; } return null; } private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey, string signType) { byte[] MODULUS, E, D, P, Q, DP, DQ, IQ; // --------- Set up stream to decode the asn.1 encoded RSA private key ------ MemoryStream mem = new MemoryStream(privkey); BinaryReader binr = new BinaryReader(mem); //wrap Memory Stream with BinaryReader for easy reading byte bt = 0; ushort twobytes = 0; int elems = 0; try { twobytes = binr.ReadUInt16(); if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81) binr.ReadByte(); //advance 1 byte else if (twobytes == 0x8230) binr.ReadInt16(); //advance 2 bytes else return null; twobytes = binr.ReadUInt16(); if (twobytes != 0x0102) //version number return null; bt = binr.ReadByte(); if (bt != 0x00) return null; //------ all private key components are Integer sequences ---- elems = GetIntegerSize(binr); MODULUS = binr.ReadBytes(elems); elems = GetIntegerSize(binr); E = binr.ReadBytes(elems); elems = GetIntegerSize(binr); D = binr.ReadBytes(elems); elems = GetIntegerSize(binr); P = binr.ReadBytes(elems); elems = GetIntegerSize(binr); Q = binr.ReadBytes(elems); elems = GetIntegerSize(binr); DP = binr.ReadBytes(elems); elems = GetIntegerSize(binr); DQ = binr.ReadBytes(elems); elems = GetIntegerSize(binr); IQ = binr.ReadBytes(elems); // ------- create RSACryptoServiceProvider instance and initialize with public key ----- CspParameters CspParameters = new CspParameters(); CspParameters.Flags = CspProviderFlags.UseMachineKeyStore; int bitLen = 1024; if ("RSA2".Equals(signType)) { bitLen = 2048; } RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(bitLen, CspParameters); //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSAParameters RSAparams = new RSAParameters(); RSAparams.Modulus = MODULUS; RSAparams.Exponent = E; RSAparams.D = D; RSAparams.P = P; RSAparams.Q = Q; RSAparams.DP = DP; RSAparams.DQ = DQ; RSAparams.InverseQ = IQ; RSA.ImportParameters(RSAparams); return RSA; } catch (Exception ex) { throw ex; // return null; } finally { binr.Close(); } } private static int GetIntegerSize(BinaryReader binr) { byte bt = 0; byte lowbyte = 0x00; byte highbyte = 0x00; int count = 0; bt = binr.ReadByte(); if (bt != 0x02) //expect integer return 0; bt = binr.ReadByte(); if (bt == 0x81) count = binr.ReadByte(); // data size in next byte else if (bt == 0x82) { highbyte = binr.ReadByte(); // data size in next 2 bytes lowbyte = binr.ReadByte(); byte[] modint = { lowbyte, highbyte, 0x00, 0x00 }; count = BitConverter.ToInt32(modint, 0); } else { count = bt; // we already have the data size } while (binr.ReadByte() == 0x00) { //remove high order zeros in data count -= 1; } binr.BaseStream.Seek(-1, SeekOrigin.Current); //last ReadByte wasn't a removed zero, so back up a byte return count; } /// <summary> /// PKCS8 文字轉RSACryptoServiceProvider 物件 /// </summary> /// <param name="privateKeyPemPkcs8"></param> /// <returns></returns> public static RSACryptoServiceProvider LoadPrivateKeyPKCS8(string privateKeyPemPkcs8) { try { //PKCS8是「BEGIN PRIVATE KEY」 privateKeyPemPkcs8 = privateKeyPemPkcs8.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "").Replace("\r", "").Replace("\n", "").Trim(); privateKeyPemPkcs8 = privateKeyPemPkcs8.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "").Replace("\r", "").Replace("\n", "").Trim(); //pkcs8 文字先轉為 .NET XML 私鑰字串 string privateKeyXml = RSAPrivateKeyJava2DotNet(privateKeyPemPkcs8); RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider(); publicRsa.FromXmlString(privateKeyXml); return publicRsa; } catch (Exception ex) { throw ex; } } /// <summary> /// PKCS8 私鑰文字 轉 .NET XML 私鑰文字 /// </summary> /// <param name="privateKeyPemPkcs8"></param> /// <returns></returns> public static string RSAPrivateKeyJava2DotNet(string privateKeyPemPkcs8) { RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKeyPemPkcs8)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>", Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned())); } #endregion /// <summary> /// 載入公鑰證書 /// </summary> /// <param name="publicKeyCert">公鑰證書文字內容</param> /// <returns></returns> public static RSACryptoServiceProvider LoadPublicCert(string publicKeyCert) { publicKeyCert = publicKeyCert.Replace("-----BEGIN CERTIFICATE-----", "").Replace("-----END CERTIFICATE-----", "").Replace("\r", "").Replace("\n", "").Trim(); byte[] bytesCerContent = Convert.FromBase64String(publicKeyCert); X509Certificate2 x509 = new X509Certificate2(bytesCerContent); RSACryptoServiceProvider rsaPub = (RSACryptoServiceProvider)x509.PublicKey.Key; return rsaPub; } /// <summary> /// pem 公鑰文字 轉 .NET RSACryptoServiceProvider。 /// </summary> /// <param name="publicKeyPem"></param> /// <returns></returns> public static RSACryptoServiceProvider LoadPublicKey(string publicKeyPem) { publicKeyPem = publicKeyPem.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "").Replace("\r", "").Replace("\n", "").Trim(); //pem 公鑰文字 轉 .NET XML 公鑰文字。 string publicKeyXml = RSAPublicKeyJava2DotNet(publicKeyPem); RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider(); publicRsa.FromXmlString(publicKeyXml); return publicRsa; } /// <summary> /// pem 公鑰文字 轉 .NET XML 公鑰文字。 /// </summary> /// <param name="publicKey"></param> /// <returns></returns> private static string RSAPublicKeyJava2DotNet(string publicKey) { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); } } }
RsaEncryptUtil 工具類:基於 RSACryptoServiceProvider 加密解密的輕度封裝,標準的支援分段加密。
using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace CommonUtils { /// <summary> /// 基於 RSACryptoServiceProvider 加密解密的輕度封裝,標準的支援分段加密。 /// </summary> public static class RsaEncryptUtil { #region 非標準的-私鑰加密-公鑰解密 /// <summary> /// 私鑰加密 .Net平臺預設是使用公鑰進行加密,私鑰進行解密。私鑰加密需要自己實現或者使用第三方dll /// </summary> /// <param name="data"></param> /// <param name="key">私鑰,格式:PKCS8</param> /// <returns></returns> public static byte[] encryptByPrivateKey(String data, String key) { String priKey = key.Trim(); String xmlPrivateKey = RSAPrivateKeyJava2DotNet(priKey); //載入私鑰 RSACryptoServiceProvider privateRsa = new RSACryptoServiceProvider(); privateRsa.FromXmlString(xmlPrivateKey); //轉換金鑰 AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetKeyPair(privateRsa); IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 引數與Java中加密解密的引數一致 c.Init(true, keyPair.Private); //第一個引數為true表示加密,為false表示解密;第二個參數列示金鑰 byte[] DataToEncrypt = Encoding.UTF8.GetBytes(data); byte[] outBytes = c.DoFinal(DataToEncrypt);//加密 return outBytes; } /// <summary> /// 私鑰加密 /// </summary> /// <param name="data">明文</param> /// <param name="key">私鑰</param> /// <param name="keyFormat">私鑰格式:PKCS1,PKCS8</param> /// <returns></returns> public static byte[] encryptByPrivateKey(String data, String key, string keyFormat) { String priKey = key.Trim(); //載入私鑰 RSACryptoServiceProvider privateRsa = RsaUtil.LoadPrivateKey(key, keyFormat); //轉換金鑰 AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetKeyPair(privateRsa); IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 引數與Java中加密解密的引數一致 c.Init(true, keyPair.Private); //第一個引數為true表示加密,為false表示解密;第二個參數列示金鑰 byte[] DataToEncrypt = Encoding.UTF8.GetBytes(data); byte[] outBytes = c.DoFinal(DataToEncrypt);//加密 return outBytes; } /// <summary> /// RSA私鑰格式轉換,java->.net /// </summary> /// <param name="privateKey"></param> /// <returns></returns> private static string RSAPrivateKeyJava2DotNet(string privateKey) { RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>", Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned())); } /// <summary> /// 用公鑰解密 /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <returns></returns> public static byte[] decryptByPublicKey(String data, String key) { String pubKey = key.Trim(); String xmlPublicKey = RSAPublicKeyJava2DotNet(pubKey); RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider(); publicRsa.FromXmlString(xmlPublicKey); AsymmetricKeyParameter keyPair = DotNetUtilities.GetRsaPublicKey(publicRsa); //轉換金鑰 // AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetRsaKeyPair(publicRsa); IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");// 引數與Java中加密解密的引數一致 c.Init(false, keyPair); //第一個引數為true表示加密,為false表示解密;第二個參數列示金鑰 byte[] DataToEncrypt = Convert.FromBase64String(data); byte[] outBytes = c.DoFinal(DataToEncrypt);//解密 return outBytes; } /// <summary> /// RSA公鑰格式轉換,java->.net /// </summary> /// <param name="publicKey"></param> /// <returns></returns> private static string RSAPublicKeyJava2DotNet(string publicKey) { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); } #endregion #region 標準的-公鑰加密-私鑰解密 /** 預設編碼字元集 */ private static string DEFAULT_CHARSET = "UTF-8"; /// <summary> /// 公鑰加密(超過 私鑰長度 / 8 - 11,分段加密) /// </summary> /// <param name="content">明文</param> /// <param name="charset">編碼</param> /// <param name="publicKeyPem">公鑰</param> /// <returns></returns> /// <exception cref="Exception"></exception> public static string RSAEncrypt(string content, string charset, string publicKeyPem) { try { //假設私鑰長度為1024, 1024/8-11=117。 //如果明文的長度小於117,直接全加密,然後轉base64。(data.Length <= maxBlockSize) //如果明文長度大於117,則每117分一段加密,寫入到另一個Stream中,最後轉base64。while (blockSize > 0) RSACryptoServiceProvider rsa = RsaUtil.LoadPublicKey(publicKeyPem); //string sPublicKeyPEM = File.ReadAllText(publicKeyPem); //RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); //rsa.PersistKeyInCsp = false; //RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM); if (string.IsNullOrEmpty(charset)) { charset = DEFAULT_CHARSET; } byte[] data = Encoding.GetEncoding(charset).GetBytes(content); int maxBlockSize = rsa.KeySize / 8 - 11; //加密塊最大長度限制 if (data.Length <= maxBlockSize) { byte[] cipherbytes = rsa.Encrypt(data, false); return Convert.ToBase64String(cipherbytes); } MemoryStream plaiStream = new MemoryStream(data); MemoryStream crypStream = new MemoryStream(); Byte[] buffer = new Byte[maxBlockSize]; int blockSize = plaiStream.Read(buffer, 0, maxBlockSize); while (blockSize > 0) { Byte[] toEncrypt = new Byte[blockSize]; Array.Copy(buffer, 0, toEncrypt, 0, blockSize); Byte[] cryptograph = rsa.Encrypt(toEncrypt, false); crypStream.Write(cryptograph, 0, cryptograph.Length); blockSize = plaiStream.Read(buffer, 0, maxBlockSize); } return Convert.ToBase64String(crypStream.ToArray(), Base64FormattingOptions.None); } catch (Exception ex) { throw new Exception("EncryptContent = " + content + ",charset = " + charset, ex); } } /// <summary> /// 私鑰解密(超過 私鑰長度 / 8 - 11,分段加密) /// </summary> /// <param name="content">密文</param> /// <param name="charset">編碼</param> /// <param name="privateKeyPem">私鑰</param> /// <param name="keyFormat">私鑰格式 PKCS1,PKCS8</param> /// <returns></returns> /// <exception cref="Exception"></exception> public static string RSADecrypt(string content, string charset, string privateKeyPem, string keyFormat) { try { //假設私鑰長度為1024, 1024/8 =128。 //如果明文的長度小於 128,直接全解密。(data.Length <= maxBlockSize) //如果明文長度大於 128,則每 128 分一段解密,寫入到另一個Stream中,最後 GetString。while (blockSize > 0) RSACryptoServiceProvider rsaCsp = RsaUtil.LoadPrivateKey(privateKeyPem, keyFormat); //RSACryptoServiceProvider rsaCsp = LoadCertificateFile(privateKeyPem, signType); if (string.IsNullOrEmpty(charset)) { charset = DEFAULT_CHARSET; } byte[] data = Convert.FromBase64String(content); int maxBlockSize = rsaCsp.KeySize / 8; //解密塊最大長度限制 if (data.Length <= maxBlockSize) { byte[] cipherbytes = rsaCsp.Decrypt(data, false); return Encoding.GetEncoding(charset).GetString(cipherbytes); } MemoryStream crypStream = new MemoryStream(data); MemoryStream plaiStream = new MemoryStream(); Byte[] buffer = new Byte[maxBlockSize]; int blockSize = crypStream.Read(buffer, 0, maxBlockSize); while (blockSize > 0) { Byte[] toDecrypt = new Byte[blockSize]; Array.Copy(buffer, 0, toDecrypt, 0, blockSize); Byte[] cryptograph = rsaCsp.Decrypt(toDecrypt, false); plaiStream.Write(cryptograph, 0, cryptograph.Length); blockSize = crypStream.Read(buffer, 0, maxBlockSize); } return Encoding.GetEncoding(charset).GetString(plaiStream.ToArray()); } catch (Exception ex) { throw new Exception("DecryptContent = " + content + ",charset = " + charset, ex); } } #endregion } }
呼叫範例:
using CommonUtils; using System; namespace ConsoleNetFxRsaEncrypt { class Program { static void Main(string[] args) { try { //PKCS8格式私鑰 string strPriPkcs8 = "MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAMz0Czg6QUtTISa2pUkloeQB/TEpHdqrfyroWpKLW9B/LWFSOGH9nyTk1pPZaeadyEZQ6gay/C0pUAetLraq9bMA/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iNmYHu3p0F3LFZ6hIZu+2ROWHM0q8hAkEA/ZwefHuXn2HIRfnGG5lEYqF9fHFDLoUGwfQKtjeeXHevPS4N+Smak/CUE6f/LUEnQiRvHdp1PFSrKVCkxALmgwJBAM7iiCKHPT1Sb8vdC4SDwPXNUvATwiOxDAmk2aieaN1Wkdnar1GQxl+5C95GJEPp+5qtyiBAr5BBcwPhTj/el7kCQQCUEyh1tE/2HkiqGslB6f5UvEehdhwsuD0W15+9fsH26WanCRHdjh4UDN7YjzldufHrG4zJWHklDaaWS+C4FZ1zAkEAuy77PUJfhjwwKqO06BDNZZiAkfdOkDyAR7l/tbCd8KWDETlrpsD2u2UR9+o+pbPukeTsHIJqVnrhNYGE6wTbAQJBAJ9qRLQJY19X6EH3+gi6M2IPyeifUZlRXCeqs9C199OdqjRQUGMPAjBwe1ZFTc7AyAwvC1+bj9qc063Ty3sPqTw="; //公鑰 string strPub = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDM9As4OkFLUyEmtqVJJaHkAf0xKR3aq38q6FqSi1vQfy1hUjhh/Z8k5NaT2WnmnchGUOoGsvwtKVAHrS62qvWzAPy7sauvG/O7hu1l+3SsrRDtv/O6hqRjEbPex/hpZM1opdkDtiiuHB5wHFYnD7JouWme9lNAViRcj4bXfqnZqwIDAQAB"; string strDJM = "泰酷拉!123ABC";//待加密字串 strDJM = "泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC泰酷拉!123ABC";//待加密字串,超過117字元的測試 Console.WriteLine("待加密字串:" + strDJM); string charSet = "UTF-8"; string strJMH = RsaEncryptUtil.RSAEncrypt(strDJM, charSet, strPub);//密文 Console.WriteLine("密文:" + strJMH); string strDecryptedByPkcs8 = RsaEncryptUtil.RSADecrypt(strJMH, charSet, strPriPkcs8, "PKCS8");//使用PKCS8格式私鑰解密 Console.WriteLine("PKCS8 解密後:" + strDecryptedByPkcs8); string strDecryptedByPkcs1 = RsaEncryptUtil.RSADecrypt(strJMH, charSet, strPriPkcs1, "PKCS1");//使用PKCS1格式私鑰解密 Console.WriteLine("PKCS1 解密後:" + strDecryptedByPkcs1); } catch (Exception ex) { Console.WriteLine("ex:" + ex.Message); } Console.WriteLine("hello"); Console.ReadKey(); } } }
-