Search in sources :

Example 76 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project ride-read-android by Ride-Read.

the class EncryptUtils method desTemplate.

/**
     * DES加密模板
     *
     * @param data           数据
     * @param key            秘钥
     * @param algorithm      加密算法
     * @param transformation 转变
     * @param isEncrypt      {@code true}: 加密 {@code false}: 解密
     * @return 密文或者明文,适用于DES,3DES,AES
     */
public static byte[] desTemplate(byte[] data, byte[] key, String algorithm, String transformation, boolean isEncrypt) {
    if (data == null || data.length == 0 || key == null || key.length == 0)
        return null;
    try {
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(transformation);
        SecureRandom random = new SecureRandom();
        cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random);
        return cipher.doFinal(data);
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) Cipher(javax.crypto.Cipher)

Example 77 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project yyl_example by Relucent.

the class AES method decrypt.

//=================================Methods================================================
/**
	 * 将密文使用AES进行解密
	 * @param encryptText 密文字符串
	 * @return 解密后的字符串
	 */
public String decrypt(String encryptText) throws Exception {
    //通过SecretKeySpec形成一个key
    SecretKey key = new SecretKeySpec(keyByte, "AES");
    //获得一个私鈅加密类Cipher,ECB是加密方式,PKCS5Padding是填充方法
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    //使用私鈅解密
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] NewCipherText = hexStringTobyteArray(encryptText);
    byte[] newString = cipher.doFinal(NewCipherText);
    return new String(newString, encoding);
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 78 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project wechat by motianhuo.

the class DES method decryptDES.

/**
	 * 数据解密
	 * 
	 * @param decryptString
	 * @return
	 * @throws Exception
	 */
public static String decryptDES(String decryptString) throws Exception {
    byte[] byteMi = new BASE64().decode(decryptString);
    IvParameterSpec zeroIv = new IvParameterSpec(iv);
    SecretKeySpec key = new SecretKeySpec(getkeys().getBytes(), "DES");
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
    byte[] decryptedData = cipher.doFinal(byteMi);
    return new String(decryptedData);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Example 79 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project MonjaDB by Kanatoko.

the class MStringUtil method getHMacSHAHash.

//-------------------------------------------------------------------------------
public static final String getHMacSHAHash(String key, String data) {
    String result;
    try {
        // Get an hmac_sha1 key from the raw key bytes
        byte[] keyBytes = key.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1_ALGORITHM);
        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        // Compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());
        // Convert raw bytes to Hex
        //byte[] hexBytes = new Hex().encode(rawHmac);
        //  Covert array of Hex bytes to a String
        result = byteToHexString(rawHmac);
    //System.out.println("MAC : " + result);
    } catch (Exception e) {
        //throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
        return "";
    }
    return result;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(javax.crypto.Mac) SignatureException(java.security.SignatureException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException)

Example 80 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project perun by CESNET.

the class Utils method cipherInput.

/**
	 * Return en/decrypted version of input using AES/CBC/PKCS5PADDING cipher.
	 * Perun's internal secretKey and initVector are used (you can configure them in
	 * perun.properties file).
	 *
	 * @param plainText text to en/decrypt
	 * @param decrypt TRUE = decrypt input / FALSE = encrypt input
	 * @return en/decrypted text
	 * @throws cz.metacentrum.perun.core.api.exceptions.InternalErrorException if anything fails
	 */
public static String cipherInput(String plainText, boolean decrypt) throws InternalErrorException {
    try {
        String encryptionKey = BeansUtils.getCoreConfig().getPwdresetSecretKey();
        String initVector = BeansUtils.getCoreConfig().getPwdresetInitVector();
        Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        SecretKeySpec k = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        c.init((decrypt) ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, k, new IvParameterSpec(initVector.getBytes("UTF-8")));
        if (decrypt) {
            byte[] bytes = Base64.decodeBase64(plainText.getBytes("UTF-8"));
            return new String(c.doFinal(bytes), "UTF-8");
        } else {
            byte[] bytes = Base64.encodeBase64(c.doFinal(plainText.getBytes("UTF-8")));
            return new String(bytes, "UTF-8");
        }
    } catch (Exception ex) {
        throw new InternalErrorException("Error when encrypting message", ex);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) IllegalArgumentException(java.lang.IllegalArgumentException)

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