zoukankan      html  css  js  c++  java
  • C# RAS生成.NET公钥与私钥以及.NET公钥与私钥转Java公钥私钥类

    在我们使用RSA加密的时候,往往会需要公钥以及私钥,但是有时只有java的Base64的公钥和私钥;有需要的朋友可以参考下

    C# 常用的加密算法,参考文章C#常用的加密算法EncryptionHelper类:MD5、Base64、SHA1、SHA256、HmacSHA256、DES、AES、RSA

    using Org.BouncyCastle.Asn1.Pkcs;
    using Org.BouncyCastle.Asn1.X509;
    using Org.BouncyCastle.Crypto.Parameters;
    using Org.BouncyCastle.Math;
    using Org.BouncyCastle.Pkcs;
    using Org.BouncyCastle.X509;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace ConsoleApp3
    {
        /// <summary>
        /// C# RAS生成.NET公钥与私钥以及.NET公钥与私钥转Java公钥私钥类
        /// </summary>
        public class RASKeyConversion
        {
            /// <summary>
            /// 第一个:C# 私钥;第二个:C# 公钥;第三个:JAVA私钥;第四个:JAVA公钥
            /// </summary>
            private static string[] sKeys = new String[4];
    
            /// <summary>
            /// 生成公钥与私钥方法
            /// </summary>
            /// <returns>第一个:C# 私钥;第二个:C# 公钥;第三个:JAVA私钥;第四个:JAVA公钥</returns>
            public static string[] createPulbicKey()
            {
                try
                {
                    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
                    {
                        //C# 私钥
                        sKeys[0] = rsa.ToXmlString(true);
                        //C# 公钥
                        sKeys[1] = rsa.ToXmlString(false);
                        //JAVA私钥
                        sKeys[2] = RSAPrivateKeyDotNet2Java(sKeys[0]);
                        //JAVA公钥
                        sKeys[3] = RSAPublicKeyDotNet2Java(sKeys[1]);
                        return sKeys;
                    }
                }
                catch (Exception)
                {
                    return null;
                }
            }
    
            /// <summary>    
            /// RSA私钥格式转换,.net->java    
            /// </summary>    
            /// <param name="privateKey">.net生成的私钥</param>    
            /// <returns></returns>    
            public static string RSAPrivateKeyDotNet2Java(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公钥格式转换,.net->java    
            /// </summary>    
            /// <param name="publicKey">.net生成的公钥</param>    
            /// <returns></returns>    
            public static string RSAPublicKeyDotNet2Java(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);
            }
        }
    }
    

    本文来自博客园,作者:TomLucas,转载请注明原文链接:https://www.cnblogs.com/lucasDC/p/15046796.html

  • 相关阅读:
    使用webdriver出现的问题:[18796:1808:0730/131103.313:ERROR:install_util.cc(600)] Failed to read HKLMSOFTWAREPoliciesGoogleChromeMachineLevelUserCloudPolicyEnrollmentToken: 系统找不到指定的文件。 (0x2) DevTools lis
    1130 host is not allowed to connect to
    Mysql误删了root用户怎么办
    猫眼电影爬取
    sklearn之聚类的均值漂移算法
    sklearn之聚类K均值算法
    基于支持向量机的分类模型和回归模型案例
    超参数处理之网格搜素
    redis基础之有序集合应用
    Sklearn之支持向量机分类
  • 原文地址:https://www.cnblogs.com/lucasDC/p/15046796.html
Copyright © 2011-2022 走看看