本文整理汇总了Java中javax.crypto.SecretKeyFactory.getInstance方法的典型用法代码示例。如果您正苦于以下问题:Java SecretKeyFactory.getInstance方法的具体用法?Java SecretKeyFactory.getInstance怎么用?Java SecretKeyFactory.getInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.SecretKeyFactory
的用法示例。
在下文中一共展示了SecretKeyFactory.getInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* Description 根据键值进行加密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一个可信任的随机数源
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(data);
}
示例2: getCipher
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
throws GeneralSecurityException {
// NoSuchAlgorithException
SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");
// InvalidKeyException
KeySpec spec = new DESedeKeySpec(key, 0);
// InvalidKeySpecException
SecretKey secretKey = factory.generateSecret(spec);
// IV
if (ivec == null) {
ivec = ZERO_IV;
}
// NoSuchAlgorithmException, NoSuchPaddingException
// NoSuchProviderException
Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);
// InvalidKeyException, InvalidAlgorithParameterException
cipher.init(mode, secretKey, encIv);
return cipher;
}
示例3: PBKDF2
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
private static byte[] PBKDF2(char[] secret, byte[] salt,
int count, int keyLength) throws GeneralSecurityException {
PBEKeySpec keySpec = new PBEKeySpec(secret, salt, count, keyLength);
SecretKeyFactory skf =
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
SecretKey key = skf.generateSecret(keySpec);
byte[] result = key.getEncoded();
return result;
}
示例4: decrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* 解密
*
* @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);
}
示例5: MyPBKDF2SecretKey
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* The key is generating by SecretKeyFactory and its value just copying
* in the key field of MySecretKey class. So, this is real key derived
* using the given algo.
*/
public MyPBKDF2SecretKey(String passPhrase, String algo, byte[] salt1,
int iterationCount, int keySize)
throws InvalidKeySpecException, NoSuchAlgorithmException {
algorithm = algo;
salt = salt1;
itereationCount = iterationCount;
pass = passPhrase;
PBEKeySpec spec = new PBEKeySpec(passPhrase.toCharArray(), salt,
iterationCount, keySize);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algo);
SecretKey realKey = keyFactory.generateSecret(spec);
keyLength = realKey.getEncoded().length;
key = new byte[keyLength];
System.arraycopy(realKey.getEncoded(), 0, key, 0, keyLength);
}
示例6: generateAndTranslateKey
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* The test case scenario implemented in the method: - derive PBKDF2 key
* using the given algorithm; - translate the key - check if the translated
* and original keys have the same key value.
*
* @return true if the test case passed; false - otherwise.
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws InvalidKeyException
*/
public boolean generateAndTranslateKey() throws NoSuchAlgorithmException,
InvalidKeySpecException, InvalidKeyException {
// derive PBKDF2 key
SecretKey key1 = getSecretKeyForPBKDF2(algoToTest);
// translate key
SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest);
SecretKey key2 = skf.translateKey(key1);
// check if it still the same after translation
if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) {
System.err.println("generateAndTranslateKey test case failed: the "
+ "key1 and key2 values in its primary encoding format are "
+ "not the same for " + algoToTest + "algorithm.");
return false;
}
return true;
}
示例7: hash
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* This method will hash the password and salt combination. Be careful, this method will erase the password after hashed it.
* You must be sure that you do not need it after using this method.
*
* @param password the password who needs to be hashed
* @param salt some list of <code>byte</code> which will be included in the password
* @return a hash of the password and salting combination.
*/
public static byte[] hash(char[] password, byte[] salt) {
final PBEKeySpec spec = new PBEKeySpec(password, salt, SecurityUtils.ITERATIONS, SecurityUtils.KEY_LENGTH);
try {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(SecurityUtils.ALGORITHM);
SecretKey key = secretKeyFactory.generateSecret(spec);
return key.getEncoded();
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
LOGGER.debug(e.getMessage(), e);
throw new SecurityException(e);
} finally {
// erase the password in the char array in order to not retrieve it in the java memory
spec.clearPassword();
}
}
示例8: decrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* Decrypt cipher text.
* @param str Cipher text to decrypt.
* @return Decrypted plain text.
*/
public String decrypt(final String str) {
// Handle null or empty string decryption more gracefully
if (str == null || str.isEmpty()) {
return str;
}
try {
final byte[] ciphertext = DatatypeConverter.parseBase64Binary(str);
if (ciphertext.length < 48) {
return null;
}
final byte[] salt = Arrays.copyOfRange(ciphertext, 0, 16);
final byte[] iv = Arrays.copyOfRange(ciphertext, 16, 32);
final byte[] ct = Arrays.copyOfRange(ciphertext, 32, ciphertext.length);
final SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
final KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), salt, 65536, 128);
final SecretKey tmp = factory.generateSecret(spec);
final SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
final byte[] plaintext = cipher.doFinal(ct);
return new String(plaintext, StandardCharsets.UTF_8);
} catch (final Exception exception) {
throw new RuntimeException(exception.getMessage(), exception);
}
}
示例9: secret
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
private static SecretKey secret() throws NoSuchAlgorithmException, InvalidKeySpecException {
byte salt[] = new byte[] { (byte)0x33, (byte)0x7b, (byte)0x09, (byte)0x0e, (byte)0xcf, (byte)0x5a, (byte)0x58, (byte)0xd9 };
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(ApplicationProperty.UrlEncoderSecret.value().toCharArray(), salt, 1024, 128);
SecretKey key = factory.generateSecret(spec);
return new SecretKeySpec(key.getEncoded(), "AES");
}
示例10: decryptByCBC
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
*
* (根据CBC 加密模式、指定加密key输出对应的BASE64编码解密串)<br>
*
* @param content
* @param decryptKey
* @return
* @throws Exception
* String
* @Author fanyaowu
* @data 2015年2月2日
* @exception
* @version
*
*/
public static String decryptByCBC(String content, String decryptKey) throws Exception
{
try
{
DESKeySpec dks = new DESKeySpec(decryptKey.getBytes());
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPT_TYPE);
SecretKey secretKey = keyFactory.generateSecret(dks);
// using DES in CBC mode
Cipher cipher = Cipher.getInstance(CBC_ALGORITHM);
// 用密匙初始化Cipher对象
IvParameterSpec param = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, param);
// 正式执行解密操作
byte decryptedData[] = cipher.doFinal(Base64.decodeBase64(content));
return new String(decryptedData);
}
catch (Exception e)
{
throw new EncryptException("Failed to encrypt for content: "
+ content + ", encrypt key: " + decryptKey, e);
}
}
示例11: decode
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/***
* 解密
* @param encryptText 要解密文字
* @return 解密文字
* @throws Exception
*/
public static String decode(String encryptText) throws Exception {
if (TextUtils.isEmpty(secretKey) || TextUtils.isEmpty(iv))
throw new NullPointerException("u should init first");
SecretKey deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(DESEDE_CBC_PKCS5_PADDING);
IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
cipher.init(2, deskey, ips);
byte[] decryptData = cipher.doFinal(Base64Util.decode(encryptText));
return new String(decryptData, encoding);
}
示例12: decrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* decripta il messaggio
*
* @param message messaggio per la decritptazione
* @return stringa decriptata
* @throws Exception
*/
private String decrypt(String message) throws Exception {
byte[] bytesrc = convertHexString(message);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}
示例13: loadPrivateKey2
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
private static PrivateKey loadPrivateKey2() throws Exception {
String privateKeyPEM = getKeyFromFile("./keys/privatekey-pkcs8.pem");
// strip of header, footer, newlines, whitespaces
privateKeyPEM = privateKeyPEM
.replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "")
.replace("-----END ENCRYPTED PRIVATE KEY-----", "")
.replaceAll("\\s", "");
// decode to get the binary DER representation
byte[] privateKeyDER = Base64.getDecoder().decode(privateKeyPEM);
EncryptedPrivateKeyInfo epkInfo = new EncryptedPrivateKeyInfo(privateKeyDER);
System.out.println("Algo: ");
System.out.println(epkInfo.getAlgName());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName());
PBEKeySpec pbeKeySpec = new PBEKeySpec("123".toCharArray());
SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(epkInfo.getAlgName());
cipher.init(Cipher.DECRYPT_MODE, pbeKey, epkInfo.getAlgParameters());
// return epkInfo.getKeySpec(cipher);
KeyFactory keyFactory2 = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory2.generatePrivate(epkInfo.getKeySpec(cipher));
return privateKey;
}
示例14: init
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
private static void init(byte[] byteData) throws DESException {
try {
DKS = new DESKeySpec(byteData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SECRET_KEY = keyFactory.generateSecret(DKS);
CIPHER = Cipher.getInstance(DES);
} catch (Exception e) {
throw new DESException("use the parameter key initial DES exception.", e);
}
}
示例15: encrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
protected String encrypt(String value ) {
try {
final byte[] bytes = value!=null ? value.getBytes(UTF8) : new byte[0];
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(Secure.getString(context.getContentResolver(), Secure.ANDROID_ID).getBytes(UTF8), 20));
return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP),UTF8);
} catch( Exception e ) {
throw new RuntimeException(e);
}
}