Erreur J2ME AES déchiffrage (org.bouncycastle.crypto.InvalidCipherTextException: bloc de tampon corrompu)

StackOverflow https://stackoverflow.com/questions/4323952

Question

Je suis en train de faire le chiffrement et le déchiffrement en utilisant l'algorithme AES avec château gonflable

Mon chiffrement et le déchiffrement fonctionne bien mais il me donne l'erreur quand ma taille de texte brut est plus grand

même parfois, il donne des données non déchiffrées

public static boolean setEncryptionKey(String keyText)
{
    byte[] keyBytes = keyText.getBytes();

    key = new KeyParameter(keyBytes);
    engine = new AESFastEngine();
    cipher = new PaddedBufferedBlockCipher(engine);

    return true;
}

Cryptage:

public static String encryptString(String plainText)
{

        byte[] plainArray = plainText.getBytes();

        cipher.init(true, key);
        byte[] cipherBytes = new byte[cipher.getOutputSize(plainArray.length)];
        int cipherLength = cipher.processBytes(plainArray, 0, plainArray.length, cipherBytes, 0);
        cipher.doFinal(cipherBytes, cipherLength);
        String cipherString = new String(cipherBytes);
        return cipherString;
    }

Décryptage:

public static String decryptString(String encryptedText)
{

        byte[] cipherBytes = encryptedText.getBytes();
        cipher.init(false, key);
        byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        int decryptedLength = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
        cipher.doFinal(decryptedBytes, decryptedLength);
        String decryptedString = new String(decryptedBytes);

        int index = decryptedString.indexOf("\u0000");
        if (index >= 0)
        {
            decryptedString = decryptedString.substring(0, index);
        }
        return decryptedString;
    }

Ce décryptage me donne erreur suivant

org.bouncycastle.crypto.InvalidCipherTextException: pad block corrupted
        at org.bouncycastle.crypto.paddings.PKCS7Padding.padCount(+30)
        at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(+190)
        at com.NewCrypto.decryptString(NewCrypto.java:103)
        at com.New_Midlet.startApp(New_Midlet.java:23)
        at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:44)
        at com.sun.midp.midlet.Scheduler.schedule(Scheduler.java:375)
        at com.sun.midp.main.Main.runLocalClass(Main.java:477)
        at com.sun.midp.main.Main.main(+80)

ce qui pourrait être le problème?

Était-ce utile?

La solution

La ligne

String cipherString = new String(cipherBytes);

est un bug. cipherBytes est un tableau d'octets avec des valeurs arbitraires et ne peut pas être converti en une chaîne en utilisant un quelconque des décodeurs de chaînes Java. Vous devez simplement envoyer / enregistrer le chiffre comme un tableau d'octets. Si vous devez faire une chaîne, vous devrez utiliser un encodeur. codeurs base64 sont souvent utilisés, comme le sont Base16 (hex). Vous pouvez utiliser le Apache Commons Codec ou mon préféré, le Harder base64 codec .

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