Search in sources :

Example 81 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project ETSMobile-Android2 by ApplETS.

the class SecurePreferences method decrypt.

private static String decrypt(String ciphertext) {
    if (ciphertext == null || ciphertext.length() == 0) {
        return ciphertext;
    }
    try {
        final Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(SecurePreferences.sKey, "AES"));
        return new String(cipher.doFinal(SecurePreferences.decode(ciphertext)), "UTF-8");
    } catch (Exception e) {
        Log.w(SecurePreferences.class.getName(), "decrypt", e);
        return null;
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 82 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project ETSMobile-Android2 by ApplETS.

the class SecurePreferences method encrypt.

private static String encrypt(String cleartext) {
    if (cleartext == null || cleartext.length() == 0) {
        return cleartext;
    }
    try {
        final Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(SecurePreferences.sKey, "AES"));
        return SecurePreferences.encode(cipher.doFinal(cleartext.getBytes("UTF-8")));
    } catch (Exception e) {
        Log.w(SecurePreferences.class.getName(), "encrypt", e);
        return null;
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 83 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Gradle-demo by Arisono.

the class AESUtil method encryptAES.

/**
	 * 加密
	 * @throws Exception 
	 */
public static byte[] encryptAES(byte[] data, byte[] key) throws Exception {
    //恢复密钥
    SecretKey secretKey = new SecretKeySpec(key, "AES");
    //Cipher完成加密
    Cipher cipher = Cipher.getInstance("AES");
    //根据密钥对cipher进行初始化
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    //加密
    byte[] encrypt = cipher.doFinal(data);
    return encrypt;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 84 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Gradle-demo by Arisono.

the class DESUtil method encryptDES.

/**
	 * 加密
	 * @throws Exception 
	 */
public static byte[] encryptDES(byte[] data, byte[] key) throws Exception {
    //获得密钥
    SecretKey secretKey = new SecretKeySpec(key, "DES");
    //Cipher完成加密
    Cipher cipher = Cipher.getInstance("DES");
    //初始化cipher
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    //加密
    byte[] encrypt = cipher.doFinal(data);
    return encrypt;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 85 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Gradle-demo by Arisono.

the class DESede method decrypt3DES.

/**
	 * 解密
	 */
public static byte[] decrypt3DES(byte[] data, byte[] key) throws Exception {
    //恢复密钥
    SecretKey secretKey = new SecretKeySpec(key, "DESede");
    //Cipher完成解密
    Cipher cipher = Cipher.getInstance("DESede");
    //初始化cipher
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plain = cipher.doFinal(data);
    return plain;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

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