本文整理汇总了Java中javax.crypto.spec.PBEKeySpec类的典型用法代码示例。如果您正苦于以下问题:Java PBEKeySpec类的具体用法?Java PBEKeySpec怎么用?Java PBEKeySpec使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PBEKeySpec类属于javax.crypto.spec包,在下文中一共展示了PBEKeySpec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculatePbeMac
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
private static byte[] calculatePbeMac(
DERObjectIdentifier oid,
byte[] salt,
int itCount,
char[] password,
byte[] data,
String provider)
throws Exception
{
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(oid.getId(), provider);
PBEParameterSpec defParams = new PBEParameterSpec(salt, itCount);
PBEKeySpec pbeSpec = new PBEKeySpec(password);
SecretKey key = keyFact.generateSecret(pbeSpec);
Mac mac = Mac.getInstance(oid.getId(), provider);
mac.init(key, defParams);
mac.update(data);
return mac.doFinal();
}
示例2: wrapperPBEKeyTest
import javax.crypto.spec.PBEKeySpec; //导入依赖的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.spec.PBEKeySpec; //导入依赖的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: calculatePbeMac
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
private static byte[] calculatePbeMac(
ASN1ObjectIdentifier oid,
byte[] salt,
int itCount,
char[] password,
boolean wrongPkcs12Zero,
byte[] data)
throws Exception
{
SecretKeyFactory keyFact = SecretKeyFactory.getInstance(oid.getId(), bcProvider);
PBEParameterSpec defParams = new PBEParameterSpec(salt, itCount);
PBEKeySpec pbeSpec = new PBEKeySpec(password);
BCPBEKey key = (BCPBEKey)keyFact.generateSecret(pbeSpec);
key.setTryWrongPKCS12Zero(wrongPkcs12Zero);
Mac mac = Mac.getInstance(oid.getId(), bcProvider);
mac.init(key, defParams);
mac.update(data);
return mac.doFinal();
}
示例5: engineGetKeySpec
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
/**
* Returns a specification (key material) of the given key
* in the requested format.
*
* @param key the key
*
* @param keySpecCl the requested format in which the key material shall be
* returned
*
* @return the underlying key specification (key material) in the
* requested format
*
* @exception InvalidKeySpecException if the requested key
* specification is inappropriate for the given key, or the
* given key cannot be processed (e.g., the given key has an
* unrecognized algorithm or format).
*/
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpecCl)
throws InvalidKeySpecException {
if (key instanceof javax.crypto.interfaces.PBEKey) {
// Check if requested key spec is amongst the valid ones
if ((keySpecCl != null)
&& PBEKeySpec.class.isAssignableFrom(keySpecCl)) {
javax.crypto.interfaces.PBEKey pKey =
(javax.crypto.interfaces.PBEKey) key;
return new PBEKeySpec
(pKey.getPassword(), pKey.getSalt(),
pKey.getIterationCount(), pKey.getEncoded().length*8);
} else {
throw new InvalidKeySpecException("Invalid key spec");
}
} else {
throw new InvalidKeySpecException("Invalid key " +
"format/algorithm");
}
}
示例6: JCEPBEKey
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
/**
* @param param
*/
public JCEPBEKey(
String algorithm,
DERObjectIdentifier oid,
int type,
int digest,
int keySize,
int ivSize,
PBEKeySpec pbeKeySpec,
CipherParameters param)
{
this.algorithm = algorithm;
this.oid = oid;
this.type = type;
this.digest = digest;
this.keySize = keySize;
this.ivSize = ivSize;
this.pbeKeySpec = pbeKeySpec;
this.param = param;
}
示例7: fill_q
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
/**
* A method to get 64 random bytes deterministically based on user input
* And to also fill the queue with numbers
**/
private void fill_q(char[] sentence, char[] word, String encryption) throws NoSuchAlgorithmException, InvalidKeySpecException {
// 512 bit key_length (64 bytes). Python and C++ use bytes rather than bits.
KeySpec ks = new PBEKeySpec(sentence, chars_to_bytes(word), iterations, key_length_in_bytes * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance(encryption);
if (debug) {
String hex_random_bytes = byte_array_to_hex(skf.generateSecret(ks).getEncoded());
System.out.println("PBKDF2: " + hex_random_bytes);
}
byte[] random_bytes = skf.generateSecret(ks).getEncoded();
for (byte b : random_bytes) {
if (debug) {
// & 0xFF ensures the int is unsigned
System.out.println(((int) b) & 0xFF);
}
number_queue.add(((int) b) & 0xFF);
}
}
示例8: makePBEMacParameters
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
/**
* generate a PBE based key suitable for a MAC algorithm, the
* key size is chosen according the MAC size, or the hashing algorithm,
* whichever is greater.
*/
public static CipherParameters makePBEMacParameters(
PBEKeySpec keySpec,
int type,
int hash,
int keySize)
{
PBEParametersGenerator generator = makePBEGenerator(type, hash);
byte[] key;
CipherParameters param;
key = convertPassword(type, keySpec);
generator.init(key, keySpec.getSalt(), keySpec.getIterationCount());
param = generator.generateDerivedMacParameters(keySize);
for (int i = 0; i != key.length; i++)
{
key[i] = 0;
}
return param;
}
示例9: convertPassword
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
private static byte[] convertPassword(int type, PBEKeySpec keySpec)
{
byte[] key;
if (type == PKCS12)
{
key = PBEParametersGenerator.PKCS12PasswordToBytes(keySpec.getPassword());
}
else if (type == PKCS5S2_UTF8 || type == PKCS5S1_UTF8)
{
key = PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(keySpec.getPassword());
}
else
{
key = PBEParametersGenerator.PKCS5PasswordToBytes(keySpec.getPassword());
}
return key;
}
示例10: buildUserAuth
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
private UserAuth buildUserAuth(final UID uid, final String authKey) throws IllegalArgumentException {
final UserAuth userAuth = new UserAuth();
final SecureRandom random = new SecureRandom();
final byte[] prefix = new byte[16];
// create salt
random.nextBytes(prefix);
userAuth.setUid(uid);
userAuth.setPrefix(ByteString.copyFrom(prefix));
final PBEKeySpec spec = new PBEKeySpec(authKey.toCharArray(), prefix, 65536, 128);
try {
userAuth.setAuthKey(ByteString.copyFrom(secretKey.generateSecret(spec).getEncoded()));
} catch (InvalidKeySpecException ikse) {
throw new IllegalArgumentException(ikse.toString());
}
return userAuth;
}
示例11: encriptar
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
public static String encriptar(String passPhrase, String str)
throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
Cipher ecipher = null;
Cipher dcipher = null;
java.security.spec.KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT_BYTES, ITERATION_COUNT);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
java.security.spec.AlgorithmParameterSpec paramSpec = new PBEParameterSpec(SALT_BYTES, ITERATION_COUNT);
ecipher.init(1, key, paramSpec);
dcipher.init(2, key, paramSpec);
byte utf8[] = str.getBytes("UTF8");
byte enc[] = ecipher.doFinal(utf8);
return (new BASE64Encoder()).encode(enc);
}
示例12: getPBEKey
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
private SecretKey getPBEKey(char[] password) throws IOException
{
SecretKey skey = null;
try {
PBEKeySpec keySpec = new PBEKeySpec(password);
SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");
skey = skFac.generateSecret(keySpec);
keySpec.clearPassword();
} catch (Exception e) {
throw new IOException("getSecretKey failed: " +
e.getMessage(), e);
}
return skey;
}
示例13: PBEKey
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
/**
* Creates a PBE key from a given PBE key specification.
*
* @param key the given PBE key specification
*/
PBEKey(PBEKeySpec keySpec, String keytype) throws InvalidKeySpecException {
char[] passwd = keySpec.getPassword();
if (passwd == null) {
// Should allow an empty password.
passwd = new char[0];
}
// Accept "\0" to signify "zero-length password with no terminator".
if (!(passwd.length == 1 && passwd[0] == 0)) {
for (int i=0; i<passwd.length; i++) {
if ((passwd[i] < '\u0020') || (passwd[i] > '\u007E')) {
throw new InvalidKeySpecException("Password is not ASCII");
}
}
}
this.key = new byte[passwd.length];
for (int i=0; i<passwd.length; i++)
this.key[i] = (byte) (passwd[i] & 0x7f);
java.util.Arrays.fill(passwd, ' ');
type = keytype;
}
示例14: AESObfuscator
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
/**
* @param salt an array of random bytes to use for each (un)obfuscation
* @param applicationId application identifier, e.g. the package name
* @param deviceId device identifier. Use as many sources as possible to
* create this unique identifier.
*/
public AESObfuscator(byte[] salt, String applicationId, String deviceId) {
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance(KEYGEN_ALGORITHM);
KeySpec keySpec =
new PBEKeySpec((applicationId + deviceId).toCharArray(), salt, 1024, 256);
SecretKey tmp = factory.generateSecret(keySpec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
mEncryptor = Cipher.getInstance(CIPHER_ALGORITHM);
mEncryptor.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(IV));
mDecryptor = Cipher.getInstance(CIPHER_ALGORITHM);
mDecryptor.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(IV));
} catch (GeneralSecurityException e) {
// This can't happen on a compatible Android device.
throw new RuntimeException("Invalid environment", e);
}
}
示例15: initCipher
import javax.crypto.spec.PBEKeySpec; //导入依赖的package包/类
/**
* Initiate the Cipher object for PBKDF2 algorithm using given "mode".
*
* @param mode Cipher mode: encrypt or decrypt
* @return Cipher object for PBKDF2 algorithm
* @throws GeneralSecurityException all security exceptions are thrown.
*/
@Override
protected Cipher initCipher(int mode) throws GeneralSecurityException {
Provider provider = Security.getProvider("SunJCE");
if (provider == null) {
throw new RuntimeException("SunJCE provider does not exist.");
}
// Generate secret key
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(),
salt, DEFAULT_ITERATION, PKDF2_DEFAULT_KEY_LEN);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(baseAlgo);
SecretKey key = keyFactory.generateSecret(pbeKeySpec);
// get Cipher instance
Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION, provider);
cipher.init(mode,
new SecretKeySpec(key.getEncoded(),KEY_ALGORITHM),
new IvParameterSpec(iv));
return cipher;
}