1. 程式人生 > 實用技巧 >.net RSA 加解密

.net RSA 加解密

一、.net → java RSA轉換

 /// <summary>
    /// RSA金鑰格式轉換
    /// </summary>
    public static class RSAConverter
    {
        /// <summary>    
        /// RSA私鑰格式轉換,java->.net    
        /// </summary>    
        /// <param name="privateKey">java生成的RSA私鑰</param>    
        /// <returns></returns>
public static string RSAPrivateKeyJava2DotNet(this 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私鑰格式轉換,.net->java /// </summary> /// <param name="privateKey">.net生成的私鑰</param> /// <returns></returns> public static string RSAPrivateKeyDotNet2Java(this string privateKey) { XmlDocument doc = new XmlDocument(); doc.LoadXml(privateKey); BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText)); BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText)); BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText)); BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText)); BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText)); BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText)); BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText)); BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText)); RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv); PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam); byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded(); return Convert.ToBase64String(serializedPrivateBytes); } /// <summary> /// RSA公鑰格式轉換,java->.net /// </summary> /// <param name="publicKey">java生成的公鑰</param> /// <returns></returns> public static string RSAPublicKeyJava2DotNet(this 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> /// RSA公鑰格式轉換,.net->java /// </summary> /// <param name="publicKey">.net生成的公鑰</param> /// <returns></returns> public static string RSAPublicKeyDotNet2Java(this string publicKey) { XmlDocument doc = new XmlDocument(); doc.LoadXml(publicKey); BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText)); BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText)); RsaKeyParameters pub = new RsaKeyParameters(false, m, p); SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub); byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded(); return Convert.ToBase64String(serializedPublicBytes); } }

二、示例

       /// <summary>
        /// .net RSA私鑰解密
        /// </summary>
        private static void test()
        {
            string privateKey = @"MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA2fy4b8rHvwhvEGY3

hGSoIVHOPNN0DzEm01kZ0hpoxO6kZzmuecZRyTJTai42ujupqVSD4aWPuStuAul7

whzjtwIDAQABAkArY89+iRmLX+1jkUUZuENzJmpr1xcx8vGsff7SwGj8Uboic0Rc

ydQr74MX+j2k71PrfCaScX9P7Bo2WQixeTDpAiEA7QoIXFkuUj2dcj5XuaBUUww8

c+USpjDlSt+ocOaeAPUCIQDrbIz4Z1D1cXzCuWbLTQaVTC5p2seCpzGgjFt+ycv2

ewIhAK2twzB++4ywrrMtcLOHASIUfhrgxkhU3Tx+IfoPT8gdAiEAxZGwcFqMF57o

+6WJ/b0jdDGdldozqpLTVA/WTP3e/HcCIDmNqE4Lyx2tgIrQDIgi9R7Fe30vgCwR

0ju33PubEg5f";


            string k = "<?xml version='1.0'>" + privateKey.RSAPrivateKeyJava2DotNet();

            String enStr = "HItL1liIdBn3orZmCUSJoyQ8038aXfjYs7xCNoepYAbsG+a4u74Tiy+ppUVD4Y8fXs8hdUK05SEfNC+/Pk41Fg==";

            //解密
            string y = Decrypt(enStr, k);

        }