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);
}
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;
}
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();
}
}
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);
}
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);
}
Aggregations