Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RSA像C#Dot Net一样加密_Java_Encryption_Rsa - Fatal编程技术网

Java RSA像C#Dot Net一样加密

Java RSA像C#Dot Net一样加密,java,encryption,rsa,Java,Encryption,Rsa,我在C#程序(我在下面提到)中使用了RSA非对称密钥加密算法,我必须通过java程序加密数据。我希望我的java程序生成与C#程序相同的加密密钥 公钥: <RSAKeyValue> <Modulus>zgfXY1oUe4nyndX4qtobP1BMxtJ1/rfKU5csdAcWrSVu6ZaEAX3rL3cWnaSLzX4E1BNjSP9pjge6TH7UoaWqOQ==</Modulus> <Exponent>AQAB</Exponent

我在C#程序(我在下面提到)中使用了RSA非对称密钥加密算法,我必须通过java程序加密数据。我希望我的java程序生成与C#程序相同的加密密钥

公钥:

<RSAKeyValue>
<Modulus>zgfXY1oUe4nyndX4qtobP1BMxtJ1/rfKU5csdAcWrSVu6ZaEAX3rL3cWnaSLzX4E1BNjSP9pjge6TH7UoaWqOQ==</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

 rsa.FromXmlString(PublicKey); // read public key XML defined above

byte[] buffer = rsa.Encrypt(Encoding.UTF8.GetBytes(strToEncrypt), false);

string encryptedStr = HttpUtility.UrlEncode(buffer);// byteConverterGetString;
byte[] modulusBytes = Base64.decode("zgfXY1oUe4nyndX4qtobP1BMxtJ1/rfKU5csdAcWrSVu6ZaEAX3rL3cWnaSLzX4E1BNjSP9pjge6TH7UoaWqOQ==");
byte[] exponentBytes = Base64.decode("AQAB");

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/ECB/PKCS1Padding");

cipher.init(Cipher.ENCRYPT_MODE, pubKey);

byte[] plainBytes = new String("Admin123").getBytes("UTF-8");

byte[] cipherData = cipher.doFinal( plainBytes );

String encryptedString = Base64.encodeBytes(cipherData);

System.out.println(URLEncoder.encode(encryptedString));
Java加密程序:

<RSAKeyValue>
<Modulus>zgfXY1oUe4nyndX4qtobP1BMxtJ1/rfKU5csdAcWrSVu6ZaEAX3rL3cWnaSLzX4E1BNjSP9pjge6TH7UoaWqOQ==</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

 rsa.FromXmlString(PublicKey); // read public key XML defined above

byte[] buffer = rsa.Encrypt(Encoding.UTF8.GetBytes(strToEncrypt), false);

string encryptedStr = HttpUtility.UrlEncode(buffer);// byteConverterGetString;
byte[] modulusBytes = Base64.decode("zgfXY1oUe4nyndX4qtobP1BMxtJ1/rfKU5csdAcWrSVu6ZaEAX3rL3cWnaSLzX4E1BNjSP9pjge6TH7UoaWqOQ==");
byte[] exponentBytes = Base64.decode("AQAB");

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/ECB/PKCS1Padding");

cipher.init(Cipher.ENCRYPT_MODE, pubKey);

byte[] plainBytes = new String("Admin123").getBytes("UTF-8");

byte[] cipherData = cipher.doFinal( plainBytes );

String encryptedString = Base64.encodeBytes(cipherData);

System.out.println(URLEncoder.encode(encryptedString));
我试过上面的java程序,但结果如下:

o%2Bgw7%2BXhYxA9ltDV5zERsF4DyXgMTc%2Fgx82wRtT1xfR3suY0XBJLadp7bXjmSX7CplDVdoQyH05Jpqgkd%2B1G4A%3D%3D

而C#程序生成的

%23E%03%c2%10)%40E%bf%7b%f9%11%87c0%12q%b9w%ba%2c%98%b4%b1%96%bc%ee%c5%c9t%1e%e71%85%b68t%00%3a%b7%d9%fb%a1%18%ba%10%b4%c3c%e1'*%3b%f6D%e2%cc6%82%f2%a6

那么有谁能帮我纠正我的java程序。。
谢谢

AFAIR PKCS1在Java mean v1.5中添加,而.Net使用OAEP(PKCS#1 v2.0)。因此,您使用的是完全不同的填充算法。顺便说一句:url编码形式中的十六进制是不好读取的,因为长度可变。使用更好的十六进制或base64。我需要使用哪种填充算法使其完美?我使用了org.apache.commons.codec.binary.Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.decodeBase64(cipherData)),但它不起作用。你能帮我吗!!!!!在Java中添加AFAIR PKCS1P意味着1.5版,而.Net使用OAEP(PKCS#1 v2.0)。因此,您使用的是完全不同的填充算法。顺便说一句:url编码形式中的十六进制是不好读取的,因为长度可变。使用更好的十六进制或base64。我需要使用哪种填充算法使其完美?我使用了org.apache.commons.codec.binary.Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.decodeBase64(cipherData)),但它不起作用。你能帮我吗!!!!!