Search in sources :

Example 41 with GCMParameterSpec

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);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyException(java.security.KeyException) UnrecoverableKeyException(java.security.UnrecoverableKeyException)

Example 42 with GCMParameterSpec

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);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) PublicKey(java.security.PublicKey)

Example 43 with GCMParameterSpec

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);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) PublicKey(java.security.PublicKey)

Example 44 with GCMParameterSpec

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);
}
Also used : SecretKey(javax.crypto.SecretKey) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) AES(cn.hutool.crypto.symmetric.AES) Test(org.junit.Test)

Example 45 with GCMParameterSpec

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);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)109 Cipher (javax.crypto.Cipher)79 SecretKeySpec (javax.crypto.spec.SecretKeySpec)47 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)32 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)31 InvalidKeyException (java.security.InvalidKeyException)30 BadPaddingException (javax.crypto.BadPaddingException)29 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)29 SecretKey (javax.crypto.SecretKey)21 GeneralSecurityException (java.security.GeneralSecurityException)12 AEADBadTagException (javax.crypto.AEADBadTagException)12 Key (java.security.Key)11 ByteBuffer (java.nio.ByteBuffer)7 RequiresApi (androidx.annotation.RequiresApi)6 IOException (java.io.IOException)6 Test (org.junit.Test)6 ExcludedTest (com.google.security.wycheproof.WycheproofRunner.ExcludedTest)5 NoPresubmitTest (com.google.security.wycheproof.WycheproofRunner.NoPresubmitTest)5 SlowTest (com.google.security.wycheproof.WycheproofRunner.SlowTest)5