Search in sources :

Example 1 with DESEngine

use of org.bouncycastle.crypto.engines.DESEngine in project gocd by gocd.

the class GoCipher method decipher.

public String decipher(byte[] key, String cipherText) throws InvalidCipherTextException {
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
    cipher.init(false, new KeyParameter(Hex.decode(key)));
    byte[] cipherTextBytes = java.util.Base64.getDecoder().decode(cipherText);
    byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)];
    int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0);
    cipher.doFinal(plainTextBytes, outputLength);
    int paddingStarts = plainTextBytes.length - 1;
    for (; paddingStarts >= 0; paddingStarts--) {
        if (plainTextBytes[paddingStarts] != 0) {
            break;
        }
    }
    return new String(plainTextBytes, 0, paddingStarts + 1);
}
Also used : PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) DESEngine(org.bouncycastle.crypto.engines.DESEngine) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Example 2 with DESEngine

use of org.bouncycastle.crypto.engines.DESEngine in project gocd by gocd.

the class GoCipher method cipher.

public String cipher(byte[] key, String plainText) throws InvalidCipherTextException {
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine()));
    KeyParameter keyParameter = new KeyParameter(Hex.decode(key));
    cipher.init(true, keyParameter);
    byte[] plainTextBytes = plainText.getBytes();
    byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)];
    int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0);
    cipher.doFinal(cipherTextBytes, outputLength);
    return java.util.Base64.getEncoder().encodeToString(cipherTextBytes).trim();
}
Also used : PaddedBufferedBlockCipher(org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher) DESEngine(org.bouncycastle.crypto.engines.DESEngine) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) CBCBlockCipher(org.bouncycastle.crypto.modes.CBCBlockCipher)

Aggregations

DESEngine (org.bouncycastle.crypto.engines.DESEngine)2 CBCBlockCipher (org.bouncycastle.crypto.modes.CBCBlockCipher)2 PaddedBufferedBlockCipher (org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher)2 KeyParameter (org.bouncycastle.crypto.params.KeyParameter)2