RSA加密.NET的Java解密 [英] RSA .NET encryption Java decryption

查看:226
本文介绍了RSA加密.NET的Java解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用RSA算法来加密在.NET中的字符串和解密结果在Java中。此刻,我已经能够做相反的(加密在Java中,解密.NET)。
在这里,我有我的代码,实际工作(JAVA加密):

 字节[] = modulusBytes Base64.decode( 2rRVVVFJRbH / wAPDtnwZwu + NXU + AZ6uXxh / SW + AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw / FfcM9DsKs / rYR83tBLiIAfgdnVjF27tZID + HJMFTiI30mALjr7 + TFP + 2lIACXA1RIKTk7S9pDmX8 =); 
字节[] = exponentBytes Base64.decode(AQAB);
BigInteger的模量=新的BigInteger(1 modulusBytes);
BigInteger的指数=新的BigInteger(1 exponentBytes);

RSAPublicKeySpec rsaPubKey =新RSAPublicKeySpec(模数,指数);
的KeyFactory其实= KeyFactory.getInstance(RSA);
公钥PUBKEY = fact.generatePublic(rsaPubKey);

密码加密= Cipher.getInstance(RSA / ECB / PKCS1Padding);
cipher.init(Cipher.ENCRYPT_MODE,PUBKEY);

字节[] = plainBytes新的String(大猫咪舞)的getBytes(UTF-8)。
字节[] =的CipherData cipher.doFinal(plainBytes);
字符串encryptedString = Base64.encode(的CipherData);
返回encryptedString;

和(.NET解密)

  const int的PROVIDER_RSA_FULL = 1; 
常量字符串CONTAINER_NAME =跟踪器;

CspParameters cspParams;
cspParams =新CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
的RSACryptoServiceProvider RS​​A1 =新的RSACryptoServiceProvider(cspParams);
rsa1.FromXmlString(< RSAKeyValue><模> 2rRVVVFJRbH / wAPDtnwZwu + NXU + AZ6uXxh / SW + AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw / FfcM9DsKs / rYR83tBLiIAfgdnVjF27tZID + HJMFTiI30mALjr7 + TFP + 2lIACXA1RIKTk7S9pDmX8 =< /模量GT;<指数> AQAB< /指数>< P> + lXMCEwIN / 7 + eMpBrq87kQppxu3jJBTwztGTfXNaPUTx + A6uqRwug5oHBbSpYXKNDNCBzVm / 0VxB3bo4FJx + ZQ ==< / P>< Q> yasOGaJaE9xlF9T2xRuKeG9ZxCiyjhYaYB / mbtL + SIbtkRLi / AxaU4g2Il / UxhxhSXArKxIzV28zktispPJx1Q ==< / Q>< D​​P> ueRgQIEFUV + fY979a1RgrVHIPpqEI1URhOMH3Q59oiXCcOumM5njyIHmWQxRAzXnG + 7xlKXi1PrnRll0L4oOKQ ==下; / DP>&下; DQ> dfEMNgG1HJhwpxdtmqkYuoakwQvsIRzcIAuIAJh1DoWaupWJGk8 / JEstHb1d + t7uJrzrAi2KyT / HscH2diE0YQ ==&下; / DQ>&下; InverseQ> YoYF9PF6FiC0YngVeaC / EQT / ea8wMYNN3YO1LuzWpcy2exPRj2U0ZbWMvHXMUb4ea2qmhZGx1QlK4ULAuWKpXQ ==&下; / InverseQ>&所述d取代; g1WAWI4pEK9TA7CA2Yyy / 2FzzNiu0uQCuE2TZYRNiomo96KQXpxwqAzZLw + VDXfJMypwDMAVZe / SqzSJnFEtZxjdxaEo3VLcZ1mnbIL0vS7D6iFeYutF9kF231165qGd3k2tgymNMMpY7oYKjS11Y6JqWDU0WE5hjS2X35iG6mE = LT; / D>< / RSAKeyValue>);

