本文整理汇总了Java中java.security.KeyPairGenerator.generateKeyPair方法的典型用法代码示例。如果您正苦于以下问题:Java KeyPairGenerator.generateKeyPair方法的具体用法?Java KeyPairGenerator.generateKeyPair怎么用?Java KeyPairGenerator.generateKeyPair使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.KeyPairGenerator
的用法示例。
在下文中一共展示了KeyPairGenerator.generateKeyPair方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateKey
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
/**
* The method that will create both the public and private key used to encrypt and decrypt the data.
*
* @param publicKeyOutput
* The path of where the public key will be created.
*
* @param privateKeyOutput
* The path of where the private key will be created.
*
* @return {@code true} If this operation was successful, otherwise {@code false}.
*/
public static boolean generateKey(String publicKeyOutput, String privateKeyOutput) {
try {
final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
keyGen.initialize(2048);
final KeyPair key = keyGen.generateKeyPair();
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(publicKeyOutput)))) {
dos.write(key.getPublic().getEncoded());
}
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(privateKeyOutput)))) {
dos.write(key.getPrivate().getEncoded());
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
示例2: main
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
for (int i=0; i<10000; i++) {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
KeyPair kp = kpg.generateKeyPair();
DSAPrivateKey dpk = (DSAPrivateKey)kp.getPrivate();
int len = dpk.getX().bitLength();
if (len <= 152) {
if (!use(kp)) {
String os = System.getProperty("os.name");
// Solaris bug, update the following line once it's fixed
if (os.equals("SunOS")) {
throw new IllegalStateException(
"Don't panic. This is a Solaris bug");
} else {
throw new RuntimeException("Real test failure");
}
}
break;
}
}
}
示例3: testKeyPairGeneratorDhInitializeWithParamSpec
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
@Test
public void testKeyPairGeneratorDhInitializeWithParamSpec()
throws NoSuchProviderException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException {
int testDHKeySizes[] = { 512, 1024, 2048 };
for (int i = 0; i < testDHKeySizes.length; i++) {
KeyPairGenerator kpg =
KeyPairGenerator.getInstance("DH", "wolfJCE");
DHParameterSpec spec = new DHParameterSpec(
new BigInteger(prime),
new BigInteger(base),
testDHKeySizes[i]);
kpg.initialize(spec);
KeyPair pair = kpg.generateKeyPair();
}
}
示例4: generateKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
/**
* Generates RSA KeyPair
*/
public static KeyPair generateKeyPair()
{
try
{
KeyPairGenerator keypairgenerator = KeyPairGenerator.getInstance("RSA");
keypairgenerator.initialize(1024);
return keypairgenerator.generateKeyPair();
}
catch (NoSuchAlgorithmException nosuchalgorithmexception)
{
nosuchalgorithmexception.printStackTrace();
LOGGER.error("Key pair generation failed!");
return null;
}
}
示例5: init
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
private static void init() {
try {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher = Cipher.getInstance(INSTANCE_TYPE);
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
generator.initialize(1024);
KeyPair pair = generator.generateKeyPair();
PUBLIC_KEY = org.apache.commons.codec.binary.Base64.encodeBase64String(pair.getPublic().getEncoded());
privKey = pair.getPrivate();
} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) {
e.printStackTrace();
Log.e(e.getMessage());
}
}
示例6: createNewKeys
import java.security.KeyPairGenerator; //导入方法依赖的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;
}
示例7: PaillierCryptosystemUtil
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
/**
* The constructor PaillierCryptosystemUtil is responsible for adding the
* Paillier Provider, initialise the key size and generate public and
* private keys. This is done once only here with the clear idea to be used
* the same keys for all encryption and decryption operations within the
* class. The instance of PaillierCryptosystemUtil is made just once from
* the main class, this way we assure that the homomorphic properties of
* Paillier Cryptosystem will be in use.
*
* @throws NoSuchAlgorithmException
*
*/
PaillierCryptosystemUtil() throws NoSuchAlgorithmException {
// Add dynamically the desired provider
Security.addProvider(new PaillierProvider());
// generates pairs of public and private keys
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Paillier");
// Initialise key size in bits
kpg.initialize(32);// 8 bits = 1 byte
KeyPair keyPair = kpg.generateKeyPair();
pubKey = keyPair.getPublic();
privKey = keyPair.getPrivate();
}
示例8: createKeysJBMR2
import java.security.KeyPairGenerator; //导入方法依赖的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());
}
示例9: generateKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
private KeyPair generateKeyPair(String algorithm)
throws NoSuchAlgorithmException {
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(algorithm);
keyGenerator.initialize(512);
KeyPair keyPair = keyGenerator.generateKeyPair();
return keyPair;
}
示例10: generateEphemeralKeys
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
/**
* Generates a random key pair based on a curve
* @param curve the curve to use
* @return a randomly generated KeyPair
*/
public static KeyPair generateEphemeralKeys(EllipticCurveParameters curve) {
try {
final ECParameterSpec curveParams = EllipticCurveParameters.encodeECParameterSpec(curve);
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
keyPairGenerator.initialize(curveParams);
return keyPairGenerator.generateKeyPair();
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return null;
}
示例11: initKey
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
/**
* 初始化密钥
*
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put("PublicKey", publicKey);
keyMap.put("PrivateKey", privateKey);
return keyMap;
}
示例12: genKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
/**
* 生成RSA密钥对
*/
public static void genKeyPair() {
KeyPairGenerator keyPairGenerator = null;
try {
keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
keyPairGenerator.initialize(KEYSIZE);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PRIVATE_KEY = keyPair.getPrivate();
PUBLIC_KEY = keyPair.getPublic();
}
示例13: getRSAKeyPair
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
private static KeyPair getRSAKeyPair() {
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
return keyGen.generateKeyPair();
} catch (Exception e) {
System.out.println("Unable to initialize RSA key pair.");
e.printStackTrace();
return null;
}
}
示例14: main
import java.security.KeyPairGenerator; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
keyPairGenerator.initialize(1024);
KeyPair keys = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keys.getPublic();
byte[] sigBytes = new byte[100];
Signature signature = Signature.getInstance("SHA1withDSA");
signature.initVerify(publicKey);
try {
signature.verify(sigBytes, Integer.MAX_VALUE, 1);
} catch (IllegalArgumentException ex) {
// Expected
}
}
示例15: 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);
}
}