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);
}
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();
}
Aggregations