use of javax.crypto.spec.SecretKeySpec in project ride-read-android by Ride-Read.
the class EncryptUtils method desTemplate.
/**
* DES加密模板
*
* @param data 数据
* @param key 秘钥
* @param algorithm 加密算法
* @param transformation 转变
* @param isEncrypt {@code true}: 加密 {@code false}: 解密
* @return 密文或者明文,适用于DES,3DES,AES
*/
public static byte[] desTemplate(byte[] data, byte[] key, String algorithm, String transformation, boolean isEncrypt) {
if (data == null || data.length == 0 || key == null || key.length == 0)
return null;
try {
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(transformation);
SecureRandom random = new SecureRandom();
cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random);
return cipher.doFinal(data);
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}
use of javax.crypto.spec.SecretKeySpec in project yyl_example by Relucent.
the class AES method decrypt.
//=================================Methods================================================
/**
* 将密文使用AES进行解密
* @param encryptText 密文字符串
* @return 解密后的字符串
*/
public String decrypt(String encryptText) throws Exception {
//通过SecretKeySpec形成一个key
SecretKey key = new SecretKeySpec(keyByte, "AES");
//获得一个私鈅加密类Cipher,ECB是加密方式,PKCS5Padding是填充方法
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
//使用私鈅解密
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] NewCipherText = hexStringTobyteArray(encryptText);
byte[] newString = cipher.doFinal(NewCipherText);
return new String(newString, encoding);
}
use of javax.crypto.spec.SecretKeySpec in project wechat by motianhuo.
the class DES method decryptDES.
/**
* 数据解密
*
* @param decryptString
* @return
* @throws Exception
*/
public static String decryptDES(String decryptString) throws Exception {
byte[] byteMi = new BASE64().decode(decryptString);
IvParameterSpec zeroIv = new IvParameterSpec(iv);
SecretKeySpec key = new SecretKeySpec(getkeys().getBytes(), "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
byte[] decryptedData = cipher.doFinal(byteMi);
return new String(decryptedData);
}
use of javax.crypto.spec.SecretKeySpec in project MonjaDB by Kanatoko.
the class MStringUtil method getHMacSHAHash.
//-------------------------------------------------------------------------------
public static final String getHMacSHAHash(String key, String data) {
String result;
try {
// Get an hmac_sha1 key from the raw key bytes
byte[] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1_ALGORITHM);
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(data.getBytes());
// Convert raw bytes to Hex
//byte[] hexBytes = new Hex().encode(rawHmac);
// Covert array of Hex bytes to a String
result = byteToHexString(rawHmac);
//System.out.println("MAC : " + result);
} catch (Exception e) {
//throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
return "";
}
return result;
}
use of javax.crypto.spec.SecretKeySpec in project perun by CESNET.
the class Utils method cipherInput.
/**
* Return en/decrypted version of input using AES/CBC/PKCS5PADDING cipher.
* Perun's internal secretKey and initVector are used (you can configure them in
* perun.properties file).
*
* @param plainText text to en/decrypt
* @param decrypt TRUE = decrypt input / FALSE = encrypt input
* @return en/decrypted text
* @throws cz.metacentrum.perun.core.api.exceptions.InternalErrorException if anything fails
*/
public static String cipherInput(String plainText, boolean decrypt) throws InternalErrorException {
try {
String encryptionKey = BeansUtils.getCoreConfig().getPwdresetSecretKey();
String initVector = BeansUtils.getCoreConfig().getPwdresetInitVector();
Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING");
SecretKeySpec k = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
c.init((decrypt) ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, k, new IvParameterSpec(initVector.getBytes("UTF-8")));
if (decrypt) {
byte[] bytes = Base64.decodeBase64(plainText.getBytes("UTF-8"));
return new String(c.doFinal(bytes), "UTF-8");
} else {
byte[] bytes = Base64.encodeBase64(c.doFinal(plainText.getBytes("UTF-8")));
return new String(bytes, "UTF-8");
}
} catch (Exception ex) {
throw new InternalErrorException("Error when encrypting message", ex);
}
}
Aggregations