简体   繁体   English

Java RSA加密 - 解密.NET

[英]Java RSA Encrypt - Decrypt .NET

I am encrypting on JAVA using RSA and attempting to decrypt using .NET. 我正在使用RSA加密JAVA并尝试使用.NET解密。 I am including my JAVA code and .NET code in hopes that someone has some experience with this sort of thing. 我包含了我的JAVA代码和.NET代码,希望有人有这方面的经验。

JAVA Code: JAVA代码:

byte[] modulusBytes = Base64.decode("xTSiS4+I/x9awUXcF66Ffw7tracsQfGCn6g6k/hGkLquHYMFTCYk4mOB5NwLwqczwvl8HkQfDShGcvrm47XHKUzA8iadWdA5n4toBECzRxiCWCHm1KEg59LUD3fxTG5ogGiNxDj9wSguCIzFdUxBYq5ot2J4iLgGu0qShml5vwk=");
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");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);

byte[] plainBytes = new String("big kitty dancing").getBytes("UTF-8");
byte[] cipherData = cipher.doFinal( plainBytes );
String encryptedString = Base64.encodeBytes(cipherData);

From this JAVA Code I take the results of the encryptedString which happen to be: 从这个JAVA代码我得到的encryptedString的结果恰好是:

FoP4+AAIH6hcabXnrvNG5YUk/nBv9n9HU0CAgZjkIWQIDjbOpSwoPVBFERrZ6641x2QaoJw5yv18XAay+0WrCaSw4sveRX+hmPm5qeVUPcjoR4slsVZ/hBFJtAHj9tva4hOugWDZa9s3RVJlxkNfE+u+Kt/YKLOi2EYbH05HjeM= FoP4 + AAIH6hcabXnrvNG5YUk / nBv9n9HU0CAgZjkIWQIDjbOpSwoPVBFERrZ6641x2QaoJw5yv18XAay + 0WrCaSw4sveRX + hmPm5qeVUPcjoR4slsVZ / hBFJtAHj9tva4hOugWDZa9s3RVJlxkNfE + U +的Kt / YKLOi2EYbH05HjeM =

And attempt to decrypt using the following .NET code 并尝试使用以下.NET代码进行解密

const int PROVIDER_RSA_FULL = 1;
const string CONTAINER_NAME = "Tracker";

CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider(cspParams);
rsa1.FromXmlString("<RSAKeyValue><Modulus>xTSiS4+I/x9awUXcF66Ffw7tracsQfGCn6g6k/hGkLquHYMFTCYk4mOB5NwLwqczwvl8HkQfDShGcvrm47XHKUzA8iadWdA5n4toBECzRxiCWCHm1KEg59LUD3fxTG5ogGiNxDj9wSguCIzFdUxBYq5ot2J4iLgGu0qShml5vwk=</Modulus><Exponent>AQAB</Exponent><P>+lXMCEwIN/7+eMpBrq87kQppxu3jJBTwztGTfXNaPUTx+A6uqRwug5oHBbSpYXKNDNCBzVm/0VxB3bo4FJx+ZQ==</P><Q>yasOGaJaE9xlF9T2xRuKeG9ZxCiyjhYaYB/mbtL+SIbtkRLi/AxaU4g2Il/UxhxhSXArKxIzV28zktispPJx1Q==</Q><DP>ueRgQIEFUV+fY979a1RgrVHIPpqEI1URhOMH3Q59oiXCcOumM5njyIHmWQxRAzXnG+7xlKXi1PrnRll0L4oOKQ==</DP><DQ>dfEMNgG1HJhwpxdtmqkYuoakwQvsIRzcIAuIAJh1DoWaupWJGk8/JEstHb1d+t7uJrzrAi2KyT/HscH2diE0YQ==</DQ><InverseQ>YoYF9PF6FiC0YngVeaC/eqt/ea8wMYNN3YO1LuzWpcy2exPRj2U0ZbWMvHXMUb4ea2qmhZGx1QlK4ULAuWKpXQ==</InverseQ><D>g1WAWI4pEK9TA7CA2Yyy/2FzzNiu0uQCuE2TZYRNiomo96KQXpxwqAzZLw+VDXfJMypwDMAVZe/SqzSJnFEtZxjdxaEo3VLcZ1mnbIL0vS7D6iFeYutF9kF231165qGd3k2tgymNMMpY7oYKjS11Y6JqWDU0WE5hjS2X35iG6mE=</D></RSAKeyValue>");

string data2Decrypt =    "FoP4+AAIH6hcabXnrvNG5YUk/nBv9n9HU0CAgZjkIWQIDjbOpSwoPVBFERrZ6641x2QaoJw5yv18XAay+0WrCaSw4sveRX+hmPm5qeVUPcjoR4slsVZ/hBFJtAHj9tva4hOugWDZa9s3RVJlxkNfE+u+Kt/YKLOi2EYbH05HjeM=";

byte[] encyrptedBytes = Convert.FromBase64String(data2Decrypt);  

byte[] plain = rsa1.Decrypt(encyrptedBytes, false);
string decryptedString = System.Text.Encoding.UTF8.GetString(plain);

On the Java side, you need to use "RSA/ECB/PKCS1Padding" as algorithm name. 在Java端,您需要使用"RSA/ECB/PKCS1Padding"作为算法名称。 With the plain "RSA" name, Java does not add the PKCS#1 padding which the C# implementation expects, hence the "bad data". 使用简单的"RSA"名称,Java不会添加C#实现所期望的PKCS#1填充,因此是“坏数据”。

The padding is a transformation of the input data (your encoded string) into a somewhat larger string which has the same length than the RSA modulus (128 bytes here). 填充是将输入数据(您的编码字符串)转换为稍大的字符串,其长度与RSA模数相同(此处为128字节)。 It is important for security and injects some randomness (the same input string will not yield the same encrypted string every time; but the decryption process removes that randomness and recovers the right string nonetheless). 它对于安全性很重要并且会注入一些随机性(相同的输入字符串不会每次产生相同的加密字符串;但解密过程会删除该随机性并恢复正确的字符串)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM