2019-02-26æ¥æ´æ°ï¼æºç æ´æ°è³githubä¸ã
2017-12-04æ¥æ´æ°ï¼å¢å æ¯æ微信æ¯ä»ã代ç 注éä¸//ââââ.NET 4.6以åç¹æââââçå«ç¨ï¼é£ä¸ªä¸å¯¹ã
RSAé对称å å¯ãç®ææ¼è¦å§ï¼ç´è¯´å¹²è´§ãï¼å¨æ¤ç¹å«æè°¢ä¸è²³è¿å¶ï¼æä¾JAVAççå ¬é¥ç§é¥ï¼
C#RSAå ç¾è§£ç¾å å¯æ¯è¾å¸¸è§ï¼ä¸è¬éå°çé®é¢æ¯é.NET代ç çå ç¾éªç¾ãå å¯è§£å¯ã以ä¸å°±æ¯è§£å³æ¤ç±»é®é¢çã
ä¸è¬æ åµä¸æ¯ãå ¬é¥å å¯ï¼ç§é¥è§£å¯ãã
æäºåæå欢乱æï¼ãç§é¥å å¯ï¼å ¬é¥è§£å¯ããï¼è¯¦è§è¿éï¼http://blog.csdn.net/gzy11/article/details/58609719
1ãRSA å ·ä½æ¯ä»ä¹è¯·ç¹å»è¿é
    * éè¦çäºæ 说ä¸éï¼ä¸æçèªå·±æ¶è¡¥å»ã
    * RSAå å¯è§£å¯ï¼ç§é¥è§£å¯ï¼å ¬é¥å å¯ã
    * RSAæ°åç¾å-ä¿ç§°å ç¾éªç¾ï¼ç§é¥å ç¾ï¼å ¬é¥éªç¾ãÂ
    * RSAå å¯è§£å¯ï¼ç§é¥è§£å¯ï¼å ¬é¥å å¯ã
    * RSAæ°åç¾å-ä¿ç§°å ç¾éªç¾ï¼ç§é¥å ç¾ï¼å ¬é¥éªç¾ãÂ
    * RSAå å¯è§£å¯ï¼ç§é¥è§£å¯ï¼å ¬é¥å å¯ã
    * RSAæ°åç¾å-ä¿ç§°å ç¾éªç¾ï¼ç§é¥å ç¾ï¼å ¬é¥éªç¾ãÂ
Â
å ³äºhashAlgorithmåæ°å¼æï¼MD5ãSHA1ãSHA256ãSHA384ãSHA512ãå ¶å®çèªå·±è¯å§ï¼å¯è½è¿ä¸ªä¸å ¨ã
å ·ä½javaä¸C#对åºçåè§è¿éï¼ä» ä¾åèï¼è¯¦è§æµè¯ä»£ç é¨åï¼ï¼http://blog.csdn.net/gzy11/article/details/58609719
å¨è¿éæéä¸åä½å¯¹æ¥å ¶ä»å®é.NET对æ¥æ¶æ³¨æäºé¡¹å§ãä¸é¢æ¯èªå·±æ»ç»çä¸äºæ¹æ³ã
ã注æç®åçå°æ¹ï¼è¶æ¯ç®åçå°æ¹è¶å®¹æé´æ²éç¿»è¹ãã
1ãé¦å æµè¯çæ¶åï¼å ç»å¯¹æ¹æä¾ä¸æ´å¥å ¬ç§é¥ï¼æè é®å¯¹æ¹è¦ä¸å¥ï¼ä»¥ä¾¿æ¹ä¾¿èªå·±å对æ¹æµè¯ä½¿ç¨ãå 为è·ä½ 对æ¥çå å¼å¤åè·ä½ ä¸å¨ä¸ä¸ªé¢éä¸ï¼ä½ 说çä»ä¸æç½ãä»ä¹ä¸æç½ä½ 说çã
2ã对æ¹æ¯å¦ä½¿ç¨safeurlï¼è¯·é®æ¸ æ¥ãå¦æ²¡æ请忽ç¥ä»¥ä¸ä»£ç ã
Â
public static string ConvertEncodeBase64URLSafe(string data)
{
return data.Replace("=", String.Empty).Replace('+', '-').Replace('/', '_');
}
public static string ConvertDecodeBase64URLSafe(string data)
{
data = data.Replace('-', '+').Replace('_', '/');
int len = data.Length % 4;
if (len > 0)
{
data += "====".Substring(0, 4 - len);
}
return data;
}
Â
Â
Â
Â
3ã对æ¹æ¯å¦ä½¿ç¨MD5withRasã请çå ç¾ãéªç¾ã
4ãä¸äºåå æµè¯å使ç¨æ¹æ³ï¼é½å¨
5ãrsa.KeySize / 8-11æ¯å¯å å¯çé¿åº¦ï¼RSAæ¯æ¬ææ¯å å¯é¨ååå ¶éè¦çæ°æ®ï¼å¦æè¶ è¿ï¼å¤åæ¯ä½ èªå·±ç设计æé®é¢ã请æ¹è¿ï¼ç½ä¸æå段å å¯è§£å¯è¶ é¿æ°æ®çæ¹æ³ãä¸å»ºè®®éç¨ï¼æ¹è¿èªå·±ç设计å»ã
6ãã注æåæ¹çç¼ç æ ¼å¼ãã
      ã注æåæ¹çç¼ç æ ¼å¼ãã
      ã注æåæ¹çç¼ç æ ¼å¼ãã
