use of javax.crypto.spec.DESKeySpec in project musicbrainz-android by jdamcd.
the class SimpleEncrypt method crypto.
private static String crypto(int mode, String secret, String input) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
DESKeySpec keySpec = new DESKeySpec(secret.getBytes(CHARSET));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey encryptKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(mode, encryptKey);
if (mode == Cipher.ENCRYPT_MODE) {
byte[] plainText = input.getBytes(CHARSET);
return Base64.encodeToString(cipher.doFinal(plainText), Base64.DEFAULT);
} else if (mode == Cipher.DECRYPT_MODE) {
byte[] cipherText = Base64.decode(input, Base64.DEFAULT);
return new String(cipher.doFinal(cipherText), CHARSET);
}
return null;
}
use of javax.crypto.spec.DESKeySpec in project smartmodule by carozhu.
the class DESUtils method decrypt.
/**
* 解密
*
* @param content
* 待解密内容
* @param key
* 解密的密钥
* @return
*/
public static byte[] decrypt(byte[] content, String key) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
return cipher.doFinal(content);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
use of javax.crypto.spec.DESKeySpec in project DataX by alibaba.
the class DESCipher 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 oxCore by GluuFederation.
the class StringEncrypter method decrypt.
public String decrypt(final String encryptedString, String encryptionKey, boolean silent) 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 decrypt(encryptedString, keySpec, silent);
} catch (final Exception e) {
throw new EncryptionException(e);
} finally {
lock.unlock();
}
}
Aggregations