Question

Je suis sur le cryptage RSA en utilisant JAVA et tenter de décrypter en utilisant .NET. Je compris mon code Java et le code .NET dans l'espoir que quelqu'un a une certaine expérience avec ce genre de chose.

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);

A partir de ce code JAVA je prends les résultats de l'encryptedString qui se trouvent être:

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

Et tenter de décrypter en utilisant le code suivant .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);
Était-ce utile?

La solution

Du côté Java, vous devez utiliser "RSA/ECB/PKCS1Padding" comme nom de l'algorithme. Avec le nom de "RSA" plaine, Java n'ajoute pas le PKCS # 1 rembourrage qui la mise en œuvre C #, d'où les attend « mauvaises données ».

Le rembourrage est une transformation des données d'entrée (de votre chaîne codée) dans une chaîne de caractères un peu plus grand qui a la même longueur que le module RSA (128 octets ici). Il est important pour la sécurité et injectent un peu aléatoire (la même chaîne d'entrée ne donnera pas la même chaîne cryptée à chaque fois, mais le processus de déchiffrement qui supprime le hasard et récupère la chaîne de droite néanmoins).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top