7ãã注ææ¬æçå å¯ãå ç¾ç»æé½æ¯Base64çãã
      ã注ææ¬æçå å¯ãå ç¾ç»æé½æ¯Base64çãã
      ã注ææ¬æçå å¯ãå ç¾ç»æé½æ¯Base64çãã
Â
byte[] dataBytes = Convert.FromBase64String(data);
string base64Str = Convert.ToBase64String(signer.GenerateSignature());
Â
Â
Â
8ã对æ¹æ¯ä¸ªåæï¼ä½¿ç¨ãç§é¥å å¯ãå ¬é¥è§£å¯ãã详è§è¿éï¼http://blog.csdn.net/gzy11/article/details/58609719
9ã注æå ç¾éªç¾Algorithmåæ°ã详è§è¿éï¼http://blog.csdn.net/gzy11/article/details/58609719 C#ä¸JAVA对åºçå ³ç³»ï¼ææ ã
10ãJAVA 使ç¨PEMç为PKCS8ï¼è½¬æ¢å½ä»¤å¦ä¸
PKCS8转æ¢ä¸ºæ®éçPKCS1ï¼æ¬ç¤ºå使ç¨æ®éPEMç§é¥
openssl pkcs8 -in private_key_pkcs8.pem -nocrypt -out private_key.pem
11ã微信RSAæ¯ä»ãå¿ é¡»ä½¿ç¨.NET4.6以ä¸çæ¬ ã
使ç¨å¦ä¸ä»£ç ã
https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_7&index=4
Â
public static string DecryptPEMByWeiXin(string privateKeyPEM, string data, string encoding = "UTF-8")
       {
           RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
           byte[] cipherbytes;
           rsa.LoadPrivateKeyPEM(privateKeyPEM);
           var padding = System.Security.Cryptography.RSAEncryptionPadding.Pkcs1;
           cipherbytes = rsa.Decrypt(Convert.FromBase64String(data), padding);
        Â
           return Encoding.GetEncoding(encoding).GetString(cipherbytes);
       }
