use of javax.crypto.spec.IvParameterSpec in project UltimateAndroid by cymcsg.
the class TripleDES method decrypt.
public static String decrypt(byte[] message) throws Exception {
byte[] values = Base64decodingByte(message, 0);
final MessageDigest md = MessageDigest.getInstance("SHA-1");
final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
final byte[] keyBytes = copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8; ) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
String s1 = "12345678";
byte[] bytes = s1.getBytes();
final IvParameterSpec iv = new IvParameterSpec(bytes);
final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key, iv);
final byte[] plainText = decipher.doFinal(values);
return new String(plainText, "UTF-8");
}
use of javax.crypto.spec.IvParameterSpec in project SeaStar by 13120241790.
the class AESUtils method Decrypt.
// 解密
public static String Decrypt(String content) throws Exception {
try {
SecretKeySpec skeySpec = toKey(defaultPwd);
Cipher cipher = Cipher.getInstance(selectMod(type));
IvParameterSpec iv = new IvParameterSpec(IV.getBytes());
if (isPwd == false) {
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
} else {
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
}
byte[] encrypted1 = Base64.decode(content, "UTF-8").getBytes();
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
use of javax.crypto.spec.IvParameterSpec in project SeaStar by 13120241790.
the class BackAES method encrypt.
/******************************** 方法一,密匙必须为16位 **********************************************/
// 加密
public static byte[] encrypt(String sSrc, String sKey, int type) {
byte[] encrypted = null;
try {
sKey = toMakekey(sKey, pwdLenght, val);
Cipher cipher = Cipher.getInstance(selectMod(type));
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, WAYS);
// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
if (isPwd == false) {
// ECB 不用密码
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
} else {
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
}
encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
// 此处使用BASE64做转码。
return Base64.encode(encrypted);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 此处使用BASE64做转码。
return null;
}
use of javax.crypto.spec.IvParameterSpec in project nhin-d by DirectProject.
the class PKCS11Commands method exportPrivateKey.
@Command(name = "ExportPrivateKey", usage = EXPORT_PRIVATE_KEY)
public void exportPrivateKey(String[] args) {
final String alias = StringArrayUtil.getRequiredValue(args, 0);
final String wrapperAlias = StringArrayUtil.getRequiredValue(args, 1);
final String file = StringArrayUtil.getOptionalValue(args, 2, alias + "-privKey.der");
try {
final KeyStore ks = mgr.getKS();
// get the wrapper key
final Key wrapperKey = mgr.getKey(wrapperAlias);
if (wrapperKey == null) {
System.out.println("Wrapper key with name " + wrapperKey + " does not exist.");
return;
}
if (!ks.containsAlias(alias)) {
System.out.println("Private key with name " + alias + " does not exist.");
return;
}
final PrivateKey privKey = (PrivateKey) ks.getKey(alias, "".toCharArray());
if (privKey == null) {
System.out.println("Key name " + alias + " does not contain a private key");
return;
}
// the algorithm used to wrap the key depends on the key type
Cipher myWrapper = null;
if (wrapperKey.getAlgorithm().startsWith("AES")) {
myWrapper = Cipher.getInstance("AES/CBC/PKCS5Padding", ks.getProvider().getName());
AlgorithmParameters mAlgParams = null;
try {
mAlgParams = AlgorithmParameters.getInstance("IV", ks.getProvider().getName());
mAlgParams.init(new IvParameterSpec(AbstractPKCS11TokenKeyStoreProtectionManager.IV_BYTES));
} catch (Exception e) {
}
if (mAlgParams == null)
myWrapper.init(Cipher.WRAP_MODE, wrapperKey, new IvParameterSpec(AbstractPKCS11TokenKeyStoreProtectionManager.IV_BYTES));
else
myWrapper.init(Cipher.WRAP_MODE, wrapperKey, mAlgParams);
} else if (wrapperKey.getAlgorithm().startsWith("RSA")) {
myWrapper = Cipher.getInstance("RSA/ECB/NoPadding", ks.getProvider().getName());
myWrapper.init(Cipher.WRAP_MODE, wrapperKey);
}
byte[] wrappedKey = null;
try {
wrappedKey = myWrapper.wrap(privKey);
} catch (Exception e) {
System.out.println("Private key with name " + alias + " could not be extracted. Your hardware may not allow exporting of private keys or " + "attributes on the key may not allow the key to be exported. \r\nError message: " + e.getMessage());
e.printStackTrace();
return;
}
final File fl = new File(file);
FileUtils.writeByteArrayToFile(fl, wrappedKey);
System.out.println("Wrapped private key written to file " + fl.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed to export private key: " + e.getMessage());
}
}
use of javax.crypto.spec.IvParameterSpec in project nhin-d by DirectProject.
the class AbstractPKCS11TokenKeyStoreProtectionManager method wrapWithSecretKey.
/**
* {@inheritDoc}}
*/
@Override
public byte[] wrapWithSecretKey(SecretKey kek, Key keyToWrap) throws CryptoException {
final IvParameterSpec iv = new IvParameterSpec(IV_BYTES);
try {
final Cipher wrapCipher = Cipher.getInstance(WRAP_ALGO, ks.getProvider().getName());
wrapCipher.init(Cipher.WRAP_MODE, kek, iv);
return wrapCipher.wrap(keyToWrap);
} catch (Exception e) {
throw new CryptoException("Failed to wrap key: " + e.getMessage(), e);
}
}
Aggregations