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