Â
Â
以ä¸æ¯RSAç帮å©ç±»ï¼
å ç¾ï¼
Â
#region å ç¾
/// <summary>
/// RSAç¾å
/// </summary>
/// <param name="privateKeyJava">ç§é¥</param>
/// <param name="data">å¾
ç¾åçå
容</param>
/// <returns></returns>
public static string RSASignJava(string data, string privateKeyJava, string hashAlgorithm = "MD5", string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromPrivateKeyJavaString(privateKeyJava);//å è½½ç§é¥
//RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsa);
设置ç¾åçç®æ³ä¸ºMD5 MD5withRSA ç¾å
//RSAFormatter.SetHashAlgorithm(hashAlgorithm);
var dataBytes = Encoding.GetEncoding(encoding).GetBytes(data);
var HashbyteSignature = rsa.SignData(dataBytes, hashAlgorithm);
return Convert.ToBase64String(HashbyteSignature);
//byte[] HashbyteSignature = ConvertToRgbHash(data, encoding);
//byte[] dataBytes =Encoding.GetEncoding(encoding).GetBytes(data);
//HashbyteSignature = rsa.SignData(dataBytes, hashAlgorithm);
//return Convert.ToBase64String(HashbyteSignature);
//æ§è¡ç¾å
//EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
//return Convert.ToBase64String(RSAFormatter.CreateSignature(HashbyteSignature));
//return result.Replace("=", String.Empty).Replace('+', '-').Replace('/', '_');
}
/// <summary>
/// RSAç¾å
/// </summary>
/// <param name="privateKeyPEM">ç§é¥</param>
/// <param name="data">å¾
ç¾åçå
容</param>
/// <returns></returns>
public static string RSASignPEM(string data, string privateKeyPEM, string hashAlgorithm = "MD5", string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.LoadPrivateKeyPEM(privateKeyPEM);//å è½½ç§é¥
var dataBytes = Encoding.GetEncoding(encoding).GetBytes(data);
var HashbyteSignature = rsa.SignData(dataBytes, hashAlgorithm);
return Convert.ToBase64String(HashbyteSignature);
}
/// <summary>
/// RSAç¾åCSharp
/// </summary>
/// <param name="privateKeyCSharp">ç§é¥</param>
/// <param name="data">å¾
ç¾åçå
容</param>
/// <returns></returns>
public static string RSASignCSharp(string data, string privateKeyCSharp, string hashAlgorithm = "MD5", string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privateKeyCSharp);//å è½½ç§é¥
var dataBytes = Encoding.GetEncoding(encoding).GetBytes(data);
var HashbyteSignature = rsa.SignData(dataBytes, hashAlgorithm);
return Convert.ToBase64String(HashbyteSignature);
}
#endregion
Â
Â
Â
éªç¾ï¼
Â
#region éªç¾
/// <summary>
/// éªè¯ç¾å-æ¹æ³ä¸
/// </summary>
/// <param name="data"></param>
/// <param name="signature"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static bool VerifyJava(string data, string publicKeyJava, string signature, string hashAlgorithm = "MD5", string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//导å
¥å
¬é¥ï¼åå¤éªè¯ç¾å
rsa.FromPublicKeyJavaString(publicKeyJava);
//è¿åæ°æ®éªè¯ç»æ
byte[] Data = Encoding.GetEncoding(encoding).GetBytes(data);
byte[] rgbSignature = Convert.FromBase64String(signature);
return rsa.VerifyData(Data, hashAlgorithm, rgbSignature);
//return SignatureDeformatter(publicKeyJava, data, signature);
//return CheckSign(publicKeyJava, data, signature);
//return rsa.VerifyData(Encoding.GetEncoding(encoding).GetBytes(data), "MD5", Encoding.GetEncoding(encoding).GetBytes(signature));
}
/// <summary>
/// éªè¯ç¾åPEM
/// </summary>
/// <param name="data"></param>
/// <param name="signature"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static bool VerifyPEM(string data, string publicKeyPEM, string signature, string hashAlgorithm = "MD5", string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//导å
¥å
¬é¥ï¼åå¤éªè¯ç¾å
rsa.LoadPublicKeyPEM(publicKeyPEM);
//è¿åæ°æ®éªè¯ç»æ
byte[] Data = Encoding.GetEncoding(encoding).GetBytes(data);
byte[] rgbSignature = Convert.FromBase64String(signature);
return rsa.VerifyData(Data, hashAlgorithm, rgbSignature);
}
/// <summary>
/// éªè¯ç¾åCSharp
/// </summary>
/// <param name="data"></param>
/// <param name="signature"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static bool VerifyCSharp(string data, string publicKeyCSharp, string signature, string hashAlgorithm = "MD5", string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//导å
¥å
¬é¥ï¼åå¤éªè¯ç¾å
rsa.LoadPublicKeyPEM(publicKeyCSharp);
//è¿åæ°æ®éªè¯ç»æ
byte[] Data = Encoding.GetEncoding(encoding).GetBytes(data);
byte[] rgbSignature = Convert.FromBase64String(signature);
return rsa.VerifyData(Data, hashAlgorithm, rgbSignature);
}
#region ç¾åéªè¯-æ¹æ³äº
/// <summary>
/// ç¾åéªè¯
/// </summary>
/// <param name="publicKey">å
¬é¥</param>
/// <param name="p_strHashbyteDeformatter">å¾
éªè¯çç¨æ·å</param>
/// <param name="signature">注åç </param>
/// <returns>ç¾åæ¯å¦ç¬¦å</returns>
public static bool SignatureDeformatter(string publicKey, string data, string signature, string hashAlgorithm = "MD5")
{
try
{
byte[] rgbHash = ConvertToRgbHash(data);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//导å
¥å
¬é¥ï¼åå¤éªè¯ç¾å
rsa.FromPublicKeyJavaString(publicKey);
RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(rsa);
deformatter.SetHashAlgorithm("MD5");
byte[] rgbSignature = Convert.FromBase64String(signature);
if (deformatter.VerifySignature(rgbHash, rgbSignature))
{
return true;
}
return false;
}
catch
{
return false;
}
}
/// <summary>
/// ç¾åæ°æ®è½¬å为RgbHash
/// </summary>
/// <param name="data"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static byte[] ConvertToRgbHash(string data, string encoding = "UTF-8")
{
using (MD5 md5 = new MD5CryptoServiceProvider())
{
byte[] bytes_md5_in = Encoding.GetEncoding(encoding).GetBytes(data);
return md5.ComputeHash(bytes_md5_in);
}
}
#endregion
#region ç¾åéªè¯-æ¹æ³ä¸
/// <summary>
/// éªè¯ç¾å
/// </summary>
/// <param name="data">åå§æ°æ®</param>
/// <param name="sign">ç¾å</param>
/// <returns></returns>
public static bool CheckSign(string publicKey, string data, string sign, string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromPublicKeyJavaString(publicKey);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] Data = Encoding.GetEncoding(encoding).GetBytes(data);
byte[] rgbSignature = Convert.FromBase64String(sign);
if (rsa.VerifyData(Data, md5, rgbSignature))
{
return true;
}
return false;
}
#endregion
#endregion
Â
Â
Â
å å¯ï¼
Â
#region å å¯
/// <summary>
/// RSAå å¯
/// </summary>
/// <param name="publicKeyJava"></param>
/// <param name="data"></param>
/// <returns></returns>
public static string EncryptJava(string publicKeyJava, string data, string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromPublicKeyJavaString(publicKeyJava);
//ââââ.NET 4.6以åç¹æââââ
//HashAlgorithmName hashName = new System.Security.Cryptography.HashAlgorithmName(hashAlgorithm);
//RSAEncryptionPadding padding = RSAEncryptionPadding.OaepSHA512;//RSAEncryptionPadding.CreateOaep(hashName);//.NET 4.6以åç¹æ
//cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), padding);
//âââââââââââââââââ
//ââââ.NET 4.6以å请ç¨æ¤æ®µä»£ç ââââ
cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), false);
return Convert.ToBase64String(cipherbytes);
}
/// <summary>
/// RSAå å¯
/// </summary>
/// <param name="publicKeyCSharp"></param>
/// <param name="data"></param>
/// <returns></returns>
public static string EncryptCSharp(string publicKeyCSharp, string data, string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(publicKeyCSharp);
//ââââ.NET 4.6以åç¹æââââ
//HashAlgorithmName hashName = new System.Security.Cryptography.HashAlgorithmName(hashAlgorithm);
//RSAEncryptionPadding padding = RSAEncryptionPadding.OaepSHA512;//RSAEncryptionPadding.CreateOaep(hashName);//.NET 4.6以åç¹æ
//cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), padding);
//âââââââââââââââââ
//ââââ.NET 4.6以å请ç¨æ¤æ®µä»£ç ââââ
cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), false);
return Convert.ToBase64String(cipherbytes);
}
/// <summary>
/// RSAå å¯PEMç§é¥
/// </summary>
/// <param name="publicKeyPEM"></param>
/// <param name="data"></param>
/// <returns></returns>
public static string EncryptPEM(string publicKeyPEM, string data, string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.LoadPublicKeyPEM(publicKeyPEM);
//ââââ.NET 4.6以åç¹æââââ
//HashAlgorithmName hashName = new System.Security.Cryptography.HashAlgorithmName(hashAlgorithm);
//RSAEncryptionPadding padding = RSAEncryptionPadding.OaepSHA512;//RSAEncryptionPadding.CreateOaep(hashName);//.NET 4.6以åç¹æ
//cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), padding);
//âââââââââââââââââ
//ââââ.NET 4.6以å请ç¨æ¤æ®µä»£ç ââââ
cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(data), false);
return Convert.ToBase64String(cipherbytes);
}
#endregion
Â
Â
Â
解å¯ï¼
Â
#region 解å¯
/// <summary>
/// RSA解å¯
/// </summary>
/// <param name="privateKeyJava"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string DecryptJava(string privateKeyJava, string data, string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromPrivateKeyJavaString(privateKeyJava);
//ââââ.NET 4.6以åç¹æââââ
//RSAEncryptionPadding padding = RSAEncryptionPadding.CreateOaep(new System.Security.Cryptography.HashAlgorithmName(hashAlgorithm));//.NET 4.6以åç¹æ
//cipherbytes = rsa.Decrypt(Encoding.GetEncoding(encoding).GetBytes(data), padding);
//âââââââââââââââââ
//ââââ.NET 4.6以å请ç¨æ¤æ®µä»£ç ââââ
cipherbytes = rsa.Decrypt(Convert.FromBase64String(data), false);
return Encoding.GetEncoding(encoding).GetString(cipherbytes);
}
/// <summary>
/// RSA解å¯
/// </summary>
/// <param name="privateKeyCSharp"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string DecryptCSharp(string privateKeyCSharp, string data, string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(privateKeyCSharp);
//ââââ.NET 4.6以åç¹æââââ
//RSAEncryptionPadding padding = RSAEncryptionPadding.CreateOaep(new System.Security.Cryptography.HashAlgorithmName(hashAlgorithm));//.NET 4.6以åç¹æ
//cipherbytes = rsa.Decrypt(Encoding.GetEncoding(encoding).GetBytes(data), padding);
//âââââââââââââââââ
//ââââ.NET 4.6以å请ç¨æ¤æ®µä»£ç ââââ
cipherbytes = rsa.Decrypt(Convert.FromBase64String(data), false);
return Encoding.GetEncoding(encoding).GetString(cipherbytes);
}
/// <summary>
/// RSA解å¯
/// </summary>
/// <param name="privateKeyPEM"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string DecryptPEM(string privateKeyPEM, string data, string encoding = "UTF-8")
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.LoadPrivateKeyPEM(privateKeyPEM);
//ââââ.NET 4.6以åç¹æââââ
//RSAEncryptionPadding padding = RSAEncryptionPadding.CreateOaep(new System.Security.Cryptography.HashAlgorithmName(hashAlgorithm));//.NET 4.6以åç¹æ
//cipherbytes = rsa.Decrypt(Encoding.GetEncoding(encoding).GetBytes(data), padding);
//âââââââââââââââââ
//ââââ.NET 4.6以å请ç¨æ¤æ®µä»£ç ââââ
cipherbytes = rsa.Decrypt(Convert.FromBase64String(data), false);
return Encoding.GetEncoding(encoding).GetString(cipherbytes);
}
#endregion
Â
Â
Â
RSAæ©å±æ¹æ³ï¼
Â
public static class RSAExtensions
{
/// <summary>
/// æjavaçç§é¥è½¬æ¢æ.netçxmlæ ¼å¼
/// </summary>
/// <param name="rsa"></param>
/// <param name="privateJavaKey"></param>
/// <returns></returns>
public static string ConvertToXmlPrivateKey(this RSA rsa, string privateJavaKey)
{
RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateJavaKey));
string xmlPrivateKey = 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()));
return xmlPrivateKey;
}
/// <summary>
/// RSAå è½½JAVA PrivateKey
/// </summary>
/// <param name="privateJavaKey">javaæä¾ç第ä¸æ¹ç§é¥</param>
/// <returns></returns>
public static void FromPrivateKeyJavaString(this RSA rsa, string privateJavaKey)
{
string xmlPrivateKey = rsa.ConvertToXmlPrivateKey(privateJavaKey);
rsa.FromXmlString(xmlPrivateKey);
}
/// <summary>
/// æjavaçå
¬é¥è½¬æ¢æ.netçxmlæ ¼å¼
/// </summary>
/// <param name="privateKey">javaæä¾ç第ä¸æ¹å
¬é¥</param>
/// <returns></returns>
public static string ConvertToXmlPublicJavaKey(this RSA rsa, string publicJavaKey)
{
RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicJavaKey));
string xmlpublicKey = string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
return xmlpublicKey;
}
/// <summary>
/// æjavaçç§é¥è½¬æ¢æ.netçxmlæ ¼å¼
/// </summary>
/// <param name="privateKey">javaæä¾ç第ä¸æ¹å
¬é¥</param>
/// <returns></returns>
public static void FromPublicKeyJavaString(this RSA rsa, string publicJavaKey)
{
string xmlpublicKey = rsa.ConvertToXmlPublicJavaKey(publicJavaKey);
rsa.FromXmlString(xmlpublicKey);
}
/ <summary>
/ RSAå
¬é¥æ ¼å¼è½¬æ¢ï¼java->.net
/ </summary>
/ <param name="publicKey">javaçæçå
¬é¥</param>
/ <returns></returns>
//private static string ConvertJavaPublicKeyToDotNet(this RSA rsa,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()));
//}
/// <summary>Extension method for initializing a RSACryptoServiceProvider from PEM data string.</summary>
#region Methods
/// <summary>Extension method which initializes an RSACryptoServiceProvider from a DER public key blob.</summary>
public static void LoadPublicKeyDER(this RSACryptoServiceProvider provider, byte[] DERData)
{
byte[] RSAData = GetRSAFromDER(DERData);
byte[] publicKeyBlob = GetPublicKeyBlobFromRSA(RSAData);
provider.ImportCspBlob(publicKeyBlob);
}
/// <summary>Extension method which initializes an RSACryptoServiceProvider from a DER private key blob.</summary>
public static void LoadPrivateKeyDER(this RSACryptoServiceProvider provider, byte[] DERData)
{
byte[] privateKeyBlob = GetPrivateKeyDER(DERData);
provider.ImportCspBlob(privateKeyBlob);
}
/// <summary>Extension method which initializes an RSACryptoServiceProvider from a PEM public key string.</summary>
public static void LoadPublicKeyPEM(this RSACryptoServiceProvider provider, string sPEM)
{
byte[] DERData = GetDERFromPEM(sPEM);
LoadPublicKeyDER(provider, DERData);
}
/// <summary>Extension method which initializes an RSACryptoServiceProvider from a PEM private key string.</summary>
public static void LoadPrivateKeyPEM(this RSACryptoServiceProvider provider, string sPEM)
{
byte[] DERData = GetDERFromPEM(sPEM);
LoadPrivateKeyDER(provider, DERData);
}
/// <summary>Returns a public key blob from an RSA public key.</summary>
internal static byte[] GetPublicKeyBlobFromRSA(byte[] RSAData)
{
byte[] data = null;
UInt32 dwCertPublicKeyBlobSize = 0;
if (CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING,
new IntPtr((int)CRYPT_OUTPUT_TYPES.RSA_CSP_PUBLICKEYBLOB), RSAData, (UInt32)RSAData.Length, CRYPT_DECODE_FLAGS.NONE,
data, ref dwCertPublicKeyBlobSize))
{
data = new byte[dwCertPublicKeyBlobSize];
if (!CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING,
new IntPtr((int)CRYPT_OUTPUT_TYPES.RSA_CSP_PUBLICKEYBLOB), RSAData, (UInt32)RSAData.Length, CRYPT_DECODE_FLAGS.NONE,
data, ref dwCertPublicKeyBlobSize))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
else
throw new Win32Exception(Marshal.GetLastWin32Error());
return data;
}
/// <summary>Converts DER binary format to a CAPI CRYPT_PRIVATE_KEY_INFO structure.</summary>
internal static byte[] GetPrivateKeyDER(byte[] DERData)
{
byte[] data = null;
UInt32 dwRSAPrivateKeyBlobSize = 0;
IntPtr pRSAPrivateKeyBlob = IntPtr.Zero;
if (CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING, new IntPtr((int)CRYPT_OUTPUT_TYPES.PKCS_RSA_PRIVATE_KEY),
DERData, (UInt32)DERData.Length, CRYPT_DECODE_FLAGS.NONE, data, ref dwRSAPrivateKeyBlobSize))
{
data = new byte[dwRSAPrivateKeyBlobSize];
if (!CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING, new IntPtr((int)CRYPT_OUTPUT_TYPES.PKCS_RSA_PRIVATE_KEY),
DERData, (UInt32)DERData.Length, CRYPT_DECODE_FLAGS.NONE, data, ref dwRSAPrivateKeyBlobSize))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
else
throw new Win32Exception(Marshal.GetLastWin32Error());
return data;
}
/// <summary>Converts DER binary format to a CAPI CERT_PUBLIC_KEY_INFO structure containing an RSA key.</summary>
internal static byte[] GetRSAFromDER(byte[] DERData)
{
byte[] data = null;
byte[] publicKey = null;
CERT_PUBLIC_KEY_INFO info;
UInt32 dwCertPublicKeyInfoSize = 0;
IntPtr pCertPublicKeyInfo = IntPtr.Zero;
if (CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING, new IntPtr((int)CRYPT_OUTPUT_TYPES.X509_PUBLIC_KEY_INFO),
DERData, (UInt32)DERData.Length, CRYPT_DECODE_FLAGS.NONE, data, ref dwCertPublicKeyInfoSize))
{
data = new byte[dwCertPublicKeyInfoSize];
if (CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING, new IntPtr((int)CRYPT_OUTPUT_TYPES.X509_PUBLIC_KEY_INFO),
DERData, (UInt32)DERData.Length, CRYPT_DECODE_FLAGS.NONE, data, ref dwCertPublicKeyInfoSize))
{
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
info = (CERT_PUBLIC_KEY_INFO)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(CERT_PUBLIC_KEY_INFO));
publicKey = new byte[info.PublicKey.cbData];
Marshal.Copy(info.PublicKey.pbData, publicKey, 0, publicKey.Length);
}
finally
{
handle.Free();
}
}
else
throw new Win32Exception(Marshal.GetLastWin32Error());
}
else
throw new Win32Exception(Marshal.GetLastWin32Error());
return publicKey;
}
/// <summary>Extracts the binary data from a PEM file.</summary>
internal static byte[] GetDERFromPEM(string sPEM)
{
UInt32 dwSkip, dwFlags;
UInt32 dwBinarySize = 0;
if (!CryptStringToBinary(sPEM, (UInt32)sPEM.Length, CRYPT_STRING_FLAGS.CRYPT_STRING_BASE64HEADER, null, ref dwBinarySize, out dwSkip, out dwFlags))
throw new Win32Exception(Marshal.GetLastWin32Error());
byte[] decodedData = new byte[dwBinarySize];
if (!CryptStringToBinary(sPEM, (UInt32)sPEM.Length, CRYPT_STRING_FLAGS.CRYPT_STRING_BASE64HEADER, decodedData, ref dwBinarySize, out dwSkip, out dwFlags))
throw new Win32Exception(Marshal.GetLastWin32Error());
return decodedData;
}
#endregion Methods
#region P/Invoke Constants
/// <summary>Enumeration derived from Crypto API.</summary>
internal enum CRYPT_ACQUIRE_CONTEXT_FLAGS : uint
{
CRYPT_NEWKEYSET = 0x8,
CRYPT_DELETEKEYSET = 0x10,
CRYPT_MACHINE_KEYSET = 0x20,
CRYPT_SILENT = 0x40,
CRYPT_DEFAULT_CONTAINER_OPTIONAL = 0x80,
CRYPT_VERIFYCONTEXT = 0xF0000000
}
/// <summary>Enumeration derived from Crypto API.</summary>
internal enum CRYPT_PROVIDER_TYPE : uint
{
PROV_RSA_FULL = 1
}
/// <summary>Enumeration derived from Crypto API.</summary>
internal enum CRYPT_DECODE_FLAGS : uint
{
NONE = 0,
CRYPT_DECODE_ALLOC_FLAG = 0x8000
}
/// <summary>Enumeration derived from Crypto API.</summary>
internal enum CRYPT_ENCODING_FLAGS : uint
{
PKCS_7_ASN_ENCODING = 0x00010000,
X509_ASN_ENCODING = 0x00000001,
}
/// <summary>Enumeration derived from Crypto API.</summary>
internal enum CRYPT_OUTPUT_TYPES : int
{
X509_PUBLIC_KEY_INFO = 8,
RSA_CSP_PUBLICKEYBLOB = 19,
PKCS_RSA_PRIVATE_KEY = 43,
PKCS_PRIVATE_KEY_INFO = 44
}
/// <summary>Enumeration derived from Crypto API.</summary>
internal enum CRYPT_STRING_FLAGS : uint
{
CRYPT_STRING_BASE64HEADER = 0,
CRYPT_STRING_BASE64 = 1,
CRYPT_STRING_BINARY = 2,
CRYPT_STRING_BASE64REQUESTHEADER = 3,
CRYPT_STRING_HEX = 4,
CRYPT_STRING_HEXASCII = 5,
CRYPT_STRING_BASE64_ANY = 6,
CRYPT_STRING_ANY = 7,
CRYPT_STRING_HEX_ANY = 8,
CRYPT_STRING_BASE64X509CRLHEADER = 9,
CRYPT_STRING_HEXADDR = 10,
CRYPT_STRING_HEXASCIIADDR = 11,
CRYPT_STRING_HEXRAW = 12,
CRYPT_STRING_NOCRLF = 0x40000000,
CRYPT_STRING_NOCR = 0x80000000
}
#endregion P/Invoke Constants
#region P/Invoke Structures
/// <summary>Structure from Crypto API.</summary>
[StructLayout(LayoutKind.Sequential)]
internal struct CRYPT_OBJID_BLOB
{
internal UInt32 cbData;
internal IntPtr pbData;
}
/// <summary>Structure from Crypto API.</summary>
[StructLayout(LayoutKind.Sequential)]
internal struct CRYPT_ALGORITHM_IDENTIFIER
{
internal IntPtr pszObjId;
internal CRYPT_OBJID_BLOB Parameters;
}
/// <summary>Structure from Crypto API.</summary>
[StructLayout(LayoutKind.Sequential)]
struct CRYPT_BIT_BLOB
{
internal UInt32 cbData;
internal IntPtr pbData;
internal UInt32 cUnusedBits;
}
/// <summary>Structure from Crypto API.</summary>
[StructLayout(LayoutKind.Sequential)]
struct CERT_PUBLIC_KEY_INFO
{
internal CRYPT_ALGORITHM_IDENTIFIER Algorithm;
internal CRYPT_BIT_BLOB PublicKey;
}
#endregion P/Invoke Structures
#region P/Invoke Functions
/// <summary>Function for Crypto API.</summary>
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CryptDestroyKey(IntPtr hKey);
/// <summary>Function for Crypto API.</summary>
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CryptImportKey(IntPtr hProv, byte[] pbKeyData, UInt32 dwDataLen, IntPtr hPubKey, UInt32 dwFlags, ref IntPtr hKey);
/// <summary>Function for Crypto API.</summary>
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CryptReleaseContext(IntPtr hProv, Int32 dwFlags);
/// <summary>Function for Crypto API.</summary>
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CryptAcquireContext(ref IntPtr hProv, string pszContainer, string pszProvider, CRYPT_PROVIDER_TYPE dwProvType, CRYPT_ACQUIRE_CONTEXT_FLAGS dwFlags);
/// <summary>Function from Crypto API.</summary>
[DllImport("crypt32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CryptStringToBinary(string sPEM, UInt32 sPEMLength, CRYPT_STRING_FLAGS dwFlags, [Out] byte[] pbBinary, ref UInt32 pcbBinary, out UInt32 pdwSkip, out UInt32 pdwFlags);
/// <summary>Function from Crypto API.</summary>
[DllImport("crypt32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CryptDecodeObjectEx(CRYPT_ENCODING_FLAGS dwCertEncodingType, IntPtr lpszStructType, byte[] pbEncoded, UInt32 cbEncoded, CRYPT_DECODE_FLAGS dwFlags, IntPtr pDecodePara, ref byte[] pvStructInfo, ref UInt32 pcbStructInfo);
/// <summary>Function from Crypto API.</summary>
[DllImport("crypt32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CryptDecodeObject(CRYPT_ENCODING_FLAGS dwCertEncodingType, IntPtr lpszStructType, byte[] pbEncoded, UInt32 cbEncoded, CRYPT_DECODE_FLAGS flags, [In, Out] byte[] pvStructInfo, ref UInt32 cbStructInfo);
#endregion P/Invoke Functions
}
Â
Â
Â
åå æµè¯ï¼
[TestClass]
public class RSAHelperTest
{
string publicKeyJava = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkixtIe0MzCFDW3eCDSq/6SFFN+eK+zg1q62SX
TbJprt7zOD6D9A8zU1fTEzY9+0gVUYmOMPOF8jI8EMZOnl2jDUtn3KdD2Uuee3/cmEfMpAN++KMG
6Tfm3p3Iz4kw/dLmM1EAkADIp4zFmkvxd/BN+dmT/1Tp87mMUQBS8mvdRwIDAQAB";
string privateKeyJava = @"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKSLG0h7QzMIUNbd4INKr/pIUU35
4r7ODWrrZJdNsmmu3vM4PoP0DzNTV9MTNj37SBVRiY4w84XyMjwQxk6eXaMNS2fcp0PZS557f9yY
R8ykA374owbpN+bencjPiTD90uYzUQCQAMinjMWaS/F38E352ZP/VOnzuYxRAFLya91HAgMBAAEC
gYBXJCSq2jzYhgrqAE+quJ9CGZKVjUV8b3vDqC55wvg96BLDFIJnN5XlRLo6Wu5bHP0r7SbAW0Rd
J8ta7DdZ450Kow+k2Z69gYO818cptQXYrs4ky6M48NXeSSoYeGESxW7LGJs++o2nGmVRkhj4DMYY
8lur1oYsyDAy/d3B0ucnwQJBAP0Kc2KCOl8xnbXUuoFhJHaVoKPWqdhody5sNHK+11Bgc3/ZhqNM
T1uIiiZnB3CTyfKeJAgX0fwde7fmtZHaUO8CQQCmd7a3qXvUbnQao03ITrthGRvGAJSAfTAG/VEx
2g1knxUmiq+bek+FGi7UYXYRZ/rVqX934ztTAOnBqVtnK4kpAkEAs6KAqVUkFUJG4LfFM2YAGcM9
SDJzXvNCcI1WaoM6nY/rTr7hCvp4d9WlpX+M04nHWtqTX79xTdasZrB9A68FtwJAHXWmIk6eGXQK
nAQ2abJ1OrPE1H+ZyDtfWn1N9zKNmDcG+TEl7q/wjq+ZhgBRcrciDtnWMxNFlmTc+WbNRC7SMQJB
AIBSE1kfhy3V63db91Gnr789G2QKy3TI46QEY3dirLiXWF3/tt+9K8itjeB22S5S7cWbzJ+2FIFW
mFB/DP3ER8Q=";
string publicKeyPEM = @"-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDe8W+2jrrWY55W8nZcWCBXQSJJ
uJxkannRsZbwiwJBftXCzN0wSaujwmT0S0Aqttnqg/sO+jDHVnpph8omZdSvnySt
/PiGEqmsk+6AsgZ7eczeokVkrFvVbjq13s6NVF8tgMds/w5VG8+uEECmiHCKM8/V
3oXhq7aLPLINFYggcQIDAQAB
-----END PUBLIC KEY-----
";
string privateKeyPEM = @"-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQDe8W+2jrrWY55W8nZcWCBXQSJJuJxkannRsZbwiwJBftXCzN0w
SaujwmT0S0Aqttnqg/sO+jDHVnpph8omZdSvnySt/PiGEqmsk+6AsgZ7eczeokVk
rFvVbjq13s6NVF8tgMds/w5VG8+uEECmiHCKM8/V3oXhq7aLPLINFYggcQIDAQAB
AoGAN9OnkMRe/u6FKOqgPvGGb7MMvf8vkmmjRaI91NBKaVI0fwpD3SKli2VTWGU7
lTaijPottQtriY8bKi/prAHV5wiSetsiPB9DQq8bN3rLtJPG6NCmwiOUhamBg9kD
VbqOb/1dg0vGL1zEKxtscaK5yDCBnyX+4LfAR5Upg0Q4t70CQQD8n5XxSdQpHNAf
sl26cTxf6YHCzHoqUfDo4SE74jWy8ooNF3qk0jaYy6+N1EOLwpMfm2MeWdxCYFmC
9NdbowIDAkEA4exKy9FGTPVkkVhAwva0fm7/mJSHsl7btPTJvmgEfO9eHbozYacQ
2PmbZd46uPzaI0LaDh8ICbaLvf8IPiVjewJAXIMUpggjaerjTLhFGsHdGkKpAm1f
T6AyWRYY1ZVBlQa9B45Rm4pf9BSjdY0GL2hR+IEvCy5dOvGN1idTtns7gQJAfiqO
TqptPxcfdYe6iuZpP95PAO+ZpEQTIEg/zgSa1QZ8Ic/VV+iLoRAr90SWuK4ESALS
cWZk/7+g5JbjDImmtwJAfBpGw+TVeMw+2Oo9GJnIM9Ga/hiYQDCkZ9A/4fYrNxxr
znkMqFQ28dhYQbKK9/WcPTjHGakpY/2hY/9dki6CJw==
-----END RSA PRIVATE KEY-----
";
[TestMethod]
public void RSA_Java_MD5Verify()
{
string data = "hello word!";
string hashAlgorithm = "MD5";
string signResult = RSAHelper.RSASignJava(data,privateKeyJava, hashAlgorithm);
string sign1 = "bI/x8LHsuvCVgO7YEBA0hvQWdpLzqdThfYKYEeo4YFTclhFV+R4QCN2FgYshZqjDTBzHAvRHxoRk4kbYWeOuVNG+W3exdtqfUuUp29EgxEPFfqECUqucgN/bFxrtKtNGOCAqOBSx6SBkRpByKEAH7FHsRmPS4Bd4MUT4xr9gGg8=";
if (sign1 == signResult)
{
}
bool result = RSAHelper.VerifyJava(data, publicKeyJava, signResult, hashAlgorithm);
if (!result)
{
int a = 0;
int b = 0 / a;
}
Console.WriteLine(hashAlgorithm + ":" + result);
}
[TestMethod]
public void RSA_Java_SHA1Verify()
{
//éè¿æµè¯
string data = "hello word!";
string hashAlgorithm = "SHA1";
string signResult = RSAHelper.RSASignJava(data,privateKeyJava, hashAlgorithm);
bool result = RSAHelper.VerifyJava(data,publicKeyJava, signResult, hashAlgorithm);
if (!result)
{
int a = 0;
int b = 0 / a;
}
Console.WriteLine(hashAlgorithm + ":" + result);
}
[TestMethod]
public void RSA_Java_SHA256Verify()
{
//æªéè¿
string data = "hello word!";
string hashAlgorithm = "SHA256";
string signResult = RSAHelper.RSASignJava(data,privateKeyJava, hashAlgorithm);
bool result = RSAHelper.VerifyJava(data,publicKeyJava, signResult, hashAlgorithm);
if (!result)
{
int a = 0;
int b = 0 / a;
}
Console.WriteLine(hashAlgorithm + ":" + result);
}
[TestMethod]
public void RSASign_JAVA_WithVerify()
{
string data = "hello word!";
//"RIPEMD160",
List<string> hashAlgorithmList = new List<string>() { "MD5", "SHA1", "SHA256", "SHA384", "SHA512" };//{MD5ãRIPEMD160ãSHA1ãSHA256ãSHA384ãSHA512}
foreach (var hashAlgorithm in hashAlgorithmList)
{
string signResult = RSAHelper.RSASignJava(data,privateKeyJava, hashAlgorithm);
bool result = RSAHelper.VerifyJava(data,publicKeyJava, signResult, hashAlgorithm);
if (!result)
{
int a = 0;
int b = 0 / a;
}
Console.WriteLine(hashAlgorithm + ":" + result);
}
}
[TestMethod]
public void RSASign_PEM_WithVerify()
{
string data = "hello word!";
//"RIPEMD160",
List<string> hashAlgorithmList = new List<string>() { "MD5", "SHA1", "SHA256", "SHA384", "SHA512" };//{MD5ãRIPEMD160ãSHA1ãSHA256ãSHA384ãSHA512}
foreach (var hashAlgorithm in hashAlgorithmList)
{
string signResult = RSAHelper.RSASignPEM(data, privateKeyPEM, hashAlgorithm);
bool result = RSAHelper.VerifyPEM(data, publicKeyPEM, signResult, hashAlgorithm);
if (!result)
{
int a = 0;
int b = 0 / a;
}
Console.WriteLine(hashAlgorithm + ":" + result);
}
}
[TestMethod]
public void RSA_Java_EncryptWithDecrypt()
{
string data = "hello word!";//"MD5",
string encryptResult = RSAHelper.EncryptJava(publicKeyJava,data);
string decryptResult = RSAHelper.DecryptJava(privateKeyJava,encryptResult);
if (data != decryptResult)
{
int a = 0;
int b = 0 / a;
}
}
[TestMethod]
public void RSA_PEM_EncryptWithDecrypt()
{
string data = "hello word!";//"MD5",
string encryptResult = RSAHelper.EncryptPEM(publicKeyPEM,data);
string decryptResult = RSAHelper.DecryptPEM(privateKeyPEM,encryptResult);
if (data != decryptResult)
{
int a = 0;
int b = 0 / a;
}
}
}
æµè¯ç»æï¼
ä¸è½½å°åï¼
http://download.csdn.net/detail/gzy11/9738814
å·²ä¸ä¼ github æéè¦çå¯èªè¡ä¸è½½
githubä¸è½½å°åï¼
https://github.com/winphone3721/WNSLP.Toolkits.RSA
CSDNä¸è½½ä¸é è°±ï¼ä¸è¦ç§¯åçç°å¨ä¸ç人å¤äºç§¯åè¦é£ä¹å¤ï¼è°æ²¡äºèå»æ´ç§¯ååãè¦æºç çå¯ä»¥ç´æ¥çè¨ï¼çä¸é®ç®±ï¼é®ç®±åå¤ä¸åæ¶ï¼æ¥é请èªè¡GitHubä¸è½½ã