Search in sources :

Example 16 with Cipher

use of javax.crypto.Cipher 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 17 with Cipher

use of javax.crypto.Cipher 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 18 with Cipher

use of javax.crypto.Cipher in project remusic by aa112901.

the class RSAUtils method decryptByPrivateKey.

/**
     * 私钥解密
     *
     * @param data
     * @param privateKey
     * @return
     * @throws Exception
     */
public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    //模长
    int key_len = privateKey.getModulus().bitLength() / 8;
    byte[] bytes = data.getBytes();
    byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
    System.err.println(bcd.length);
    //如果密文长度大于模长则要分组解密
    String ming = "";
    byte[][] arrays = splitArray(bcd, key_len);
    for (byte[] arr : arrays) {
        ming += new String(cipher.doFinal(arr));
    }
    return ming;
}
Also used : Cipher(javax.crypto.Cipher)

Example 19 with Cipher

use of javax.crypto.Cipher in project remusic by aa112901.

the class RSAUtils method encryptByPublicKey.

/**
     * 公钥加密
     *
     * @param data
     * @param publicKey
     * @return
     * @throws Exception
     */
public static String encryptByPublicKey(String data, RSAPublicKey publicKey) throws Exception {
    // Cipher cipher = Cipher.getInstance("RSA");
    Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    //        // 模长
    //        int key_len = publicKey.getModulus().bitLength() / 8;
    //        // 加密数据长度 <= 模长-11
    //        String[] datas = splitString(data, key_len - 11);
    //        String mi = "";
    //        //如果明文长度大于模长-11则要分组加密
    //        for (String s : datas) {
    //            mi += bcd2Str(cipher.doFinal(s.getBytes()));
    //        }
    String mi = "";
    mi = toHex(cipher.doFinal(data.getBytes()));
    return mi;
}
Also used : Cipher(javax.crypto.Cipher)

Example 20 with Cipher

use of javax.crypto.Cipher 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)

Aggregations

Cipher (javax.crypto.Cipher)626 SecretKey (javax.crypto.SecretKey)168 SecretKeySpec (javax.crypto.spec.SecretKeySpec)166 IvParameterSpec (javax.crypto.spec.IvParameterSpec)130 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)105 InvalidKeyException (java.security.InvalidKeyException)76 SecretKeyFactory (javax.crypto.SecretKeyFactory)75 IOException (java.io.IOException)72 GeneralSecurityException (java.security.GeneralSecurityException)68 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)60 Key (java.security.Key)54 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)54 BadPaddingException (javax.crypto.BadPaddingException)51 SecureRandom (java.security.SecureRandom)44 UnsupportedEncodingException (java.io.UnsupportedEncodingException)43 KeyGenerator (javax.crypto.KeyGenerator)43 PrivateKey (java.security.PrivateKey)41 PublicKey (java.security.PublicKey)40 PBEKeySpec (javax.crypto.spec.PBEKeySpec)36 KeyFactory (java.security.KeyFactory)35