本文整理汇总了Java中java.security.KeyPairGenerator.getInstance方法的典型用法代码示例。如果您正苦于以下问题:Java KeyPairGenerator.getInstance方法的具体用法?Java KeyPairGenerator.getInstance怎么用?Java KeyPairGenerator.getInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.KeyPairGenerator
的用法示例。
在下文中一共展示了KeyPairGenerator.getInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testKeyPairGeneratorEccMultipleInits
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
@Test
public void testKeyPairGeneratorEccMultipleInits()
throws NoSuchProviderException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
if (enabledCurves.size() > 0) {
KeyPairGenerator kpg =
KeyPairGenerator.getInstance("EC", "wolfJCE");
ECGenParameterSpec ecSpec =
new ECGenParameterSpec(enabledCurves.get(0));
kpg.initialize(ecSpec);
kpg.initialize(ecSpec);
}
}
示例2: testKeyPairGeneratorEccKeyGenAllCurves
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
@Test
public void testKeyPairGeneratorEccKeyGenAllCurves()
throws NoSuchProviderException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
/* try generating keys for all supported curves */
for (int i = 0; i < enabledCurves.size(); i++) {
KeyPairGenerator kpg =
KeyPairGenerator.getInstance("EC", "wolfJCE");
ECGenParameterSpec ecSpec =
new ECGenParameterSpec(enabledCurves.get(i));
kpg.initialize(ecSpec);
KeyPair kp = kpg.generateKeyPair();
}
}
示例3: testSignature4
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
/**
* Test signature with a SOAP message and generate ECDSA keys during the
* test.
*/
public void testSignature4() {
String data = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:id=\"http://x-road.eu/xsd/identifiers\" xmlns:xrd=\"http://x-road.eu/xsd/xroad.xsd\"><SOAP-ENV:Header><xrd:client id:objectType=\"SUBSYSTEM\"><id:xRoadInstance>FI</id:xRoadInstance><id:memberClass>GOV</id:memberClass><id:memberCode>MEMBER1</id:memberCode><id:subsystemCode>subsystem</id:subsystemCode></xrd:client><xrd:service id:objectType=\"SERVICE\"><id:xRoadInstance>FI</id:xRoadInstance><id:memberClass>COM</id:memberClass><id:memberCode>MEMBER2</id:memberCode><id:subsystemCode>subsystem</id:subsystemCode><id:serviceCode>getRandom</id:serviceCode><id:serviceVersion>v1</id:serviceVersion></xrd:service><xrd:userId>EE1234567890</xrd:userId><xrd:id>ID-1234567890</xrd:id><xrd:protocolVersion>4.0</xrd:protocolVersion><xrd:requestHash algorithmId=\"SHA-512\">ZPbWPAOcJxzE81EmSk//R3DUQtqwMcuMMF9tsccJypdNcukzICQtlhhr3a/bTmexDrn8e/BrBVyl2t0ni/cUvw==</xrd:requestHash></SOAP-ENV:Header><SOAP-ENV:Body><ns1:getRandomResponse xmlns:ns1=\"http://producer.x-road.ee\"><request><data>1234567890 Каллио</data></request><response><data>9876543210</data></response></ns1:getRandomResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>";
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(256, new SecureRandom());
KeyPair keyPair = keyGen.generateKeyPair();
String signature = CryptoHelper.createSignature(keyPair.getPrivate(), data, "SHA512withECDSA");
assertEquals(true, CryptoHelper.verifySignature(keyPair.getPublic(), data, signature, "SHA512withECDSA"));
} catch (Exception e) {
fail(e.getMessage());
}
}
示例4: createRSA256KeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
private KeyPair createRSA256KeyPair() {
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048);
return generator.generateKeyPair();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
示例5: failsWhenUidIsTooLong
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
@Test
public void failsWhenUidIsTooLong() throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(512);
KeyPair keys = keyGen.genKeyPair();
FixedClock clock = new FixedClock(2002L);
FirebaseTokenFactory tokenFactory = new FirebaseTokenFactory(FACTORY, clock);
thrown.expect(IllegalStateException.class);
tokenFactory.createSignedCustomAuthTokenForUser(
Strings.repeat("a", 129), ISSUER, keys.getPrivate());
}
示例6: generateUserKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
/**
* Generates a random user key pair.
*
* @param version The encryption version for which the key pair should be created.
* @param password The password which should be used to secure the private key.
*
* @return The generated user key pair.
*
* @throws InvalidKeyPairException If the version for the user key pair is not supported.
* @throws InvalidPasswordException If the password to secure the private key is invalid.
* @throws CryptoSystemException If a unknown error occurred.
*/
public static UserKeyPair generateUserKeyPair(String version, String password)
throws InvalidKeyPairException, InvalidPasswordException, CryptoSystemException {
validateUserKeyPairVersion(version);
validatePassword(password);
KeyPair keyPair;
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
keyPair = keyGen.generateKeyPair();
} catch (NoSuchAlgorithmException e) {
throw new CryptoSystemException("Could not generate RSA key pair. Algorithm is " +
"missing.", e);
}
String privateKeyString = encryptPrivateKey(keyPair.getPrivate(), password);
String publicKeyString = getStringFromPublicKey(keyPair.getPublic());
UserPrivateKey userPrivateKey = new UserPrivateKey();
userPrivateKey.setVersion(version);
userPrivateKey.setPrivateKey(privateKeyString);
UserPublicKey userPublicKey = new UserPublicKey();
userPublicKey.setVersion(version);
userPublicKey.setPublicKey(publicKeyString);
UserKeyPair userKeyPair = new UserKeyPair();
userKeyPair.setUserPrivateKey(userPrivateKey);
userKeyPair.setUserPublicKey(userPublicKey);
return userKeyPair;
}
示例7: generateRSAKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
/**
* 随机生成RSA密钥对
*
* @param keyLength 密钥长度,范围:512~2048<br>
* 一般1024
*/
public static KeyPair generateRSAKeyPair(int keyLength) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
kpg.initialize(keyLength);
return kpg.genKeyPair();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
示例8: buildKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
private KeyPair buildKeyPair() {
try {
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
return keyGen.generateKeyPair();
} catch (final NoSuchAlgorithmException e) {
throw new TechnicalException(e);
}
}
示例9: generateKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
public void generateKeyPair() throws Exception {
KeyPairGenerator psrssKeyGen = KeyPairGenerator.getInstance("PSRSS", new WPProvider());
psrssKeyGen.initialize(512);
KeyPair keyPair = psrssKeyGen.generateKeyPair();
assertEquals(512, ((PSRSSPrivateKey) keyPair.getPrivate()).getKey().bitLength());
assertEquals(512, ((PSRSSPublicKey) keyPair.getPublic()).getKey().bitLength());
psrssKeyGen.initialize(513);
keyPair = psrssKeyGen.generateKeyPair();
assertEquals(513, ((PSRSSPrivateKey) keyPair.getPrivate()).getKey().bitLength());
assertEquals(513, ((PSRSSPublicKey) keyPair.getPublic()).getKey().bitLength());
}
示例10: checkParam
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
private static void checkParam(AlgorithmParameters param,
DSAGenParameterSpec genParam) throws InvalidParameterSpecException,
NoSuchAlgorithmException, NoSuchProviderException,
InvalidAlgorithmParameterException {
String algorithm = param.getAlgorithm();
if (!algorithm.equalsIgnoreCase(ALGORITHM_NAME)) {
throw new RuntimeException(
"Unexpected type of parameters: " + algorithm);
}
DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
int valueL = spec.getP().bitLength();
int strengthP = genParam.getPrimePLength();
if (strengthP != valueL) {
System.out.printf("P: Expected %d but actual %d%n", strengthP,
valueL);
throw new RuntimeException("Wrong P strength");
}
int valueN = spec.getQ().bitLength();
int strengthQ = genParam.getSubprimeQLength();
if (strengthQ != valueN) {
System.out.printf("Q: Expected %d but actual %d%n", strengthQ,
valueN);
throw new RuntimeException("Wrong Q strength");
}
if (genParam.getSubprimeQLength() != genParam.getSeedLength()) {
System.out.println("Defaut seed length should be the same as Q.");
throw new RuntimeException("Wrong seed length");
}
// use the parameters to generate real DSA keys
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_NAME,
PROVIDER_NAME);
keyGen.initialize(spec);
keyGen.generateKeyPair();
}
示例11: generateKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
public static BrowserIDKeyPair generateKeyPair(final int keysize) throws NoSuchAlgorithmException {
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(keysize);
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
return new BrowserIDKeyPair(new RSASigningPrivateKey(privateKey), new RSAVerifyingPublicKey(publicKey));
}
示例12: generateKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
public static KeyPair generateKeyPair(String algorithm) throws NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
keyGen.initialize(1024);
return keyGen.genKeyPair();
}
示例13: main
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
Locale reservedLocale = Locale.getDefault();
try {
// first test if HumanInputStream really acts like a human being
HumanInputStream.test();
KeyToolTest t = new KeyToolTest();
if (System.getProperty("file") != null) {
t.sqeTest();
t.testAll();
t.i18nTest();
t.v3extTest("RSA");
t.v3extTest("DSA");
boolean testEC = true;
try {
KeyPairGenerator.getInstance("EC");
} catch (NoSuchAlgorithmException nae) {
testEC = false;
}
if (testEC) t.v3extTest("EC");
}
if (System.getProperty("nss") != null) {
t.srcP11Arg = NSS_SRC_P11_ARG;
t.p11Arg = NSS_P11_ARG;
t.testPKCS11();
// FAIL:
// 1. we still don't have srcprovidername yet
// 2. cannot store privatekey into NSS keystore
// java.security.KeyStoreException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_TEMPLATE_INCOMPLETE.
//t.testPKCS11ImportKeyStore();
t.i18nPKCS11Test();
//FAIL: currently PKCS11-NSS does not support 2 NSS KeyStores to be loaded at the same time
//t.sszzTest();
}
if (System.getProperty("solaris") != null) {
// For Solaris Cryptography Framework
t.srcP11Arg = SUN_SRC_P11_ARG;
t.p11Arg = SUN_P11_ARG;
t.testPKCS11();
t.testPKCS11ImportKeyStore();
t.i18nPKCS11Test();
}
System.out.println("Test pass!!!");
} finally {
// restore the reserved locale
Locale.setDefault(reservedLocale);
}
}
示例14: main
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
if (initSecmod() == false) {
return;
}
String configName = BASE + SEP + "nsscrypto.cfg";
Provider p = getSunPKCS11(configName);
if (args.length > 1 && "sm".equals(args[0])) {
System.setProperty("java.security.policy",
BASE + File.separator + args[1]);
System.setSecurityManager(new SecurityManager());
}
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
KeyPair kp = kpg.generateKeyPair();
System.out.println(kp.getPublic());
System.out.println(kp.getPrivate());
byte[] data = generateData(2048);
Signature sig = Signature.getInstance("SHA1withRSA", p);
sig.initSign(kp.getPrivate());
sig.update(data);
byte[] s = sig.sign();
System.out.println("signature: " + toString(s));
sig.initVerify(kp.getPublic());
sig.update(data);
boolean ok = sig.verify(s);
if (ok == false) {
throw new Exception("Signature verification failed");
}
System.out.println("OK");
}
示例15: testECDHKeyAgreement
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
@Test
public void testECDHKeyAgreement()
throws NoSuchProviderException, NoSuchAlgorithmException,
InvalidParameterSpecException, InvalidKeyException,
InvalidAlgorithmParameterException {
/* initialize key pair generator */
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", "wolfJCE");
ECGenParameterSpec ecsp = new ECGenParameterSpec("secp256r1");
keyGen.initialize(ecsp);
KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", "wolfJCE");
KeyAgreement bKeyAgree = KeyAgreement.getInstance("ECDH", "wolfJCE");
KeyPair aPair = keyGen.generateKeyPair();
KeyPair bPair = keyGen.generateKeyPair();
aKeyAgree.init(aPair.getPrivate());
bKeyAgree.init(bPair.getPrivate());
aKeyAgree.doPhase(bPair.getPublic(), true);
bKeyAgree.doPhase(aPair.getPublic(), true);
byte secretA[] = aKeyAgree.generateSecret();
byte secretB[] = bKeyAgree.generateSecret();
assertArrayEquals(secretA, secretB);
/* now, try reusing the A object without calling init() again */
KeyAgreement cKeyAgree = KeyAgreement.getInstance("ECDH", "wolfJCE");
KeyPair cPair = keyGen.generateKeyPair();
cKeyAgree.init(cPair.getPrivate());
aKeyAgree.doPhase(cPair.getPublic(), true);
cKeyAgree.doPhase(aPair.getPublic(), true);
byte secretA2[] = aKeyAgree.generateSecret();
byte secretC[] = cKeyAgree.generateSecret();
assertArrayEquals(secretA2, secretC);
}