use of javax.crypto.spec.GCMParameterSpec in project TinyKeePass by sorz.
the class SecureStringStorage method getCipher.
private Cipher getCipher(int mode, byte[] iv) throws SystemException, KeyException {
try {
SecretKey key = (SecretKey) keyStore.getKey(KEY_ALIAS, null);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
if (iv != null) {
GCMParameterSpec params = new GCMParameterSpec(128, iv);
cipher.init(mode, key, params);
} else {
cipher.init(mode, key);
}
return cipher;
} catch (KeyException e) {
throw e;
} catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
throw new SystemException(e);
}
}
use of javax.crypto.spec.GCMParameterSpec in project android by nextcloud.
the class EncryptionUtils method decryptStringSymmetric.
/**
* Decrypt string with RSA algorithm, ECB mode, OAEPWithSHA-256AndMGF1 padding
* Asymmetric encryption, with private and public key
*
* @param string string to decrypt
* @param encryptionKeyBytes key from metadata
* @return decrypted string
*/
public static String decryptStringSymmetric(String string, byte[] encryptionKeyBytes) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(AES_CIPHER);
String ivString;
int delimiterPosition = string.lastIndexOf(ivDelimiter);
if (delimiterPosition == -1) {
// backward compatibility
delimiterPosition = string.lastIndexOf(ivDelimiterOld);
ivString = string.substring(delimiterPosition + ivDelimiterOld.length());
} else {
ivString = string.substring(delimiterPosition + ivDelimiter.length());
}
String cipherString = string.substring(0, delimiterPosition);
byte[] iv = new IvParameterSpec(decodeStringToBase64Bytes(ivString)).getIV();
Key key = new SecretKeySpec(encryptionKeyBytes, AES);
GCMParameterSpec spec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.DECRYPT_MODE, key, spec);
byte[] bytes = decodeStringToBase64Bytes(cipherString);
byte[] encodedBytes = cipher.doFinal(bytes);
return decodeBase64BytesToString(encodedBytes);
}
use of javax.crypto.spec.GCMParameterSpec in project android by nextcloud.
the class EncryptionUtils method encryptFile.
/**
* @param file file do crypt
* @param encryptionKeyBytes key, either from metadata or {@link EncryptionUtils#generateKey()}
* @param iv initialization vector, either from metadata or {@link EncryptionUtils#randomBytes(int)}
* @return encryptedFile with encryptedBytes and authenticationTag
*/
public static EncryptedFile encryptFile(File file, byte[] encryptionKeyBytes, byte[] iv) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IOException {
Cipher cipher = Cipher.getInstance(AES_CIPHER);
Key key = new SecretKeySpec(encryptionKeyBytes, AES);
GCMParameterSpec spec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
byte[] fileBytes = new byte[(int) randomAccessFile.length()];
randomAccessFile.readFully(fileBytes);
byte[] cryptedBytes = cipher.doFinal(fileBytes);
String authenticationTag = encodeBytesToBase64String(Arrays.copyOfRange(cryptedBytes, cryptedBytes.length - (128 / 8), cryptedBytes.length));
return new EncryptedFile(cryptedBytes, authenticationTag);
}
use of javax.crypto.spec.GCMParameterSpec in project hutool by looly.
the class AESTest method gcmTest.
/**
* 见:https://blog.csdn.net/weixin_42468911/article/details/114358682
*/
@Test
public void gcmTest() {
final SecretKey key = KeyUtil.generateKey("AES");
byte[] iv = RandomUtil.randomBytes(12);
AES aes = new AES("GCM", "NoPadding", key, new GCMParameterSpec(128, iv));
// 原始数据
String phone = "13534534567";
// 加密
byte[] encrypt = aes.encrypt(phone);
final String decryptStr = aes.decryptStr(encrypt);
Assert.assertEquals(phone, decryptStr);
}
use of javax.crypto.spec.GCMParameterSpec in project qpid-broker-j by apache.
the class AESGCMKeyFileEncrypter method decrypt.
@Override
public String decrypt(final String encrypted) {
if (!EncryptionHelper.isValidBase64(encrypted)) {
throw new IllegalArgumentException(String.format("Encrypted value is not valid Base 64 data: '%s'", encrypted));
}
final byte[] encryptedBytes = Strings.decodeBase64(encrypted);
if (encryptedBytes.length < GCM_INITIALIZATION_VECTOR_LENGTH) {
throw new IllegalArgumentException(String.format("Encrypted value length is less than expected : '%s'", encrypted));
}
try {
final Cipher cipher = Cipher.getInstance(CIPHER_NAME);
final byte[] initializationVectorBytes = new byte[GCM_INITIALIZATION_VECTOR_LENGTH];
System.arraycopy(encryptedBytes, 0, initializationVectorBytes, 0, GCM_INITIALIZATION_VECTOR_LENGTH);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, initializationVectorBytes);
cipher.init(Cipher.DECRYPT_MODE, _secretKey, gcmParameterSpec);
return new String(EncryptionHelper.readFromCipherStream(encryptedBytes, GCM_INITIALIZATION_VECTOR_LENGTH, encryptedBytes.length - GCM_INITIALIZATION_VECTOR_LENGTH, cipher), StandardCharsets.UTF_8);
} catch (IOException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalArgumentException("Unable to decrypt secret", e);
}
}
Aggregations