use of javax.crypto.spec.DESKeySpec in project protools by SeanDragon.
the class ToolDES method toKey.
/**
* 转换密钥
*
* @param key
* 二进制密钥
*
* @return Key 密钥
*
* @throws Exception
*/
private static Key toKey(byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
// 实例化DES密钥材料
DESKeySpec dks = new DESKeySpec(key);
// 实例化秘密密钥工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
// 生成秘密密钥
SecretKey secretKey = keyFactory.generateSecret(dks);
return secretKey;
}
use of javax.crypto.spec.DESKeySpec in project happy-dns-android by qiniu.
the class DES method encrypt.
/**
* Convert data to encrypted hex string
* @param data data to encrypt
* @param key encrypt key
* @return hex string
*/
public static String encrypt(String data, String key) {
try {
Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key.getBytes())));
return Hex.encodeHexString(c.doFinal(data.getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of javax.crypto.spec.DESKeySpec in project afwall by ukanth.
the class Api method unhideCrypt.
/**
* Decrypt the password
*
* @param key
* @param data
* @return
*/
public static String unhideCrypt(String key, String data) {
if (key == null || data == null)
return null;
String decryptStr = null;
try {
byte[] dataBytes = Base64.decode(data, base64Mode);
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] dataBytesDecrypted = (cipher.doFinal(dataBytes));
decryptStr = new String(dataBytesDecrypted);
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
return decryptStr;
}
use of javax.crypto.spec.DESKeySpec in project afwall by ukanth.
the class Api method hideCrypt.
/**
* Encrypt the password
*
* @param key
* @param data
* @return
*/
public static String hideCrypt(String key, String data) {
if (key == null || data == null)
return null;
String encodeStr = null;
try {
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
byte[] dataBytes = data.getBytes(charsetName);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
encodeStr = Base64.encodeToString(cipher.doFinal(dataBytes), base64Mode);
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}
return encodeStr;
}
use of javax.crypto.spec.DESKeySpec in project XUtil by xuexiangjys.
the class CipherUtils method getDESKey.
/**
* 返回可逆算法DES的密钥
*
* @param key 前8字节将被用来生成密钥。
* @return 生成的密钥
* @throws Exception
*/
public static Key getDESKey(byte[] key) throws Exception {
DESKeySpec des = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
return keyFactory.generateSecret(des);
}
Aggregations