Search in sources :

Example 61 with DESKeySpec

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

the class EncryptUtil 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)

Example 62 with DESKeySpec

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

the class EncryptUtil method decrypt.

/**
 * ????
 *
 * @param src ????
 * @param key ??????????????8?????
 * @return ??????????????
 * @throws Exception
 */
public static byte[] decrypt(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.DECRYPT_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)

Example 63 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project wechat by dllwh.

the class DesHelper method encrypt.

/**
 * ----------------------------------------------------- Fields end
 */
/**
 * ----------------------------------------------- [私有方法]
 */
/**
 * ----------------------------------------------- [私有方法]
 */
/**
 * @方法描述: 用指定的key对数据进行DES加密.
 * @param datasource
 *            DES加密数据
 * @param password
 *            DES加密的key
 * @return 返回解密后的数据
 */
public static String encrypt(String dataSource, String key, String type) throws Exception {
    // DES算法要求有一个可信任的随机数源
    SecureRandom random = new SecureRandom();
    // 从原始密钥数据创建DESKeySpec对象
    DESKeySpec desKey = new DESKeySpec(key.getBytes(ENCODING));
    // 创建一个密匙工厂,然后用它把DESKeySpec转换成
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SecureUtil.DES);
    SecretKey securekey = keyFactory.generateSecret(desKey);
    // Cipher对象实际完成加密操作
    Cipher cipher = Cipher.getInstance(type);
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
    // 正式执行加密操作
    return Base64Helper.encode(cipher.doFinal(dataSource.getBytes(ENCODING)));
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 64 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project cloudstack by apache.

the class VncClient method encodePassword.

/**
 * Encode password using DES encryption with given challenge.
 *
 * @param challenge
 *            a random set of bytes.
 * @param password
 *            a password
 * @return DES hash of password and challenge
 */
public byte[] encodePassword(byte[] challenge, String password) throws Exception {
    // VNC password consist of up to eight ASCII characters.
    // Padding
    byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
    byte[] passwordAsciiBytes = password.getBytes(RfbConstants.CHARSET);
    System.arraycopy(passwordAsciiBytes, 0, key, 0, Math.min(password.length(), 8));
    // Flip bytes (reverse bits) in key
    for (int i = 0; i < key.length; i++) {
        key[i] = flipByte(key[i]);
    }
    KeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
    Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] response = cipher.doFinal(challenge);
    return response;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) KeySpec(java.security.spec.KeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 65 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project AndroidUtilCode by Blankj.

the class EncryptUtils method symmetricTemplate.

/**
 * Return the bytes of symmetric encryption or decryption.
 *
 * @param data           The data.
 * @param key            The key.
 * @param algorithm      The name of algorithm.
 * @param transformation The name of the transformation, e.g., <i>DES/CBC/PKCS5Padding</i>.
 * @param isEncrypt      True to encrypt, false otherwise.
 * @return the bytes of symmetric encryption or decryption
 */
private static byte[] symmetricTemplate(final byte[] data, final byte[] key, final String algorithm, final String transformation, final byte[] iv, final boolean isEncrypt) {
    if (data == null || data.length == 0 || key == null || key.length == 0)
        return null;
    try {
        SecretKey secretKey;
        if ("DES".equals(algorithm)) {
            DESKeySpec desKey = new DESKeySpec(key);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
            secretKey = keyFactory.generateSecret(desKey);
        } else {
            secretKey = new SecretKeySpec(key, algorithm);
        }
        Cipher cipher = Cipher.getInstance(transformation);
        if (iv == null || iv.length == 0) {
            cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKey);
        } else {
            AlgorithmParameterSpec params = new IvParameterSpec(iv);
            cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKey, params);
        }
        return cipher.doFinal(data);
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

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