本文整理匯總了Java中javax.crypto.KeyGenerator.getInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java KeyGenerator.getInstance方法的具體用法?Java KeyGenerator.getInstance怎麽用?Java KeyGenerator.getInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.crypto.KeyGenerator
的用法示例。
在下文中一共展示了KeyGenerator.getInstance方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initFingerprintManager
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
private void initFingerprintManager() throws Throwable {
mFpManager = (FingerprintManager) mContext.getSystemService(Context.FINGERPRINT_SERVICE);
if (!mFpManager.isHardwareDetected())
throw new IllegalStateException("Fingerprint hardware not present");
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
KeyGenerator keyGenerator = KeyGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyStore.load(null);
keyGenerator.init(new KeyGenParameterSpec.Builder(
KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
.build());
keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance(
KeyProperties.KEY_ALGORITHM_AES + "/" +
KeyProperties.BLOCK_MODE_CBC + "/" +
KeyProperties.ENCRYPTION_PADDING_PKCS7);
SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
cipher.init(Cipher.ENCRYPT_MODE, key);
mFpHandler = new FingerprintHandler(cipher);
if (DEBUG) log("Fingeprint manager initialized");
}
示例2: getSecretKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
public static SecretKey getSecretKey(String secretKey) throws Exception
{
// 返回生成指定算法的秘密密鑰的 KeyGenerator 對象
KeyGenerator kg = KeyGenerator.getInstance(ENCRYPT_TYPE);
// 初始化此密鑰生成器,使其具有確定的密鑰大小
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(secretKey.getBytes());
kg.init(56, random);
// 生成一個密鑰
byte[] key = kg.generateKey().getEncoded();
// 實例化DES密鑰規則
DESKeySpec dks = new DESKeySpec(key);
// 實例化密鑰工廠
SecretKeyFactory skf = SecretKeyFactory.getInstance(ENCRYPT_TYPE);
// 生成密鑰
return skf.generateSecret(dks);
}
示例3: GCMParameterSpecTest
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
/**
* Initialize IV, IV with offset, plain text, AAD and SecretKey
*
* @param keyLength length of a secret key
* @param tagLength tag length
* @param IVlength IV length
* @param offset offset in a buffer for IV
* @param textLength plain text length
* @param AADLength AAD length
*/
public GCMParameterSpecTest(int keyLength, int tagLength, int IVlength,
int offset, int textLength, int AADLength)
throws NoSuchAlgorithmException, NoSuchProviderException {
this.tagLength = tagLength; // save tag length
this.IVlength = IVlength; // save IV length
this.offset = offset; // save IV offset
// prepare IV
IV = Helper.generateBytes(IVlength);
// prepare IV with offset
IVO = new byte[this.IVlength + this.offset];
System.arraycopy(IV, 0, IVO, offset, this.IVlength);
// prepare data
data = Helper.generateBytes(textLength);
// prepare AAD
AAD = Helper.generateBytes(AADLength);
// init a secret key
KeyGenerator kg = KeyGenerator.getInstance("AES", "SunJCE");
kg.init(keyLength);
key = kg.generateKey();
}
示例4: createInstance
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
@Override
protected SecretKey createInstance() throws Exception {
if (secureRandom == null) {
final SecureRandomFactory secureRandomFactory = new SecureRandomFactory();
secureRandomFactory.setProvider(provider);
secureRandomFactory.afterPropertiesSet();
secureRandom = secureRandomFactory.createInstance();
}
final KeyGenerator kg;
if (provider == null) {
kg = KeyGenerator.getInstance(algorithm);
} else {
kg = KeyGenerator.getInstance(algorithm, provider);
}
kg.init(keySize, secureRandom);
return kg.generateKey();
}
示例5: initHmacMD2Key
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
/**
* 初始化HmacMD2密鑰
*
* @return byte[] 密鑰
* @throws Exception
*/
public static byte[] initHmacMD2Key() throws Exception {
// 初始化KeyGenerator
KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD2");
// 產生秘密密鑰
SecretKey secretKey = keyGenerator.generateKey();
// 獲得密鑰
return secretKey.getEncoded();
}
示例6: generateValues
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
private void generateValues() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(RandomValuesProvider.getRandomKeyLength());
radix = Defaults.ALPHABET.radix();
key = keyGenerator.generateKey().getEncoded();
input = RandomValuesProvider.randomPlainTextOfBasicDomain();
values = RandomValuesProvider.valuesAsString(input, key, radix);
}
開發者ID:idealista,項目名稱:format-preserving-encryption-java,代碼行數:9,代碼來源:FormatPreservingEncryptionRandomPlainTextAndKeysShould.java
示例7: initKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
/**
* 產生金鑰
* KeyGenerator.getInstance
* Returns a KeyGenerator object that generates secret keys for the specified algorithm.
* SecretKey.getEncoded
* Returns the key in its primary encoding format
*/
public static byte[] initKey() throws Exception {
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORTHM);
//KeyGenerator.getInstance(algorithm);
kg.init(128);
//128, 192, 256
SecretKey secretKey = kg.generateKey();
return secretKey.getEncoded();
}
示例8: getAESKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
private SecretKey getAESKey(int bits) {
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(bits);
return keyGen.generateKey();
} catch (Exception e) {
return null;
}
}
示例9: initKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
/**
* 生成密鑰
*
* @param keysize
* @return
*/
public byte[] initKey(int keysize) {
KeyGenerator keyGen = null;
try {
keyGen = KeyGenerator.getInstance("DES");
keyGen.init(keysize);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
SecretKey secretKey = keyGen.generateKey();
return secretKey.getEncoded();
}
示例10: initHmacSHAKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
/**
* 初始化HmacSHA1密鑰
*
* @return
* @throws Exception
*/
public static byte[] initHmacSHAKey() throws Exception {
// 初始化KeyGenerator
KeyGenerator keyGenerator = KeyGenerator.getInstance("HMacTiger");
// 產生秘密密鑰
SecretKey secretKey = keyGenerator.generateKey();
// 獲得密鑰
return secretKey.getEncoded();
}
示例11: Encryptor
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
private Encryptor(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException
{
keyGen = KeyGenerator.getInstance(algorithm);
key = keyGen.generateKey();
cipher = Cipher.getInstance(algorithm);
}
示例12: encrypt
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
private byte[] encrypt(byte[] content, byte[] password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(content);
} catch (Exception e) {
logger.Erro(this.getClass().getName(), "加密失敗!");
}
return null;
}
示例13: generateRandomAesKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
public static String generateRandomAesKey(int keysize) throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(keysize); // for example
SecretKey secretKey = keyGen.generateKey();
byte[] encoded = secretKey.getEncoded();
return Base64.encodeToString(encoded, Base64.DEFAULT);
}
示例14: getAESKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
private static SecretKey getAESKey(int bits) {
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(bits);
return keyGen.generateKey();
} catch (Exception e) {
return null;
}
}
示例15: ReadWriteSkip
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類
ReadWriteSkip(int keyLength, int textLength, int AADLength)
throws Exception {
this.keyLength = keyLength;
this.textLength = textLength;
// init AAD
this.AAD = Helper.generateBytes(AADLength);
// init a secret Key
KeyGenerator kg = KeyGenerator.getInstance(AES, PROVIDER);
kg.init(this.keyLength);
this.key = kg.generateKey();
this.plaintext = Helper.generateBytes(textLength);
}