• Java RSA公钥、私钥与C#RSA公钥、私钥转换


    最近公司项目需要对接第四方支付接口,但是对方的demo是Java的,给的算法是Java版本的公钥和私钥,如果要做RSA加密就需要将Java的公钥和私钥转换成dotnet版本的公钥和私钥,具体的算法如下:

     1         /// <summary>  
     2         /// RSA私钥格式转换,java->.net  
     3         /// </summary>  
     4         /// <param name="privateKey">java生成的RSA私钥</param>  
     5         /// <returns></returns>  
     6         public static string RSAPrivateKeyJava2DotNet(string privateKey)
     7         {
     8             RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
     9             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>",
    10                 Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
    11                 Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
    12                 Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
    13                 Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
    14                 Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
    15                 Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
    16                 Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
    17                 Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
    18         }
    19 
    20         /// <summary>  
    21         /// RSA私钥格式转换,.net->java  
    22         /// </summary>  
    23         /// <param name="privateKey">.net生成的私钥</param>  
    24         /// <returns></returns>  
    25         public static string RSAPrivateKeyDotNet2Java(string privateKey)
    26         {
    27             XmlDocument doc = new XmlDocument();
    28             doc.LoadXml(privateKey);
    29             BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
    30             BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
    31             BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
    32             BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
    33             BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
    34             BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
    35             BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
    36             BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));
    37 
    38             RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);
    39 
    40             PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);
    41             byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
    42             return Convert.ToBase64String(serializedPrivateBytes);
    43         }
    44 
    45         /// <summary>  
    46         /// RSA公钥格式转换,java->.net  
    47         /// </summary>  
    48         /// <param name="publicKey">java生成的公钥</param>  
    49         /// <returns></returns>  
    50         public static string RSAPublicKeyJava2DotNet(string publicKey)
    51         {
    52             RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
    53             return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
    54                 Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
    55                 Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
    56         }
    57 
    58         /// <summary>  
    59         /// RSA公钥格式转换,.net->java  
    60         /// </summary>  
    61         /// <param name="publicKey">.net生成的公钥</param>  
    62         /// <returns></returns>  
    63         public static string RSAPublicKeyDotNet2Java(string publicKey)
    64         {
    65             XmlDocument doc = new XmlDocument();
    66             doc.LoadXml(publicKey);
    67             BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
    68             BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
    69             RsaKeyParameters pub = new RsaKeyParameters(false, m, p);
    70 
    71             SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
    72             byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
    73             return Convert.ToBase64String(serializedPublicBytes);
    74         }
  • 相关阅读:
    转Asktom:Pipelined Functions
    转:PVE法师附魔
    附魔300375图纸掉落大全
    转:Oracle直方图详解
    转:JSON的序列化及GET异步调用.
    转:ORACLE 中dbms_stats的使用
    jQuery中$.each的用法
    魔兽世界天赋详解之 法师篇 一冰法
    Miley's Oracle讲堂第三课:如何在Oracle中使用对象表存储数据.
    台服体验之急速升级
  • 原文地址:https://www.cnblogs.com/tianyang1027/p/10344955.html
Copyright © 2020-2023  润新知