欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#RSA对接JAVA中RSA方式代码实例

程序员文章站 2022-06-15 17:39:10
c#中通过fromxmlstring属性加载的是xml形式,而java中用到的是解析后的pem格式的字符串,总之读取证书中信息无非是转换方式问题 /// <...

c#中通过fromxmlstring属性加载的是xml形式,而java中用到的是解析后的pem格式的字符串,总之读取证书中信息无非是转换方式问题

/// <summary>
    /// c# 使用 java 的公钥进行rsa加密 utf8编码  通过解析公钥加密数据
    /// </summary>
    /// <param name="publickey"></param>
    /// <returns></returns>
    public static string rsaencrypts(string content)
    {

      rsacryptoserviceprovider rsa = new rsacryptoserviceprovider();
      byte[] cipherbytes;
      x509certificate2 x509certificate2 = new x509certificate2("d:\\config\\dsptest.cer");
      //创建并返回当前算法对象的xml字符串表示形式
      string publickeystring = x509certificate2.publickey.key.toxmlstring(false);
      rsa.fromxmlstring(publickeystring);
      //rsa.fromxmlstring(rsapublickeyjava2dotnet(publickeystring));
      cipherbytes = rsa.encrypt(encoding.utf8.getbytes(content), false);
      return convert.tobase64string(cipherbytes);  
    }

      /// <summary>
      /// 私钥解密 通过解析私钥来解析数据 x509keystorageflags.exportable这个属性很关键是获取私钥信息的
      /// </summary>
      /// <param name="privatekey"></param>
      /// <param name="content"></param>
      /// <returns></returns>
     public static string rsadecrypt( string content)
    {
      byte[] rgb = convert.frombase64string(content);
      rsacryptoserviceprovider rsa = new rsacryptoserviceprovider();
      byte[] cipherbytes;
      x509certificate2 x509certificate2 = new x509certificate2("d:\\config\\test.pfx", "cfca1234", x509keystorageflags.exportable);
      //创建并返回当前算法对象的xml字符串表示形式
      //rsa.fromxmlstring(rsaprivatekeyjava2dotnet(const.privatekey));

      rsa.fromxmlstring(x509certificate2.privatekey.toxmlstring(true));
      cipherbytes = rsa.decrypt(convert.frombase64string(content), false);
      return encoding.utf8.getstring(cipherbytes);      
    }
/***************以下是java提供的公私钥转成.net公私钥xml的方式*************************************/
/// <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()));
}

/// <summary>
/// rsa私钥格式转换,java->.net
/// </summary>
/// <param name="privatekey">java生成的rsa私钥</param>
/// <returns></returns>
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()));

}

以上实例大家可以测试下,感谢大家对的支持。