use of org.bouncycastle.crypto.CipherParameters in project openremote by openremote.
the class SecureUtils method calculateCMAC.
public static byte[] calculateCMAC(final byte[] data, final byte[] key) {
final byte[] cmac = new byte[16];
CipherParameters cipherParameters = new KeyParameter(key);
BlockCipher blockCipher = new AESEngine();
CMac mac = new CMac(blockCipher);
mac.init(cipherParameters);
mac.update(data, 0, data.length);
mac.doFinal(cmac, 0);
return cmac;
}
use of org.bouncycastle.crypto.CipherParameters in project openremote by openremote.
the class SecureUtils method encryptWithAES.
public static byte[] encryptWithAES(final byte[] data, final byte[] key) {
final byte[] encrypted = new byte[data.length];
final CipherParameters cipherParameters = new KeyParameter(key);
final AESLightEngine engine = new AESLightEngine();
engine.init(true, cipherParameters);
engine.processBlock(data, 0, encrypted, 0);
return encrypted;
}
use of org.bouncycastle.crypto.CipherParameters in project elastic-core-maven by OrdinaryDude.
the class Crypto method aesGCMDecrypt.
public static byte[] aesGCMDecrypt(byte[] ivCiphertext, byte[] key) {
try {
if (ivCiphertext.length < 16) {
throw new InvalidCipherTextException("invalid ivCiphertext length");
}
byte[] iv = Arrays.copyOfRange(ivCiphertext, 0, 16);
byte[] ciphertext = Arrays.copyOfRange(ivCiphertext, 16, ivCiphertext.length);
GCMBlockCipher aes = new GCMBlockCipher(new AESEngine());
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(false, ivAndKey);
byte[] output = new byte[aes.getOutputSize(ciphertext.length)];
int plaintextLength = aes.processBytes(ciphertext, 0, ciphertext.length, output, 0);
plaintextLength += aes.doFinal(output, plaintextLength);
byte[] result = new byte[plaintextLength];
System.arraycopy(output, 0, result, 0, result.length);
return result;
} catch (InvalidCipherTextException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
use of org.bouncycastle.crypto.CipherParameters in project elastic-core-maven by OrdinaryDude.
the class Crypto method aesEncrypt.
public static byte[] aesEncrypt(byte[] plaintext, byte[] key) {
try {
byte[] iv = new byte[16];
secureRandom.get().nextBytes(iv);
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(true, ivAndKey);
byte[] output = new byte[aes.getOutputSize(plaintext.length)];
int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
ciphertextLength += aes.doFinal(output, ciphertextLength);
byte[] result = new byte[iv.length + ciphertextLength];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(output, 0, result, iv.length, ciphertextLength);
return result;
} catch (InvalidCipherTextException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
use of org.bouncycastle.crypto.CipherParameters in project elastic-core-maven by OrdinaryDude.
the class Crypto method aesGCMEncrypt.
public static byte[] aesGCMEncrypt(byte[] plaintext, byte[] key) {
try {
byte[] iv = new byte[16];
secureRandom.get().nextBytes(iv);
GCMBlockCipher aes = new GCMBlockCipher(new AESEngine());
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(true, ivAndKey);
byte[] output = new byte[aes.getOutputSize(plaintext.length)];
int ciphertextLength = aes.processBytes(plaintext, 0, plaintext.length, output, 0);
ciphertextLength += aes.doFinal(output, ciphertextLength);
byte[] result = new byte[iv.length + ciphertextLength];
System.arraycopy(iv, 0, result, 0, iv.length);
System.arraycopy(output, 0, result, iv.length, ciphertextLength);
return result;
} catch (InvalidCipherTextException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
Aggregations