Search in sources :

Example 56 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project RxCache by VictorAlbertos.

the class BuiltInEncryptor method initCiphers.

private void initCiphers(String key) {
    try {
        SecretKeySpec secretKey = generateSecretKey(key);
        encryptCipher = Cipher.getInstance("AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        decryptCipher = Cipher.getInstance("AES");
        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IOException(java.io.IOException)

Example 57 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project DataX by alibaba.

the class SecretUtil method encrypt3DES.

/**
     * 加密 DESede<br>
     * 用密钥加密
     *
     * @param data 裸的原始数据
     * @param key  加密的密钥
     * @return 结果也采用base64加密
     * @throws Exception
     */
public static String encrypt3DES(String data, String key) {
    try {
        // 生成密钥
        SecretKey desKey = new SecretKeySpec(build3DesKey(key), KEY_ALGORITHM_3DES);
        // 对数据加密
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_3DES);
        cipher.init(Cipher.ENCRYPT_MODE, desKey);
        return encryptBASE64(cipher.doFinal(data.getBytes(ENCODING)));
    } catch (Exception e) {
        throw DataXException.asDataXException(FrameworkErrorCode.SECRET_ERROR, "3重DES加密出错", e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DataXException(com.alibaba.datax.common.exception.DataXException)

Example 58 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project remusic by aa112901.

the class AESTools method encrpty.

public static String encrpty(String paramString) {
    MessageDigest messageDigest = null;
    try {
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    messageDigest.update(INPUT.getBytes());
    byte[] stringBytes = messageDigest.digest();
    StringBuilder stringBuilder = new StringBuilder(stringBytes.length * 2);
    for (int i = 0; i < stringBytes.length; i++) {
        stringBuilder.append(CHARS[((stringBytes[i] & 0xF0) >>> 4)]);
        stringBuilder.append(CHARS[(stringBytes[i] & 0xF)]);
    }
    String str = stringBuilder.toString();
    SecretKeySpec localSecretKeySpec = new SecretKeySpec(str.substring(str.length() / 2).getBytes(), "AES");
    Cipher localCipher;
    try {
        localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        localCipher.init(1, localSecretKeySpec, new IvParameterSpec(IV.getBytes()));
        return URLEncoder.encode(new String(BytesHandler.getChars(localCipher.doFinal(paramString.getBytes()))), "utf-8");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return "";
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 59 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project remusic by aa112901.

the class Aes method decrypt.

private static byte[] decrypt(byte[] content, String password) {
    try {
        byte[] keyStr = getKey(password);
        SecretKeySpec key = new SecretKeySpec(keyStr, "AES");
        //algorithmStr
        Cipher cipher = Cipher.getInstance(algorithmStr);
        //   ʼ
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] result = cipher.doFinal(content);
        //
        return result;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 60 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project sonarqube by SonarSource.

the class AesCipher method loadSecretFileFromFile.

@VisibleForTesting
Key loadSecretFileFromFile(@Nullable String path) throws IOException {
    if (StringUtils.isBlank(path)) {
        throw new IllegalStateException("Secret key not found. Please set the property " + CoreProperties.ENCRYPTION_SECRET_KEY_PATH);
    }
    File file = new File(path);
    if (!file.exists() || !file.isFile()) {
        throw new IllegalStateException("The property " + CoreProperties.ENCRYPTION_SECRET_KEY_PATH + " does not link to a valid file: " + path);
    }
    String s = FileUtils.readFileToString(file);
    if (StringUtils.isBlank(s)) {
        throw new IllegalStateException("No secret key in the file: " + path);
    }
    return new SecretKeySpec(Base64.decodeBase64(StringUtils.trim(s)), CRYPTO_KEY);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) File(java.io.File) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

SecretKeySpec (javax.crypto.spec.SecretKeySpec)498 Cipher (javax.crypto.Cipher)194 SecretKey (javax.crypto.SecretKey)142 Mac (javax.crypto.Mac)110 IvParameterSpec (javax.crypto.spec.IvParameterSpec)106 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)94 InvalidKeyException (java.security.InvalidKeyException)67 IOException (java.io.IOException)44 Key (java.security.Key)36 SecureRandom (java.security.SecureRandom)30 Test (org.junit.Test)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)29 GeneralSecurityException (java.security.GeneralSecurityException)27 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)27 MessageDigest (java.security.MessageDigest)25 BadPaddingException (javax.crypto.BadPaddingException)25 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)25 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)18 PrivateKey (java.security.PrivateKey)18 PublicKey (java.security.PublicKey)16