C # RSA RSA way in docking JAVA

C # by FromXmlString property loaded is in XML, JAVA and used in the string is parsed PEM format, in short, is nothing more than read the certificate information is to converts the problem

        ///  <Summary> 
        ///   C # java using public key encryption rsa utf8 encoding by analyzing the data of public key cryptography
         ///  </ Summary> 
        ///  <param name = "the publickey"> </ param> 
        // /  <Returns> </ Returns> 
        public  static  String RSAEncrypts ( String Content) 
        { 

            the RSACryptoServiceProvider RSA = new new the RSACryptoServiceProvider ();
             byte [] cipherbytes; 
            an X509Certificate2 X509Certificate2 = new new an X509Certificate2 ( " D: \\ \\ dsptest.cer Config " );
             //Creates and returns the current algorithm xml object string representation 
            String publicKeyString = x509Certificate2.PublicKey.Key.ToXmlString ( to false ); 
            rsa.FromXmlString (publicKeyString); 
            // rsa.FromXmlString (RSAPublicKeyJava2DotNet (publicKeyString)); 
            cipherbytes = rsa.Encrypt (Encoding.UTF8.GetBytes (Content), to false );
             return Convert.ToBase64String (cipherbytes);    
        } 

            ///  <Summary> 
            /// private key to decrypt the private data parsed by parsing X509KeyStorageFlags.Exportable this property is critical is to obtain private key information
             ///  </ Summary> 
            ///  <param name = "PrivateKey"> </ param> 
            ///  <param name = "Content"> </ param>
            /// <Returns> </ Returns> 
         public  static  String RSADecrypt ( String Content) 
        { 
            byte [] = RGB Convert.FromBase64String (Content); 
            the RSACryptoServiceProvider RSA = new new the RSACryptoServiceProvider ();
             byte [] cipherbytes; 
            an X509Certificate2 X509Certificate2 = new new an X509Certificate2 ( " D: test.pfx Config \\ \\ " , " cfca1234 " , X509KeyStorageFlags.Exportable);
             // create an object and returns the current algorithm xml string representation
             //rsa.FromXmlString(RSAPrivateKeyJava2DotNet(Const.privatekey));

            rsa.FromXmlString(x509Certificate2.PrivateKey.ToXmlString(true));
            cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
            return Encoding.UTF8.GetString(cipherbytes);            
        }

 

/ * ************** the public and private key are provided java .net turn into public and private key way ****************** XML ************************************************** * / 
///  <Summary>  
/// the RSA public key format conversion, Java -.> NET
 ///  </ Summary> 
///  <param name = "publicKey"> public java generated </ 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())); }

 

Guess you like

Origin www.cnblogs.com/weihengblogs/p/11442014.html