本文整理汇总了Java中android.security.KeyPairGeneratorSpec类的典型用法代码示例。如果您正苦于以下问题:Java KeyPairGeneratorSpec类的具体用法?Java KeyPairGeneratorSpec怎么用?Java KeyPairGeneratorSpec使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
KeyPairGeneratorSpec类属于android.security包,在下文中一共展示了KeyPairGeneratorSpec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createKeys
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
/**
* Creates a public and private key and stores it using the AndroidKeyStore,
* so that only this application will be able to access the keys.
*/
@SuppressWarnings("deprecation")
public void createKeys() throws Exception {
KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
keyStore.load(null);
if (keyStore.containsAlias(alias)) {
Log.d(TAG, "[containsAlias]");
return;
}
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 30);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
.setAlias(alias)
.setSubject(new X500Principal("CN=" + alias))
.setSerialNumber(BigInteger.TEN)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator generator = KeyPairGenerator.getInstance(TYPE_RSA, ANDROID_KEY_STORE);
generator.initialize(spec);
KeyPair keyPair = generator.generateKeyPair();
Log.d(TAG, "Public Key is: " + keyPair.getPublic().toString());
}
示例2: generateAsymmetricKeyPair
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
private static void generateAsymmetricKeyPair() throws SecureStorageException {
try {
if (isRTL()) {
Locale.setDefault(Locale.US);
}
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 99);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context.get())
.setAlias(KEY_ALIAS)
.setSubject(new X500Principal(KEY_X500PRINCIPAL))
.setSerialNumber(BigInteger.TEN)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator generator
= KeyPairGenerator.getInstance(KEY_ENCRYPTION_ALGORITHM, KEY_KEYSTORE_NAME);
generator.initialize(spec);
generator.generateKeyPair();
} catch (Exception e) {
throw new SecureStorageException(e.getMessage(), e, KEYSTORE_EXCEPTION);
}
}
示例3: createKeysJBMR2
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
static void createKeysJBMR2(Context context, String alias)
throws NoSuchProviderException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
Calendar start = new GregorianCalendar();
Calendar end = new GregorianCalendar();
end.add(Calendar.YEAR, 30);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
// You'll use the alias later to retrieve the key. It's a key
// for the key!
.setAlias(alias)
.setSubject(new X500Principal("CN=" + alias))
.setSerialNumber(BigInteger.valueOf(Math.abs(alias.hashCode())))
// Date range of validity for the generated pair.
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance(SecurityConstants.TYPE_RSA,
SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
kpGenerator.initialize(spec);
KeyPair kp = kpGenerator.generateKeyPair();
Log.d(TAG, "Public Key is: " + kp.getPublic().toString());
}
示例4: generateKeyPair
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static void generateKeyPair(Context context, String alias)
throws GeneralSecurityException {
final Calendar start = new GregorianCalendar();
final Calendar end = new GregorianCalendar();
end.add(Calendar.YEAR, 100);
final KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
final KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
.setAlias(alias)
.setSubject(new X500Principal("CN=" + alias))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
gen.initialize(spec);
gen.generateKeyPair();
}
示例5: createNewKeys
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
private static KeyPair createNewKeys(Context ctx, String alias) {
KeyPair keyPair = null;
try {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 1);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx)
.setAlias(alias)
.setSubject(new X500Principal("CN=" + alias))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", keyStoreInstance);
generator.initialize(spec);
keyPair = generator.generateKeyPair();
} catch (Exception e) {
Toast.makeText(ctx, "Exception " + e.getMessage() + " occured", Toast.LENGTH_LONG).show();
Log.e(TAG, Log.getStackTraceString(e));
}
return keyPair;
}
示例6: generateNewKey
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
private void generateNewKey(@NonNull Context context, @NonNull String alias) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
Calendar notBefore = Calendar.getInstance();
Calendar notAfter = Calendar.getInstance();
notAfter.add(Calendar.YEAR, 1);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
.setAlias(alias)
.setSubject(new X500Principal("CN=zerokit"))
.setSerialNumber(BigInteger.ONE)
.setStartDate(notBefore.getTime())
.setEndDate(notAfter.getTime())
.build();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", ANDROID_KEYSTORE);
generator.initialize(spec);
generator.generateKeyPair();
}
示例7: initializeKeystore
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
protected void initializeKeystore() throws KeyStoreException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
// Create new key if needed
if (!keyStore.containsAlias(keyAlias)) {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 1);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
.setAlias(keyAlias)
.setSubject(new X500Principal("CN=Sample Name, O=Android Authority"))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator generator = KeyPairGenerator.getInstance(KEYPAIR_ALGO, ANDROID_KEY_STORE);
generator.initialize(spec);
generator.generateKeyPair();
}
}
示例8: createKeysJBMR2
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static void createKeysJBMR2(Context context, String alias)
throws NoSuchProviderException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
Calendar start = new GregorianCalendar();
Calendar end = new GregorianCalendar();
end.add(Calendar.YEAR, 30);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
// You'll use the alias later to retrieve the key. It's a key
// for the key!
.setAlias(alias)
.setSubject(new X500Principal("CN=" + alias))
.setSerialNumber(BigInteger.valueOf(Math.abs(alias.hashCode())))
// Date range of validity for the generated pair.
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance(SecurityConstants.TYPE_RSA,
SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
kpGenerator.initialize(spec);
KeyPair kp = kpGenerator.generateKeyPair();
Log.d(TAG, "Public Key is: " + kp.getPublic().toString());
}
示例9: createNewKey
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
public void createNewKey() {
try {
//new key if needed
if (!keyStore.containsAlias(alias)) {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM, KEY_PROVIDER);
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 1);
kpg.initialize(new KeyPairGeneratorSpec.Builder(mContext)
.setAlias(alias)
.setSubject(new X500Principal(String.format("CN=%s, OU=%s", alias, mContext.getPackageName())))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build()
);
KeyPair keyPair = kpg.generateKeyPair();
}
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
示例10: shouldThrowOnNoSuchProviderErrorWhenTryingToObtainRSAKeys
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@Test
public void shouldThrowOnNoSuchProviderErrorWhenTryingToObtainRSAKeys() throws Exception {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
exception.expect(KeyException.class);
exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");
PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);
PowerMockito.mockStatic(KeyPairGenerator.class);
PowerMockito.when(KeyPairGenerator.getInstance(ALGORITHM_RSA, ANDROID_KEY_STORE))
.thenThrow(new NoSuchProviderException());
cryptoUtil.getRSAKeyEntry();
}
示例11: shouldThrowOnNoSuchAlgorithmErrorWhenTryingToObtainRSAKeys
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@Test
public void shouldThrowOnNoSuchAlgorithmErrorWhenTryingToObtainRSAKeys() throws Exception {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
exception.expect(KeyException.class);
exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");
PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);
PowerMockito.mockStatic(KeyPairGenerator.class);
PowerMockito.when(KeyPairGenerator.getInstance(ALGORITHM_RSA, ANDROID_KEY_STORE))
.thenThrow(new NoSuchAlgorithmException());
cryptoUtil.getRSAKeyEntry();
}
示例12: createNewKeys
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
private void createNewKeys(Context context, String key) {
String alias = key;
try {
if (!keyStore.containsAlias(alias)) {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 1);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context.getApplicationContext())
.setAlias(alias)
.setSubject(new X500Principal("CN=Sample Name, O=Android Authority"))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
generator.initialize(spec);
KeyPair keyPair = generator.generateKeyPair();
}
} catch (Exception e) {
}
}
示例13: createNewKeys
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
public void createNewKeys(View view) {
String alias = aliasText.getText().toString();
try {
// Create new key if needed
if (!keyStore.containsAlias(alias)) {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 1);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(this)
.setAlias(alias)
.setSubject(new X500Principal("CN=Sample Name, O=Android Authority"))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
generator.initialize(spec);
KeyPair keyPair = generator.generateKeyPair();
}
} catch (Exception e) {
Toast.makeText(this, "Exception " + e.getMessage() + " occured", Toast.LENGTH_LONG).show();
Log.e(TAG, Log.getStackTraceString(e));
}
refreshKeys();
}
示例14: generateKeyPair
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static void generateKeyPair(Context context, String alias)
throws GeneralSecurityException {
final Calendar start = new GregorianCalendar();
final Calendar end = new GregorianCalendar();
end.add(Calendar.YEAR, 100);
final KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
.setAlias(alias)
.setSubject(new X500Principal("CN=" + alias))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
final KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
gen.initialize(spec);
gen.generateKeyPair();
}
示例15: generateRSAKey
import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.KITKAT)
@SuppressWarnings("deprecation")
private KeyPair generateRSAKey(String keystoreAlias) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
fixPrng();
Calendar start = new GregorianCalendar();
Calendar end = new GregorianCalendar();
end.add(Calendar.YEAR, 10);
KeyPairGeneratorSpec spec =
new KeyPairGeneratorSpec.Builder(context)
.setAlias(keystoreAlias)
.setSubject(new X500Principal("CN=" + keystoreAlias))
.setSerialNumber(BigInteger.valueOf(1337))
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.setKeySize(RSA.KEY_LENGTH_BITS)
.build();
KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance(RSA.CIPHER, KeyStoreConstants.PROVIDER);
kpGenerator.initialize(spec);
KeyPair keyPair = kpGenerator.generateKeyPair();
storePublicKey(keyPair.getPublic());
return keyPair;
}