In C#, the XML form is loaded through the FromXmlString attribute, while in JAVA, the parsed PEM format string is used. In short, reading the information in the certificate is nothing more than a conversion method.
/// <summary> /// c# Use java's public key to encrypt data utf8 encoding by parsing public keys /// </summary> /// <param name="publickey"></param> /// <returns></returns> public static string RSAEncrypts(string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; X509Certificate2 x509Certificate2 = new X509Certificate2("D:\\Config\\"); //Create and return the xml string representation of the current algorithm object string publicKeyString = (false); (publicKeyString); //(RSAPublicKeyJava2DotNet(publicKeyString)); cipherbytes = (Encoding.(content), false); return Convert.ToBase64String(cipherbytes); } /// <summary> /// Private key decryption parsing data by parsing the private key This attribute is very important 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); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; X509Certificate2 x509Certificate2 = new X509Certificate2("D:\\Config\\", "cfca1234", ); //Create and return the xml string representation of the current algorithm object //(RSAPrivateKeyJava2DotNet()); ((true)); cipherbytes = (Convert.FromBase64String(content), false); return Encoding.(cipherbytes); }
/******************The following is how to convert public and private keys provided by java to .net public and private key XML*********************************************/ /// <summary> /// RSA public key format conversion, java->.net/// </summary> /// <param name="publicKey">java generated public key</param>/// <returns></returns> public static string RSAPublicKeyJava2DotNet(string publicKey) { RsaKeyParameters publicKeyParam = (RsaKeyParameters)(Convert.FromBase64String(publicKey)); return ("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", Convert.ToBase64String(()), Convert.ToBase64String(())); } /// <summary> /// RSA private key format conversion, java->.net/// </summary> /// <param name="privateKey">Java generated RSA private key</param>/// <returns></returns> public static string RSAPrivateKeyJava2DotNet(string privateKey) { RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)(Convert.FromBase64String(privateKey)); return ("<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(()), Convert.ToBase64String(()), Convert.ToBase64String(()), Convert.ToBase64String(()), Convert.ToBase64String(()), Convert.ToBase64String(()), Convert.ToBase64String(()), Convert.ToBase64String(())); }
You can test the above examples, thank you for your support.