Search in sources :

Example 36 with DESKeySpec

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

the class DesHelper method decrypt.

/**
 * @方法描述: 用指定的key对数据进行DES解密.
 * @param datasource
 *            待解密的数据
 * @param password
 *            DES解密的key
 * @return 返回DES解密后的数据
 */
public static String decrypt(String datasource, String key, String type) throws Exception {
    // DES算法要求有一个可信任的随机数源
    SecureRandom sr = new SecureRandom();
    // 从原始密匙数据创建一个DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(key.getBytes(ENCODING));
    // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SecureUtil.DES);
    // 将DESKeySpec对象转换成SecretKey对象
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher对象实际完成解密操作
    Cipher cipher = Cipher.getInstance(type);
    // 用密匙初始化Cipher对象
    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
    // 正式执行解密操作
    /**
     * 为了防止解密时报javax.crypto.IllegalBlockSizeException: Input length must be
     * multiple of 8 when decrypting with padded cipher异常,
     * 不能把加密后的字节数组直接转换成字符串
     */
    byte[] encryptBytes = cipher.doFinal(Base64Helper.decode(datasource.toCharArray()));
    return new String(encryptBytes);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 37 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project new-cloud by xie-summer.

the class ECBEncrypt method des.

/**
 * Description: des加密
 *
 * @author Ivan 2009-8-25 下午04:08:37
 * @param reqBytes
 * @param key
 * @return
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws UnsupportedEncodingException
 * @throws ShortBufferException
 */
public static byte[] des(byte[] reqBytes, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ShortBufferException {
    SecretKeySpec keySpec = null;
    DESKeySpec deskey = null;
    deskey = new DESKeySpec(key);
    keySpec = new SecretKeySpec(deskey.getKey(), "DES");
    Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    byte[] cipherText = new byte[cipher.getOutputSize(reqBytes.length)];
    int ctLength = cipher.update(reqBytes, 0, reqBytes.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    // byte[] data = cipher.doFinal(reqBytes);
    return cipherText;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec)

Example 38 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project oxCore by GluuFederation.

the class StringEncrypter method encrypt.

/**
 * Encrypt a string
 *
 * @param unencryptedString
 *            String to encrypt
 * @return Encrypted string (using scheme and key specified at construction)
 * @throws EncryptionException
 */
public String encrypt(final String unencryptedString, String encryptionKey) throws EncryptionException {
    lock.lock();
    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
        String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
        KeySpec keySpec;
        if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
            keySpec = new DESedeKeySpec(keyAsBytes);
        } else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
            keySpec = new DESKeySpec(keyAsBytes);
        } else {
            throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
        }
        return encrypt(unencryptedString, keySpec);
    } catch (final Exception e) {
        throw new EncryptionException(e);
    } finally {
        lock.unlock();
    }
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) KeySpec(java.security.spec.KeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 39 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project translationstudio8 by heartsome.

the class DESImpl method decrypt.

/**
	 * 解密.
	 * @param src
	 *            数据源
	 * @param key
	 *            密钥,长度必须是8的倍数
	 * @return byte[]
	 * 			  解密后的原始数据
	 * @throws Exception
	 *             the exception
	 */
public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
    SecureRandom sr = new SecureRandom();
    DESKeySpec dks = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance(DES);
    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 40 with DESKeySpec

use of javax.crypto.spec.DESKeySpec in project translationstudio8 by heartsome.

the class DESImpl method encrypt.

/**
	 * 加密.
	 * @param src
	 *            数据源
	 * @param key
	 *            密钥,长度必须是8的倍数
	 * @return byte[]
	 * 			  加密后的数据
	 * @throws Exception
	 *             the exception
	 */
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    SecureRandom sr = new SecureRandom();
    DESKeySpec dks = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance(DES);
    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