Schlechte Polsterung Ausnahme :pad-block beschädigt beim Aufruf dofinal

Arbeite ich an der Verschlüsselung und Entschlüsselung. Ich bin sehr neu in die Kryptographie und ich bin mit pad-block beschädigt Ausnahme bei der Verwendung von bouncy castle

Hier ist mein Verschlüsselungs - /Entschlüsselungs-code.

private AESFastEngine Motor;

private BufferedBlockCipher cipher;

private final KeyParameter key=setEncryptionKey("testinggtestingg");

public  KeyParameter setEncryptionKey(String keyText) {
    //adding in spaces to force a proper key
    keyText += "                ";

    //cutting off at 128 bits (16 characters)
    keyText = keyText.substring(0, 16);

    byte[] keyBytes = keyText.getBytes();
    //key = new KeyParameter(keyBytes);
    engine = new AESFastEngine();
    cipher = new PaddedBufferedBlockCipher(engine);
    return new KeyParameter(keyBytes);

}

public String encryptString(String plainText) {

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

        return (new String(cipherBytes));
    } catch (DataLengthException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (InvalidCipherTextException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    //else
    return null;
}

public String decryptString(String encryptedText) {
    try {
        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);

        //crop accordingly
        int index = decryptedString.indexOf("\u0000");
        if (index >= 0) {
            decryptedString = decryptedString.substring(0, index);
        }
        return decryptedString;
    } catch (DataLengthException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (InvalidCipherTextException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    //else
    return null;
} 

InformationsquelleAutor pradeepds | 2013-01-18

Schreibe einen Kommentar