RSA public key and private key pairs generated
1 /// <summary> 2 /// RSA生成公钥和私钥 3 /// </summary> 4 /// <returns></returns> 5 public static string[] GenerateKeys() 6 { 7 try 8 { 9 string[] sKeys = new String[2]; 10 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024); 11 sKeys[0] = rsa.ToXmlString(true);// private key 12 is sKeys [ . 1 ] = rsa.ToXmlString ( to false ); // public key 13 is return sKeys; 14 } 15 the catch (Exception) 16 { . 17 return null ; 18 is } . 19 }
RSA private key format conversion
1 public class RSAKeyConvert 2 { 3 /// <summary> 4 /// RSA私钥格式转换,java->.net 5 /// </summary> 6 /// <param name="privateKey">java生成的RSA私钥</param> 7 /// <returns></returns> 8 public static string RSAPrivateKeyJava2DotNet(string privateKey) 9 { 10 RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey)); 11 12 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>", 13 Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()), 14 Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()), 15 Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()), 16 Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()), 17 Convert.ToBase64String (privateKeyParam.DP.ToByteArrayUnsigned ()), 18 is Convert.ToBase64String (privateKeyParam.DQ.ToByteArrayUnsigned ()), . 19 Convert.ToBase64String (privateKeyParam.QInv.ToByteArrayUnsigned ()), 20 is Convert.ToBase64String (privateKeyParam.Exponent. ToByteArrayUnsigned ())); 21 is } 22 is 23 is /// <Summary> 24 /// the RSA private key format conversion, .NET -> Java 25 /// </ Summary> 26 is /// <param name = "privateKey" > .NET generated private key </ param> 27 /// <Returns> </ Returns> 28 public static string RSAPrivateKeyDotNet2Java(string privateKey) 29 { 30 XmlDocument doc = new XmlDocument(); 31 doc.LoadXml(privateKey); 32 BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText)); 33 BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText)); 34 BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText)); 35 BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText)); 36 BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText)); 37 BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText)); 38 BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText)); 39 BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText)); 40 41 RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv); 42 43 PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam); 44 byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded(); 45 return Convert.ToBase64String(serializedPrivateBytes); 46 } 47 48 /// <summary> 49 /// RSA公钥格式转换,java->.net 50 /// </summary> 51 /// <param name="publicKey">java生成的公钥</param> 52 /// <returns></returns> 53 public static string RSAPublicKeyJava2DotNet(string publicKey) 54 { 55 RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); 56 return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", 57 Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()), 58 Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); 59 } 60 61 /// <summary> 62 /// RSA公钥格式转换,.net->java 63 /// </summary> 64 /// <param name="publicKey">.net生成的公钥</param> 65 /// <returns></returns> 66 public static string RSAPublicKeyDotNet2Java(string publicKey) 67 { 68 XmlDocument doc = new XmlDocument(); 69 doc.LoadXml(publicKey); 70 BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText)); 71 BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText)); 72 RsaKeyParameters pub = new RsaKeyParameters(false, m, p); 73 74 SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub); 75 byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded(); 76 return Convert.ToBase64String(serializedPublicBytes); 77 } 78 }
A private key to encrypt data
. 1 /// <Summary> 2 /// for RSA encryption with a private key to the data . 3 /// </ Summary> . 4 /// <param name = "xmlPrivateKey"> private </ param> . 5 /// < param name = "strEncryptString"> data to be encrypted </ param> . 6 /// <Returns> data (the Base64) encrypted </ Returns> . 7 public static String RSAEncryptByPrivateKey ( String xmlPrivateKey, String strEncryptString) . 8 { . 9 // load private 10 = PrivateRsa the RSACryptoServiceProvider new new the RSACryptoServiceProvider ( 1024 ); . 11 privateRsa.FromXmlString (xmlPrivateKey); 12 is 13 is // conversion key 14 AsymmetricCipherKeyPair KeyPair = DotNetUtilities.GetKeyPair (privateRsa); 15 16 IBufferedCipher C = CipherUtilities.GetCipher ( " the RSA / an ECB / PKCS1Padding " ); // the same parameters in Java cryptographic parameters 17 // first parameter to true to indicate encryption, decryption, false; second parameter key 18 is c.Init ( true , keyPair.Private); 19 20 byte[] DataToEncrypt = Encoding.UTF8.GetBytes(strEncryptString); 21 byte[] outBytes = c.DoFinal(DataToEncrypt);//加密 22 string strBase64 = Convert.ToBase64String(outBytes); 23 24 return strBase64; 25 }
Need to reference BouncyCastle.Crypto.dll