Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of RSA Mode Code in C#RSA docking JAVA

Shulou Source: shulou.com Published: 2022-06-03 06:13:46 09月28日 Update

This article introduces the example analysis of RSA code in C#RSA docking JAVA, the content is very detailed, interested friends can refer to it, hope to be helpful to you.

What is loaded in C # through the FromXmlString attribute is XML, while in JAVA, the parsed string in PEM format is used. In short, reading the information in the certificate is nothing more than a matter of conversion.

/ c # rsa encryption using the public key of java utf8 encoding encrypted data by parsing the public key / public static string RSAEncrypts (string content) {RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (); byte [] cipherbytes; X509Certificate2 x509Certificate2 = new X509Certificate2 ("D:\\ Config\\ dsptest.cer"); / / create and return the xml string representation of the current algorithm object string publicKeyString = x509Certificate2.PublicKey.Key.ToXmlString (false) Rsa.FromXmlString (publicKeyString); / / rsa.FromXmlString (RSAPublicKeyJava2DotNet (publicKeyString)); cipherbytes = rsa.Encrypt (Encoding.UTF8.GetBytes (content), false); return Convert.ToBase64String (cipherbytes);} / the private key decryption parses the data X509KeyStorageFlags.Exportable by parsing the private key. The key attribute is to obtain the private key information of / public static string RSADecrypt (string content) {byte [] rgb = Convert.FromBase64String (content). RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (); byte [] cipherbytes; X509Certificate2 x509Certificate2 = new X509Certificate2 ("D:\ Config\\ test.pfx", "cfca1234", X509KeyStorageFlags.Exportable); / / create and return the xml string representation of the current algorithm object / / rsa.FromXmlString (RSAPrivateKeyJava2DotNet (Const.privatekey)); rsa.FromXmlString (x509Certificate2.PrivateKey.ToXmlString (true)); cipherbytes = rsa.Decrypt (Convert.FromBase64String (content), false); return Encoding.UTF8.GetString (cipherbytes);}

/ * the following is how the public and private keys provided by java are converted to .net public and private key XML * * / RSA public key format conversion Java- > .net/// java generated public key / public static string RSAPublicKeyJava2DotNet (string publicKey) {RsaKeyParameters publicKeyParam = (RsaKeyParameters) PublicKeyFactory.CreateKey (Convert.FromBase64String (publicKey)) Return string.Format ("{0} {1}", Convert.ToBase64String (publicKeyParam.Modulus.ToByteArrayUnsigned ()), Convert.ToBase64String (publicKeyParam.Exponent.ToByteArrayUnsigned ();} / RSA private key format conversion, java- > .net/// RSA private key generated by java / public static string RSAPrivateKeyJava2DotNet (string privateKey) {RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters) PrivateKeyFactory.CreateKey (Convert.FromBase64String (privateKey)); return string.Format ("{0} {1})

{2}

{3} {4} {5} {6} {7} ", 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 ());}

This is the example analysis of the code of RSA mode in C#RSA docking JAVA. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Methods public keys characters strings forms formats codes examples analysis information public and private content objects attributes data more algorithms encryption help generation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi Linux NVidia MySQL