串data2Decrypt =BaB21vY + RD / jiY3AAsb269fIWTEH38s0xLUfJ7CoVUgaQ6vYzB0tiJ1Ag9HNEdCcuZdGchhqnms8jpsqsHC1iKrz6QCLsgUU7VNWDfQqZYR6Rl / GwR0biK2STnOL + G06F / JUdixHOHOgROify1m8qppYo5plpOVMqYFzEMREMkM =;

字节[] = encyrptedBytes Convert.FromBase64String(da​​ta2Decrypt);

字节[] =平原rsa1.Decrypt(encyrptedBytes,FALSE);
串decryptedString = System.Text.Encoding.UTF8.GetString(平原)
Console.WriteLine(SALIDA:+ decryptedString);

现在我想要做相反的......但我得到这样(的大小一些错误?关键应该是128字节...等),我应该怎么办呢

下面我添加当前的非工作代码:



.NET

 公共字符串加密(字符串文本)
{
const int的PROVIDER_RSA_FULL = 1;
常量字符串CONTAINER_NAME =跟踪器;

CspParameters cspParams;
cspParams =新CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
的RSACryptoServiceProvider RS​​A1 =新的RSACryptoServiceProvider(cspParams);
rsa1.FromXmlString(< RSAKeyValue><模> 2rRVVVFJRbH / wAPDtnwZwu + NXU + AZ6uXxh / SW + AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw / FfcM9DsKs / rYR83tBLiIAfgdnVjF27tZID + HJMFTiI30mALjr7 + TFP + 2lIACXA1RIKTk7S9pDmX8 =< /模量GT;<指数> AQAB< /指数>&下;差异无显着92jJJyzFBSx6gL4Y1YpALmc5CNjoE / wETjqb3ci2v0 + 3rZWvJKmKy1ZEdlXpyuvXVksJ6cMdUpNAkMknUk9pTQ ==下; / P>&所述; Q> 4kxkABZOXyDLryYGCGY0b8N0FIdu5BTCFDYEdcatxl / f7ZGDS1NgHJpUWxkVXFfHy2Y / GuDOIbpcwlsO739H + W ==下; / Q>&下; DP> 5bNFvrdUHF + VRN45VFjNCcgQLeSkY5mBrdfASoNFGA29LM5iE5nNIMfxPCS7sQiRnq6Af6YFHVtVgJchiMvtqQ ==下; / DP> ;&所述; DQ> J + ng1qVY5epnXlWiFIla45C7K6sNfIMvAcdwgq39KWEjeWPGyYqWXtpOtzh2eylf6Bx4GVHKBW0NPJTIJMsfLQ ==下; / DQ>&下; InverseQ> 8uu0dfPVDqB2qFM1Vdi8hl + 2uZtN7gjT2co1cEWy29HVYBZD0k9KKCf2​​PbkeuSfpgFpE70wW5Hrp8V7l / SwSOw ==&下; / InverseQ>&所述d取代; MM / c18zroJ2Iqi9s5 / asvUBF3pjO3NSEbFjFpP / NT6WdKimvECWPz2xT6NlV0Vc6tQaAAmtn7Bt + HPhfVdrA4 / ysYVe3 / 6TWkPjW + bvAhMWu / ZqISx11 / jPYSGD9g3ZXgUiqcQM8UbOjlswoq4fpheEXTB0xdVutDLpO3qgHN6k = LT / d取代;&下; / RSAKeyValue>中);

System.Text.UTF8Encoding编码=新System.Text.UTF8Encoding();
字节[] = textBytes encoding.GetBytes(文本);
字节[] = encryptedOutput rsa1.Encrypt(textBytes,FALSE);
串outputB64 = Convert.ToBase64String(encryptedOutput);
Console.WriteLine(outputB64);
返回outputB64;
}



