上篇文章中写的RSA加密是针对C#的,现在外部调用的是PHP,我们平常见到的RSA无论公钥和私钥都是一长串数字,很显然C#生成的XML不是通用的加密。如果外部调用需要处理一下。
一、首先可以去网上找一些生成RSA密钥的工具,生成公钥和私钥。
二、拿到公钥和私钥需要处理一下,转换个XML格式的公钥和私钥
public class BouncyCastle { public 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> /// RSA公钥格式转换,java->.net /// </summary> /// <param name="publicKey">java生成的公钥</param> /// <returns></returns> public 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())); } }
三、生成工具生成的是2048位的公钥和私钥,这里加密和解密也要用2048位的
#region RSA的加密函数 //############################################################################## //RSA 方式加密 //说明KEY必须是XML的行式,返回的是字符串 //在有一点需要说明!!该加密方式有 长度 限制的!! //############################################################################## //RSA的加密函数 string public static string RSAEncrypt(string xmlPublicKey, string m_strEncryptString) { byte[] PlainTextBArray; byte[] CypherTextBArray; string Result; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048); rsa.FromXmlString(xmlPublicKey); PlainTextBArray = (new UTF8Encoding()).GetBytes(m_strEncryptString); CypherTextBArray = rsa.Encrypt(PlainTextBArray, false); Result = Convert.ToBase64String(CypherTextBArray); return Result; } #endregion #region RSA的解密函数 //RSA的解密函数 string public static string RSADecrypt(string xmlPrivateKey, string m_strDecryptString) { xmlPrivateKey = BouncyCastle.RSAPrivateKeyJava2DotNet(xmlPrivateKey); byte[] PlainTextBArray; byte[] DypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048); rsa.FromXmlString(xmlPrivateKey); PlainTextBArray = Convert.FromBase64String(m_strDecryptString); DypherTextBArray = rsa.Decrypt(PlainTextBArray, false); Result = (new UTF8Encoding()).GetString(DypherTextBArray); return Result; } #endregion
四、加密和解密
/// <summary> /// 加密 /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPost] public IHttpActionResult RSAEncrypt(RSARequest model) { string publicKey = BouncyCastle.RSAPublicKeyJava2DotNet(model.XmlPublicKey); string encryptionStr = RSACryption.RSAEncrypt(publicKey, model.StrEncryptString); return Ok(new { code = ResultCode.Success, encryptionStr = encryptionStr, message = "加密成功!" }); } /// <summary> ///解密 /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPost] public IHttpActionResult RSADecrypt(RSADecryptRequest model) { string str_Plain_Text = RSACryption.RSADecrypt(model.XmlPrivateKey, model.StrDecryptString); return Ok(new { code = ResultCode.Success, encryptionStr = str_Plain_Text, message = "解密成功!" }); }