C# Ras generates. Net public key and private key and converts. Net public key and private key to Java public key and private key classes

Time:2021-10-25

When we use RSA encryption, we often need the public key and private key, but sometimes only the public key and private key of Java Base64; Friends in need can refer to it

C # commonly used encryption algorithms, refer to the articleC # commonly used encryption algorithms encryptionhelper classes: 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
{
    /// 
    ///C# Ras generates. Net public key and private key and converts. Net public key and private key to Java public key and private key classes
    /// 
    public class RASKeyConversion
    {
        /// 
        ///First: c# private key; Second: c# public key; Third: Java private key; Fourth: Java public key
        /// 
        private static string[] sKeys = new String[4];

        /// 
        ///Method for generating public key and private key
        /// 
        ///First: c# private key; Second: c# public key; Third: Java private key; Fourth: Java public key
        public static string[] createPulbicKey()
        {
            try
            {
                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
                {
                    //C# private key
                    sKeys[0] = rsa.ToXmlString(true);
                    //C# public key
                    sKeys[1] = rsa.ToXmlString(false);
                    //Java private key
                    sKeys[2] = RSAPrivateKeyDotNet2Java(sKeys[0]);
                    //Java public key
                    sKeys[3] = RSAPublicKeyDotNet2Java(sKeys[1]);
                    return sKeys;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        ///     
        ///RSA private key format conversion,. Net - > java    
        ///     
        ///. net generated private key    
        ///     
        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);
        }

        ///     
        ///RSA public key format conversion,. Net - > java    
        ///     
        ///. net generated public key    
        ///     
        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);
        }
    }
}

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]