Today survey of C # RSA and Java RSA, many people on the web said that C # encryption or Java encryption, Java can not decrypt or C # cannot decrypt
But I tried it, found that it is possible, the following is the code I tried, if you have any problem, I want to see, why they can not exchange?
Rsamain Code
Package RSA;
Import Java.math.BigInteger;
Import Java.security.KeyFactory;
Import Java.security.PrivateKey;
Import Java.security.PublicKey;
Import Java.security.spec.RSAPrivateKeySpec;
Import Java.security.spec.RSAPublicKeySpec;
Import Javax.crypto.Cipher;
Import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
Import com.sun.org.apache.xml.internal.security.utils.Base64;
/**
* @author QH
* Jan 6, 2015
*/
public class Rsamain {
private static String module = "5m9m14xh3oqlj8bngw9e4rgpxpcktv9mskhsvfvmjhbfv+ sj5v0ubqqxa5yjln4vc49z7svju8s0x4gz6azztn06jzwogyprv54q4i0dcyadww4ze3e+botwgvu1og3qhkn8vygoj40j6u85z/ Ptju3hn1m75zr195ju7g9v4hk= ";
private static String exponentstring = "Aqab";
private static String delement = "vmayhebpagojvaexql+t8dqkft1fudeysty31ltyxjgu6xiltxxhuuzaa2ipyhgbz0nd7znwsw/ S44iql0fen1kzkioel3svanui63o3o5xddeexvm6zof1wuuh/oldovpwechyoadmtuzgvcbjk1sydjf++nr0fenw1rb1xg30= ";
private static String encryptstring = "vx/dgjs1ywkrubsodgishiwlgqyne2z/em65u7hzx+ rogwaizimnbxjuos6acehkzx66cmyead1fc6oewbevdifp44gan9dcjke/bkkqlweg6ato5q+yqy+nege1kvly9eyxs/kv1ldh3e/ 2xak5fnj8zp6ou2kq4ewl8kk/ai4= ";
/**
* @param args
*/
public static void Main (string[] args) {
byte[] en = Encrypt ();
System.out.println (Base64.encode (en));
byte[] entest = null;
try {
Entest = Base64.decode (encryptstring);
} catch (Base64decodingexception e) {
E.printstacktrace ();
}
System.out.println (entest.length);
System.out.println (en.length);
System.out.println (New String (Dencrypt (en)));
System.out.println (New String (Dencrypt (entest)));
}
public static byte[] Encrypt () {
try {
byte[] modulusbytes = Base64.decode (module);
byte[] exponentbytes = Base64.decode (exponentstring);
BigInteger modulus = new BigInteger (1, modulusbytes);
BigInteger exponent = new BigInteger (1, exponentbytes);
Rsapublickeyspec Rsapubkey = new Rsapublickeyspec (modulus, exponent);
keyfactory fact = keyfactory.getinstance ("RSA");
PublicKey PubKey = Fact.generatepublic (Rsapubkey);
Cipher Cipher = cipher.getinstance ("RSA");
Cipher.init (Cipher.encrypt_mode, PubKey);
byte[] CipherData = cipher.dofinal (New String ("Chenhailong"). GetBytes ());
return cipherdata;
} catch (Exception e) {
E.printstacktrace ();
}
return null;
}
public static byte[] Dencrypt (byte[] encrypted) {
try {
byte[] expbytes = Base64.decode (delement);
byte[] modbytes = Base64.decode (module);
BigInteger modules = new BigInteger (1, modbytes);
BigInteger exponent = new BigInteger (1, expbytes);
Keyfactory factory = keyfactory.getinstance ("RSA");
Cipher Cipher = cipher.getinstance ("RSA");
Rsaprivatekeyspec Privspec = new Rsaprivatekeyspec (modules, exponent);
Privatekey Privkey = factory.generateprivate (Privspec);
Cipher.init (Cipher.decrypt_mode, Privkey);
byte[] decrypted = cipher.dofinal (encrypted);
return decrypted;
} catch (Exception e) {
E.printstacktrace ();
}
return null;
}
}
C # code
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Security.Cryptography;
Namespace RSA
{
Class Program
{
static void Main (string[] args)
{
String de = "ibilupjfgpmxgpbgn3f2jjd6xjcqrsapjvvbvbbebdv21pjj7ltrfhejsvnjx/mvozrmx0lxsvoxtmvvvwvf7k7w5hs7qo+ Amn96ywke7wilem9m4ppz60a/ksckskiona67txcqolxb8n18tkanckhv0ce+gyekk5+mt7e1vao= ";
String encrypt = Rsaencrypt ("", "Chenhailong");
Byte[] Encrypt = Rsaencrypt ("Chenhailong");
String name = Rsadecrypt (encrypt);
String name = Rsadecrypt (convert.frombase64string (DE));
Console.WriteLine (Encrypt. Length);
Console.WriteLine (Convert.tobase64string (Encrypt));
Console.WriteLine (name);
Console.readkey ();
}
<summary>
RSA Encrypt
</summary>
<param name= "PublicKey" ></param>
<param name= "Content" ></param>
<returns></returns>
public static byte[] Rsaencrypt (string content)
{
String publickey = @ "<rsakeyvalue><modulus>5m9m14xh3oqlj8bngw9e4rgpxpcktv9mskhsvfvmjhbfv+ sj5v0ubqqxa5yjln4vc49z7svju8s0x4gz6azztn06jzwogyprv54q4i0dcyadww4ze3e+botwgvu1og3qhkn8vygoj40j6u85z/ Ptju3hn1m75zr195ju7g9v4hk=</modulus><exponent>aqab</exponent></rsakeyvalue> ";
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider ();
Byte[] cipherbytes;
Rsa. Fromxmlstring (PublicKey);
Cipherbytes = RSA. Encrypt (Encoding.UTF8.GetBytes (content), false);
Return convert.tobase64string (cipherbytes);
return cipherbytes;
}
<summary>
RSA Decrypt
</summary>
<param name= "Privatekey" ></param>
<param name= "Content" ></param>
<returns></returns>
public static string Rsadecrypt (byte[] content)
{
string privatekey = @ "<RSAKeyValue>< modulus>5m9m14xh3oqlj8bngw9e4rgpxpcktv9mskhsvfvmjhbfv+ sj5v0ubqqxa5yjln4vc49z7svju8s0x4gz6azztn06jzwogyprv54q4i0dcyadww4ze3e+botwgvu1og3qhkn8vygoj40j6u85z/ ptju3hn1m75zr195ju7g9v4hk=</modulus><exponent>aqab</exponent><p>/ hf2dnk7rnfl3lbqghwcpfdu778hupiebixcdl5wibtpkzdpsw90aermhjyaw2rgvgri6zsftlh00khspcnumw==</p><q>6cn/ Jolrpapdtep1fkq+uz++1do0eex7hyqi9ry29cqshzcei7leyooswyuaj3xa/ducdqenpsoj9kfbo4wsow==</q><dp> Ga1rhijro8e/yhxjrkyo/nqc5icqghrpmnlpkd9n3cjzvpoiskwf7fzuhezdanejfkzhcza21z24ag3rko5qnw==</dp><dq> mngscb8rylmsrz2ek2pyqwo7h/szt8y5ilo9wu08dwnot/7umioeqfdwsty3w5xqqhnvc9wfycfp4h4qbissyw==</dq>< inverseq>eg02s7sadhh1evt9dd0z62y0uy7giyvxx/uq+izkscwb8m2g7qv9xgzqaqllpcaekbux3y59hhm+kpamgl19kg==</ inverseq><d>vmayhebpagojvaexql+t8dqkft1fudeysty31ltyxjgu6xiltxxhuuzaa2ipyhgbz0nd7znwsw/ S44iql0fen1kzkiOel3svanui63o3o5xddeexvm6zof1wuuh/oldovpwechyoadmtuzgvcbjk1sydjf++nr0fenw1rb1xg30=</d></rsakeyvalue > ";
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider ();
Byte[] cipherbytes;
Rsa. Fromxmlstring (Privatekey);
Cipherbytes = RSA. Decrypt (content, false);
Return Encoding.UTF8.GetString (cipherbytes);
}
}
}
C # RSA and Java RSA Interoperability