的Java



 公共静态字符串解密(字符串encodedString)抛出IllegalBlockSizeException,UnsupportedEncodingException,InvalidKeyException将,抛出:NoSuchAlgorithmException,InvalidKeySpecException,NoSuchPaddingException,BadPaddingException 
{
字节[] modulusBytes = Base64.decode(2rRVVVFJRbH / wAPDtnwZwu + NXU + AZ6uXxh / SW + AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw / FfcM9DsKs / rYR83tBLiIAfgdnVjF27tZID + HJMFTiI30mALjr7 + TFP + 2lIACXA1RIKTk7S9pDmX8 =);
字节[] = exponentBytes Base64.decode(AQAB);
BigInteger的模量=新的BigInteger(1 modulusBytes);
BigInteger的指数=新的BigInteger(1 exponentBytes);

RSAPrivateKeySpec rsaPrivKey =新RSAPrivateKeySpec(模数,指数);
的KeyFactory其实= KeyFactory.getInstance(RSA);
PrivateKey的privKey = fact.generatePrivate(rsaPrivKey);

密码加密= Cipher.getInstance(RSA / ECB / PKCS1Padding);
cipher.init(Cipher.DECRYPT_MODE,privKey作为);

字节[] = base64String Base64.decode(encodedString);
字节[] = plainBytes新的String(base64String).getBytes(UTF-8);
字节[] =的CipherData cipher.doFinal(plainBytes);

的System.out.println(的CipherData);
返回cipherData.toString();
}


解决方案

的最后几行您Java的代码解密没有意义。这些线是:



 字节[] = base64String Base64.decode(encodedString); 
字节[] = plainBytes新的String(base64String).getBytes(UTF-8);
字节[] =的CipherData cipher.doFinal(plainBytes);

的System.out.println(的CipherData);
返回cipherData.toString();

您必须扭转你使用.NET加密步骤的顺序。首先,你应该解码的Base64编码字符串来获取密码字节。你这样做,但你贴错标签的结果为 base64String 。你或许应该称这个结果的CipherData 。其次,你需要解密的CipherData获得纯文本。第三,你应该创建使用的字符集两ARG String构造的第二个参数plainbytes字符串。这里是代码应该是什么样子,或者接近它

 字节[] =的CipherData Base64.decode(encodedString); 
字节[] = plainBytes cipher.doFinal(的CipherData);

返回新的String(plainBytes,UTF-8);



最后,在Java中的每一个对象具有toString()方法,但它并不总是做你想。对于数组的toString()方法简单地返回对象ID的表示该阵列,排序相当于一个内存地址的JVM的



编辑:



我错过了你还用你的解密代码错了钥匙。您正在使用的RSA公钥,但你必须改用RSA私钥。


I am trying to encrypt strings in .NET by using a RSA algorithm and decrypt the result in Java. At the moment, I have been able to do the opposite (Encrypt in Java, Decrypt in .NET). Here I have my code that actually works (JAVA encryption):

byte[] modulusBytes = Base64.decode("2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=");
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("big kitty dancing").getBytes("UTF-8");
byte[] cipherData = cipher.doFinal( plainBytes );
String encryptedString = Base64.encode(cipherData);
return encryptedString;

And (.NET decryption)

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>2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=</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 = "BaB21vY+RD/jiY3AAsb269fIWTEH38s0xLUfJ7CoVUgaQ6vYzB0tiJ1Ag9HNEdCcuZdGchhqnms8jpsqsHC1iKrz6QCLsgUU7VNWDfQqZYR6Rl/GwR0biK2STnOL+g06f/JUdixHOHOgROify1m8qppYo5plpOVMqYFzEMREMkM=";

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

byte[] plain = rsa1.Decrypt(encyrptedBytes, false);
string decryptedString = System.Text.Encoding.UTF8.GetString(plain);
Console.WriteLine("SALIDA: " + decryptedString);

Now I want to do the opposite... But I get some errors like (the size of the key should be 128 bytes... etc) How should I do it?

Here I add the current non working code:

.NET

