AES cbc padding cryptage / décryptage sur plusieurs plates-formes (.net c # et nom de code un château gonflable)

Le cryptage / décryptage ne fonctionnera pas sur plusieurs plates-formes.

J’ai utilisé ce lien pour chiffrer / déchiffrer du texte à l’aide du chiffrement AES du château gonflable au sein du nom de code un.

Cryptage / décryptage AES avec Bouncycastle Exemple en J2ME

Alors que côté serveur (.net), j’utilise ce lien pour mettre en œuvre la même méthode.

AES 128bit Cross Platform (Java and C#) Encryption Compatibility

Maintenant, je ne reçois aucune erreur mais crypté à partir du nom de code on ne sera pas complètement décrypté côté serveur et vice versa.

Quelqu’un s’il vous plaît aidez-moi à ce sujet.

Code du nom de code un:

import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.CryptoException; import org.bouncycastle.crypto.engines.AESEngine; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.ParametersWithIV; import org.bouncycastle.util.encoders.Base64; public class Test { private static PaddedBufferedBlockCipher cipher = null; public static void main(Ssortingng[] args) { try { byte key[] = "MAKV2SPBNI992122".getBytes("UTF-8"); byte[] iv = new byte[16]; PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher( new AESEngine()) ); //Encryption Ssortingng plainText = "Hello How are you !2#&*()% 123456@"; byte[] plainData = plainText.getBytes("UTF-8"); KeyParameter keyParam = new KeyParameter(key); CipherParameters ivAndKey = new ParametersWithIV(keyParam, iv); cipher.init(true, ivAndKey); byte[] ciptherBytes = cipherData(plainData); //48 Ssortingng cipherText = new Ssortingng(Base64.encode(ciptherBytes), "UTF-8");//FileUtil.getSsortingngFromByteArray(Base64.encode(ciptherBytes)); System.out.println("encrypted >> "+cipherText); //Decryption byte[] cipherData = Base64.decode(cipherText); ivAndKey = new ParametersWithIV(keyParam, iv); cipher.init(false, ivAndKey); plainText = new Ssortingng(cipherData(cipherData), "UTF-8");//FileUtil.getSsortingngFromByteArray(cipherData(cipherData)); System.out.println("decrypted >> "+plainText); } catch (Exception e) { e.printStackTrace(); } } private static byte[] cipherData(byte[] data) throws CryptoException { int minSize = cipher.getOutputSize(data.length); byte[] outBuf = new byte[minSize]; int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0); int length2 = cipher.doFinal(outBuf, length1); int actualLength = length1 + length2; byte[] result = new byte[actualLength]; System.arraycopy(outBuf, 0, result, 0, result.length); return result; } 

Code de .net:

  public static RijndaelManaged GetRijndaelManaged(Ssortingng secretKey) { var keyBytes = new byte[16]; var secretKeyBytes = Encoding.UTF8.GetBytes(secretKey); Array.Copy(secretKeyBytes, keyBytes, Math.Min(keyBytes.Length, secretKeyBytes.Length)); return new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7, KeySize = 128, BlockSize = 128, Key = keyBytes, IV = keyBytes }; } public static byte[] EncryptCBC(byte[] plainBytes, RijndaelManaged rijndaelManaged) { return rijndaelManaged.CreateEncryptor() .TransformFinalBlock(plainBytes, 0, plainBytes.Length); } public static byte[] DecryptCBC(byte[] encryptedData, RijndaelManaged rijndaelManaged) { return rijndaelManaged.CreateDecryptor() .TransformFinalBlock(encryptedData, 0, encryptedData.Length); } public static Ssortingng EncryptCBCStr(Ssortingng plainText, Ssortingng key) { var plainBytes = Encoding.UTF8.GetBytes(plainText); return Convert.ToBase64Ssortingng(EncryptCBC(plainBytes, GetRijndaelManaged(key))); } public static Ssortingng DecryptCBCStr(Ssortingng encryptedText, Ssortingng key) { var encryptedBytes = Convert.FromBase64Ssortingng(encryptedText); return Encoding.UTF8.GetSsortingng(DecryptCBC(encryptedBytes, GetRijndaelManaged(key))); } // call var PlainText = "Hello How are you !2#&*()% 123456@"; var EncryptionKey = "MAKV2SPBNI992122"; var cypherCBC = EncryptCBCStr(PlainText, EncryptionKey); var decryptCBC = DecryptCBCStr(cypherCBC, EncryptionKey); 

Merci à l’avance

Ce problème a été corrigé … c’est juste un problème clé / octets IV. Comme en .net, il existe la même clé et le même IV, alors qu’en java, j’utilisais un IV différent.

correction en code java:

au lieu de cela

 byte key[] = "MAKV2SPBNI992122".getBytes("UTF-8"); byte[] iv = new byte[16]; 

utilisez ceci.

 byte key[] = "MAKV2SPBNI992122".getBytes("UTF-8"); byte[] iv = "MAKV2SPBNI992122".getBytes("UTF-8");