1, first with.net generate XML format of public key and secret key
///& lt; Summary>
///generate XML format in a given path of private and public keys,
///& lt;/summary>
Public void GenerateKeys (string path)
{
Using (var rsa=new RSACryptoServiceProvider (RsaKeySize))
{
Try
{
//access to private and public keys,
Var publicKey=rsa. ToXmlString (false);
Var privateKey=rsa. ToXmlString (true);
//save to disk
File. WriteAllText (Path.Com bine (path, publicKeyFileName), publicKey);
File. WriteAllText (Path.Com bine (path, privateKeyFileName), privateKey);
MessageBox. Show (the string. Format (" generation of RSA key path: {0} \ \ [{1}, {2}] ", path, publicKeyFileName, privateKeyFileName));
}
The finally
{
Rsa. PersistKeyInCsp=false;
}
}
}
2, the XML with BouncyCastle of public key and the key switch to PEM format
///& lt; Summary>
///RSA private key format conversion,. Net - & gt; Java
///& lt;/summary>
///& lt; Param name="privateKey & gt;" The.net generated private key & lt;/param>
///& lt; 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]. The InnerText));
BigInteger exp=new BigInteger (1, Convert FromBase64String (doc. DocumentElement. GetElementsByTagName (" Exponent ") [0]. The InnerText));
BigInteger d=new BigInteger (1, Convert FromBase64String (doc. DocumentElement. GetElementsByTagName (" d ") [0]. The InnerText));
BigInteger p=new BigInteger (1, Convert FromBase64String (doc. DocumentElement. GetElementsByTagName (" p ") [0]. The InnerText));
BigInteger q=new BigInteger (1, Convert FromBase64String (doc. DocumentElement. GetElementsByTagName (" q ") [0]. The InnerText));
BigInteger dp=new BigInteger (1, Convert FromBase64String (doc. DocumentElement. GetElementsByTagName (" dp ") [0]. The InnerText));
BigInteger dq=new BigInteger (1, Convert FromBase64String (doc. DocumentElement. GetElementsByTagName (" dq ") [0]. The InnerText));
BigInteger qinv=new BigInteger (1, Convert FromBase64String (doc. DocumentElement. GetElementsByTagName (" InverseQ ") [0]. The InnerText));
Exp RsaPrivateCrtKeyParameters privateKeyParam=new RsaPrivateCrtKeyParameters (m, d, p, q, dp, dq, qinv);
PrivateKeyInfo PrivateKeyInfo=PrivateKeyInfoFactory. CreatePrivateKeyInfo (privateKeyParam);
Byte [] serializedPrivateBytes=privateKeyInfo. ToAsn1Object () GetEncoded ();
Return the Convert. ToBase64String (serializedPrivateBytes);
}
///& lt; Summary>
///RSA public key format conversion, the.net - & gt; Java
///& lt;/summary>
///& lt; Param name="publicKey & gt;" The.net the generated public key & lt;/param>
///& lt; 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]. The InnerText));
BigInteger p=new BigInteger (1, Convert FromBase64String (doc. DocumentElement. GetElementsByTagName (" Exponent ") [0]. The InnerText));
RsaKeyParameters pub=new RsaKeyParameters (false, m, p);
SubjectPublicKeyInfo publicKeyInfo=SubjectPublicKeyInfoFactory. CreateSubjectPublicKeyInfo (pub);
Byte [] serializedPublicBytes=publicKeyInfo. ToAsn1Object () GetDerEncoded ();
Return the Convert. ToBase64String (serializedPublicBytes);
}
3, then the Java on how to take advantage of me into PEM format public key for encryption and decryption, make with. NET?