public string Encrypt(string text)
{
    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>2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=</Modulus><Exponent>AQAB</Exponent><P>92jJJyzFBSx6gL4Y1YpALmc5CNjoE/wETjqb3ci2v0+3rZWvJKmKy1ZEdlXpyuvXVksJ6cMdUpNAkMknUk9pTQ==</P><Q>4kxkABZOXyDLryYGCGY0b8N0FIdu5BTCFDYEdcatxl/f7ZGDS1NgHJpUWxkVXFfHy2Y/GuDOIbpcwlsO739H+w==</Q><DP>5bNFvrdUHF+VRN45VFjNCcgQLeSkY5mBrdfASoNFGA29LM5iE5nNIMfxPCS7sQiRnq6Af6YFHVtVgJchiMvtqQ==</DP><DQ>j+ng1qVY5epnXlWiFIla45C7K6sNfIMvAcdwgq39KWEjeWPGyYqWXtpOtzh2eylf6Bx4GVHKBW0NPJTIJMsfLQ==</DQ><InverseQ>8uu0dfPVDqB2qFM1Vdi8hl+2uZtN7gjT2co1cEWy29HVYBZD0k9KKCf2PbkeuSfpgFpE70wW5Hrp8V7l/SwSOw==</InverseQ><D>MM/c18zroJ2Iqi9s5/asvUBF3pjO3NSEbFjFpP/NT6WdKimvECWPz2xT6NlV0Vc6tQaAAmtn7Bt+HPhfVdrA4/ysYVe3/6TWkPjW+bvAhMWu/ZqISx11/jPYSGD9g3ZXgUiqcQM8UbOjlswoq4fpheEXTB0xdVutDLpO3qgHN6k=</D></RSAKeyValue>");

    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    byte[] textBytes = encoding.GetBytes(text);
    byte[] encryptedOutput = rsa1.Encrypt(textBytes, false);
    string outputB64 = Convert.ToBase64String(encryptedOutput);
    Console.WriteLine(outputB64);
    return outputB64;
}

Java

public static String Decrypt(String encodedString) throws IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException
{
    byte[] modulusBytes = Base64.decode("2rRVVVFJRbH/wAPDtnwZwu+nxU+AZ6uXxh/sW+AMCBogg7vndZsnRiHoLttYYPqOyOhfgaBOQogrIfrKL4lipK4m52SBzw/FfcM9DsKs/rYR83tBLiIAfgdnVjF27tZID+HJMFTiI30mALjr7+tfp+2lIACXA1RIKTk7S9pDmX8=");
    byte[] exponentBytes = Base64.decode("AQAB");
    BigInteger modulus = new BigInteger(1, modulusBytes );
    BigInteger exponent = new BigInteger(1, exponentBytes);

    RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus, exponent);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PrivateKey privKey = fact.generatePrivate(rsaPrivKey);

    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, privKey);

    byte[] base64String = Base64.decode(encodedString);
    byte[] plainBytes = new String(base64String).getBytes("UTF-8");
    byte[] cipherData = cipher.doFinal(plainBytes);

    System.out.println(cipherData);
    return cipherData.toString();
}

解决方案

The last few lines of your Java decrypt code do not make sense. These lines are:

byte[] base64String = Base64.decode(encodedString);
byte[] plainBytes = new String(base64String).getBytes("UTF-8");
byte[] cipherData = cipher.doFinal(plainBytes);

System.out.println(cipherData);
return cipherData.toString();

You have to reverse the order of the steps you used to encrypt in .NET. First, you should Base64 decode the encoded string to get the cipher bytes. You did that, but you mislabeled the result as base64String. You probably should call this result cipherData. Second, you need to decrypt cipherData to get plain text. Third, you should create a string from plainbytes using the two-arg String constructor with the Charset for the second argument. Here is what the code should look like, or close to it.

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

return new String(plainBytes, "UTF-8");

Finally, in Java every object has a toString() method but it doesn't always do what you want. For arrays the toString() method simply returns a representation of object id for that array, sort of the JVM equivalent of a memory address.

EDIT:

I missed that you are also using the wrong key in your decrypt code. Your are using the RSA public key, but you must instead use the RSA private key.

这篇关于RSA加密.NET的Java解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