[英]Java RSA Encrypt
I have used RSA Assymetric Key Encryption Algorithm in C# program (I have mentioned below) and I have to encrypt data through a java program. 我已经在C#程序中使用了RSA密钥加密算法(我在下面提到了),并且必须通过Java程序对数据进行加密。 I want my java program to generate same encrypted key as like result of C# program.
我希望我的Java程序生成与C#程序相同的加密密钥。
Public Key: 公钥:
<RSAKeyValue>
<Modulus>zgfXY1oUe4nyndX4qtobP1BMxtJ1/rfKU5csdAcWrSVu6ZaEAX3rL3cWnaSLzX4E1BNjSP9pjge6TH7UoaWqOQ==</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
C# Encryption Programme: C#加密程序:
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;
Java Encryption Programme: Java加密程序:
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));
I tried above java program but it gives me result like: 我尝试了上面的Java程序,但它给了我这样的结果:
o%2Bgw7%2BXhYxA9ltDV5zERsF4DyXgMTc%2Fgx82wRtT1xfR3suY0XBJLadp7bXjmSX7CplDVdoQyH05Jpqgkd%2B1G4A%3D%3D
0%2Bgw7%2BXhYxA9ltDV5zERsF4DyXgMTc%2Fgx82wRtT1xfR3suY0XBJLadp7bXjmSX7CplDVdoQyH05Jpqgkd%2B1G4A%3D%3D
and C# program generates like 和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%80%f2%a6
%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%80%F2%A6
so can anyone help me to correct my java program.. thanks 所以任何人都可以帮助我更正我的Java程序..谢谢
It seems to me that you're URL encoding two different things: 在我看来,您正在对两种不同的内容进行URL编码:
The result of these two different approaches will not be the same. 这两种不同方法的结果将不同。 Perhaps you should encode
new String( cipherData )
in the Java part - or just compare the two byte[]
arrays before encoding? 也许您应该在Java部分中对
new String( cipherData )
进行编码-或者只是在编码之前比较两个byte[]
数组?
Cheers, 干杯,
You are URLencoding diferent objects, just try this code: 您正在URLencoding不同的对象,只需尝试以下代码:
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 string = new String(cipherData);
System.out.println(URLEncoder.encode(string,"UTF-8"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.