Search in sources :

Example 26 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project smartmodule by carozhu.

the class DESUtils method encrypt.

/**
 * 加密
 *
 * @param bytesContent
 *            待加密内容
 * @param key
 *            加密的密钥
 * @return
 */
public static byte[] encrypt(byte[] bytesContent, String key) {
    try {
        SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
        byte[] result = cipher.doFinal(bytesContent);
        return result;
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 27 with DESKeySpec

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

the class DesExample method encrypt.

/**
 * 根据密匙加密数据*
 */
public static byte[] encrypt(byte[] data, byte[] keydate) throws Exception {
    DESKeySpec dks = new DESKeySpec(keydate);
    // 创建一个密匙工厂
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(dks);
    // Cipher对象实际完成加密操作
    Cipher cipher = Cipher.getInstance("DES");
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.ENCRYPT_MODE, key, new SecureRandom());
    // 执行加密操作
    return cipher.doFinal(data);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 28 with DESKeySpec

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

the class DesExample method decode.

public static byte[] decode(byte[] bytes, byte[] keydate) throws Exception {
    // 从原始密匙数据创建一个DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(keydate);
    // 创建一个密匙工厂获得SecretKey对象
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey key = keyFactory.generateSecret(dks);
    // Cipher对象实际完成解密操作
    Cipher cipher = Cipher.getInstance("DES");
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.DECRYPT_MODE, key, new SecureRandom());
    // 执行解密操作
    return cipher.doFinal(bytes);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 29 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project newsrob by marianokamp.

the class EntryManager method getSecretKey.

private static SecretKey getSecretKey() throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESKeySpec keySpec = new DESKeySpec("EntryManager.class".getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(keySpec);
    return secretKey;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 30 with DESKeySpec

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

the class DESCipher method encrypt.

/**
 *   * 加密
 *
 *   *
 *
 *   * @param src
 *
 *   * 明文(字节)
 *
 *   * @param key
 *
 *   * 密钥,长度必须是8的倍数
 *
 *   * @return 密文(字节)
 *
 *   * @throws Exception
 *
 *   
 */
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    // DES算法要求有一个可信任的随机数源
    SecureRandom sr = new SecureRandom();
    // 从原始密匙数据创建DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(key);
    // 创建一个密匙工厂,然后用它把DESKeySpec转换成
    // 一个SecretKey对象
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher对象实际完成加密操作
    Cipher cipher = Cipher.getInstance(DES);
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    return cipher.doFinal(src);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

DESKeySpec (javax.crypto.spec.DESKeySpec)69 SecretKeyFactory (javax.crypto.SecretKeyFactory)52 SecretKey (javax.crypto.SecretKey)48 Cipher (javax.crypto.Cipher)45 SecureRandom (java.security.SecureRandom)20 InvalidKeyException (java.security.InvalidKeyException)13 KeySpec (java.security.spec.KeySpec)12 IvParameterSpec (javax.crypto.spec.IvParameterSpec)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 BadPaddingException (javax.crypto.BadPaddingException)8 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)8 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)8 DESedeKeySpec (javax.crypto.spec.DESedeKeySpec)8 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 SecretKeySpec (javax.crypto.spec.SecretKeySpec)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Key (java.security.Key)4 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)3 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)2 SQLException (android.database.SQLException)2