本文整理汇总了Java中javax.crypto.SecretKeyFactory.generateSecret方法的典型用法代码示例。如果您正苦于以下问题:Java SecretKeyFactory.generateSecret方法的具体用法?Java SecretKeyFactory.generateSecret怎么用?Java SecretKeyFactory.generateSecret使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.SecretKeyFactory
的用法示例。
在下文中一共展示了SecretKeyFactory.generateSecret方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: encrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* 加密
* @param datasource byte[]
* @param password String
* @return byte[]
*/
public static String encrypt(String datasource, String password) {
try {
if(StringUtils.trimToNull(datasource) == null){
return null;
}
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(password.getBytes());
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
// 现在,获取数据并加密
// 正式执行加密操作
return new BASE64Encoder().encode(cipher.doFinal(datasource.getBytes()));
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
示例2: wrapperPBEKeyTest
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
private void wrapperPBEKeyTest(Provider p) throws InvalidKeySpecException,
InvalidKeyException, NoSuchPaddingException,
IllegalBlockSizeException, InvalidAlgorithmParameterException,
NoSuchAlgorithmException {
for (String alg : PBE_ALGORITHM_AR) {
String baseAlgo = alg.split("/")[0].toUpperCase();
// only run the tests on longer key lengths if unlimited version
// of JCE jurisdiction policy files are installed
if (Cipher.getMaxAllowedKeyLength(alg) < Integer.MAX_VALUE
&& (baseAlgo.endsWith("TRIPLEDES") || alg
.endsWith("AES_256"))) {
out.println("keyStrength > 128 within " + alg
+ " will not run under global policy");
continue;
}
SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
SecretKey key = skf.generateSecret(new PBEKeySpec("Secret Lover"
.toCharArray()));
wrapTest(alg, alg, key, key, Cipher.SECRET_KEY, true);
}
}
示例3: cryptData
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
protected byte[] cryptData(
boolean forEncryption,
AlgorithmIdentifier algId,
char[] password,
boolean wrongPKCS12Zero,
byte[] data)
throws IOException
{
String algorithm = algId.getAlgorithm().getId();
PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
PBEKeySpec pbeSpec = new PBEKeySpec(password);
try
{
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(
pbeParams.getIV(),
pbeParams.getIterations().intValue());
BCPBEKey key = (BCPBEKey)keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPKCS12Zero);
Cipher cipher = Cipher.getInstance(algorithm, bcProvider);
int mode = forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE;
cipher.init(mode, key, defParams);
return cipher.doFinal(data);
}
catch (Exception e)
{
throw new IOException("exception decrypting data - " + e.toString());
}
}
示例4: decrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* DES解密
*
* @param src
* @param key
* @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: encode
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* DES算法,加密
*
* @param data
* 待加密字符串
* @param key
* 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
* @throws Exception
*/
public static String encode(String key, String data) {
if (data == null)
return null;
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
AlgorithmParameterSpec paramSpec = iv;
cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
byte[] bytes = cipher.doFinal(data.getBytes());
return byte2String(bytes);
} catch (Exception e) {
e.printStackTrace();
return data;
}
}
示例6: desEncrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* DES加密
*/
public static String desEncrypt(String source, String desKey) throws Exception {
try {
// 从原始密匙数据创建DESKeySpec对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(new DESKeySpec(desKey.getBytes()));
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey);
// 现在,获取数据并加密
byte[] destBytes = cipher.doFinal(source.getBytes());
StringBuilder hexRetSB = new StringBuilder();
for (byte b : destBytes) {
String hexString = Integer.toHexString(0x00ff & b);
hexRetSB.append(hexString.length() == 1 ? 0 : "").append(hexString);
}
return hexRetSB.toString();
} catch (Exception e) {
throw new Exception("DES加密发生错误", e);
}
}
示例7: getEncodedPrivateKeySpec
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
public PKCS8EncodedKeySpec getEncodedPrivateKeySpec(String key, String password) throws Exception {
String privateKeyPEM = key
.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);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName());
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(epkInfo.getAlgName());
cipher.init(Cipher.DECRYPT_MODE, pbeKey, epkInfo.getAlgParameters());
return epkInfo.getKeySpec(cipher);
}
示例8: 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 algorithm.
*
* @param passPhrase some string intended to be a password
* @param algo PBKDF2 algorithm
* @param salt slat for PBKDF2
* @param iterationCount iteration count
* @param keySize key size in bits
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
*/
public MyPBKDF2SecretKey(String passPhrase, String algo, byte[] salt,
int iterationCount, int keySize)
throws InvalidKeySpecException, NoSuchAlgorithmException {
this.algorithm = algo;
this.salt = salt;
this.itereationCount = iterationCount;
this.keySize = keySize;
this.pass = passPhrase;
PBEKeySpec spec = new PBEKeySpec(passPhrase.toCharArray(),
this.salt, iterationCount, this.keySize);
SecretKeyFactory keyFactory
= SecretKeyFactory.getInstance(algo);
SecretKey realKey = keyFactory.generateSecret(spec);
this.keyLength = realKey.getEncoded().length;
this.key = new byte[this.keyLength];
System.arraycopy(realKey.getEncoded(), 0, this.key, 0,
this.keyLength);
}
示例9: 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);
}
示例10: encryptByCBC
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
*
* (根据CBC 加密模式、指定加密key输出对应的BASE64编码加密串)<br>
*
* @param content
* @param encryptKey
* @return
* @throws Exception
* String
* @Author fanyaowu
* @data 2015年2月2日
* @exception
* @version
*
*/
public static String encryptByCBC(String content, String encryptKey) throws Exception
{
try
{
DESKeySpec dks = new DESKeySpec(encryptKey.getBytes());
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
// 一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPT_TYPE);
SecretKey secretKey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(CBC_ALGORITHM);
// 用密匙初始化Cipher对象
IvParameterSpec param = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, param);
// 执行加密操作
byte encryptedData[] = cipher.doFinal(content.getBytes());
return Base64.encodeBase64String(encryptedData);
}
catch (Exception e)
{
throw new EncryptException("Failed to encrypt for content: "
+ content + ", encrypt key: " + encryptKey, e);
}
}
示例11: getKey
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
private static Key getKey(String password) {
try {
// ��Կ����
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
// ��Կ����
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
// ������Կ
SecretKey secretKey = keyFactory.generateSecret(keySpec);
return secretKey;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例12: 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");
}
示例13: encrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* Encrypts the message using password based encryption.
* @param algo
* the encryption algorithm
* @param plaintext
* the message to be encrypted
* @param password
* the password
* @param iterationCount
* the iteration count
* @param salt
* the salt
* @return iv and the cipher text in form of
* len(iv) of 1 byte | iv of len(iv) bytes | cipher text.
* @throws GeneralSecurityException
* if error occurs.
*/
public static byte[] encrypt(PBEAlgo algo, byte[] plaintext, char[] password,
int iterationCount, byte[] salt) throws GeneralSecurityException {
ParamUtil.requireNonNull("plaintext", plaintext);
ParamUtil.requireNonNull("password", password);
ParamUtil.requireMin("iterationCount", iterationCount, 1);
ParamUtil.requireNonNull("salt", salt);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algo.algoName());
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKey pbeKey = secretKeyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance(algo.algoName());
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, iterationCount);
cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParameterSpec);
pbeKeySpec.clearPassword();
byte[] iv = cipher.getIV();
int ivLen = (iv == null) ? 0 : iv.length;
if (ivLen > 255) {
throw new GeneralSecurityException("IV too long: " + ivLen);
}
byte[] cipherText = cipher.doFinal(plaintext);
byte[] ret = new byte[1 + ivLen + cipherText.length];
// length of IV
ret[0] = (byte) (ivLen & 0xFF);
if (ivLen > 0) {
System.arraycopy(iv, 0, ret, 1, ivLen);
}
System.arraycopy(cipherText, 0, ret, 1 + ivLen, cipherText.length);
return ret;
}
示例14: decrypt
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
protected String decrypt(String value){
try {
final byte[] bytes = value!=null ? Base64Support.decode(value,Base64Support.DEFAULT) : new byte[0];
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(
Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID).getBytes(UTF8), 20));
return new String(pbeCipher.doFinal(bytes),UTF8);
} catch( Exception e) {
Log.e(this.getClass().getName(), "Warning, could not decrypt the value. It may be stored in plaintext. "+e.getMessage());
value = "error";
return value;
}
}
示例15: desDecode
import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
* 3DES解密
*
* @param encryptText 加密文本 pk 密钥
* @return 解密的结果
* @throws Exception
*/
public static String desDecode(String encryptText, String pk) throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(pk.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
byte[] decryptData = cipher.doFinal(hexStringToBytes(encryptText));
return new String(decryptData, encoding);
}