本文整理汇总了Java中java.security.KeyPair.getPublic方法的典型用法代码示例。如果您正苦于以下问题:Java KeyPair.getPublic方法的具体用法?Java KeyPair.getPublic怎么用?Java KeyPair.getPublic使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.KeyPair
的用法示例。
在下文中一共展示了KeyPair.getPublic方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readWalletFile
import java.security.KeyPair; //导入方法依赖的package包/类
private PrivateKey readWalletFile(final char[] userPassword) throws FileNotFoundException,
IOException, ClassNotFoundException, InvalidKeySpecException,
InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException {
final WalletInformation walletInformation;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
walletInformation = (WalletInformation) ois.readObject();
} catch(IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Error with file {0}", ex.getMessage());
return null;
}
final KeyPair keyPair = Cryptography.keyPairFromWalletInformation(userPassword, walletInformation);
if (keyPair == null) {
System.out.println("The password you entered is incorrect");
return null;
}
this.publicKey = keyPair.getPublic();
return keyPair.getPrivate();
}
示例2: GameServerThread
import java.security.KeyPair; //导入方法依赖的package包/类
public GameServerThread(Socket con)
{
_connection = con;
_connectionIp = con.getInetAddress().getHostAddress();
try
{
_in = _connection.getInputStream();
_out = new BufferedOutputStream(_connection.getOutputStream());
}
catch (IOException e)
{
e.printStackTrace();
}
KeyPair pair = GameServerTable.getInstance().getKeyPair();
_privateKey = (RSAPrivateKey) pair.getPrivate();
_publicKey = (RSAPublicKey) pair.getPublic();
_blowfish = new NewCrypt("_;v.]05-31!|+-%xT!^[$\00");
start();
}
示例3: initKey
import java.security.KeyPair; //导入方法依赖的package包/类
/**
* 初始化密钥 for RSA ALGORITHM
*
* @return
* @throws Exception
*/
public static String[] initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator
.getInstance(KEY_ALGORITHM_RSA);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
// 公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
String[] publicAndPrivateKey = {
encryptBASE64(publicKey.getEncoded()),
encryptBASE64(privateKey.getEncoded())};
return publicAndPrivateKey;
}
示例4: generateKeyPair
import java.security.KeyPair; //导入方法依赖的package包/类
/**
*
* 生成KeyPair
* @return
* @throws Exception
*/
public static Map<String, Object> generateKeyPair() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(KEYSIZE);
KeyPair keyPair = keyPairGen.generateKeyPair();
// 公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
BigInteger modules = privateKey.getModulus();
Map<String, Object> keys = new HashMap<String, Object>(3);
keys.put(PUBLIC_KEY, publicKey);
keys.put(PRIVATE_KEY, privateKey);
keys.put(MODULES, modules);
return keys;
}
示例5: initKey
import java.security.KeyPair; //导入方法依赖的package包/类
/**
* 初始化密钥
*
* @return Map 密钥对儿 Map
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
// 实例化密钥对儿生成器
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
// 初始化密钥对儿生成器
keyPairGen.initialize(KEY_SIZE);
// 生成密钥对儿
KeyPair keyPair = keyPairGen.generateKeyPair();
// 公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// 封装密钥
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
示例6: recoverKeyPair_Rsa
import java.security.KeyPair; //导入方法依赖的package包/类
@Test
public void recoverKeyPair_Rsa() throws Exception {
KeyPair kp = PubkeyUtils.recoverKeyPair(RSA_KEY_PKCS8);
RSAPublicKey pubKey = (RSAPublicKey) kp.getPublic();
assertEquals(RSA_KEY_N, pubKey.getModulus());
assertEquals(RSA_KEY_E, pubKey.getPublicExponent());
}
示例7: recoverKeyPair_Ec
import java.security.KeyPair; //导入方法依赖的package包/类
@Test
public void recoverKeyPair_Ec() throws Exception {
KeyPair kp = PubkeyUtils.recoverKeyPair(EC_KEY_PKCS8);
ECPublicKey pubKey = (ECPublicKey) kp.getPublic();
assertEquals(EC_KEY_pub_x, pubKey.getW().getAffineX());
assertEquals(EC_KEY_pub_y, pubKey.getW().getAffineY());
}
示例8: initKey
import java.security.KeyPair; //导入方法依赖的package包/类
/**
* 初始化密钥
*
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(CipherType.RSA.getType());
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 公钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 私钥
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
示例9: initKey
import java.security.KeyPair; //导入方法依赖的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;
}
示例10: publicKeyTest
import java.security.KeyPair; //导入方法依赖的package包/类
@Test
public void publicKeyTest() throws Exception {
final KeyPair keyPair = generateKeyPair();
final PublicKey publicKey = keyPair.getPublic();
final byte[] encodedPublicKey = publicKey.getEncoded();
final PublicKey reconstruction = JsonUtil.bytesToPublicKey(encodedPublicKey);
assertEquals(publicKey, reconstruction);
assertEquals(publicKey.hashCode(), reconstruction.hashCode());
}
示例11: initKey
import java.security.KeyPair; //导入方法依赖的package包/类
/**
* 初始化密钥
*
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator
.getInstance(Algorithm.RSA.getType());
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 公钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 私钥
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
示例12: init
import java.security.KeyPair; //导入方法依赖的package包/类
static Offsets init(String provider, String algorithm)
throws NoSuchAlgorithmException, NoSuchProviderException,
InvalidKeyException, SignatureException {
// fill the cleartext data with random bytes
byte[] cleartext = new byte[100];
RandomFactory.getRandom().nextBytes(cleartext);
// NONEwith requires input to be of 20 bytes
int size = algorithm.contains("NONEwith") ? 20 : 100;
// create signature instance
Signature signature = Signature.getInstance(algorithm, provider);
String keyAlgo;
if (algorithm.contains("RSA")) {
keyAlgo = "RSA";
} else if (algorithm.contains("ECDSA")) {
keyAlgo = "EC";
} else if (algorithm.contains("DSA")) {
keyAlgo = "DSA";
} else {
throw new RuntimeException("Test doesn't support this signature "
+ "algorithm: " + algorithm);
}
KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);
KeyPair kp = kpg.generateKeyPair();
PublicKey pubkey = kp.getPublic();
PrivateKey privkey = kp.getPrivate();
return new Offsets(signature, pubkey, privkey, size, cleartext);
}
示例13: checkKeyPair
import java.security.KeyPair; //导入方法依赖的package包/类
private static void checkKeyPair(KeyPair kp, int pSize,
Provider provider) throws Exception {
DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();
BigInteger p = privateKey.getParams().getP();
if (p.bitLength() != pSize) {
throw new Exception(
"Invalid modulus size: " + p.bitLength() + "/" + pSize);
}
// System.out.println("P(" + pSize + "): " + p.toString());
if (!p.isProbablePrime(128)) {
throw new Exception("Good luck, the modulus is composite!");
}
DHPublicKey publicKey = (DHPublicKey)kp.getPublic();
p = publicKey.getParams().getP();
if (p.bitLength() != pSize) {
throw new Exception(
"Invalid modulus size: " + p.bitLength() + "/" + pSize);
}
BigInteger leftOpen = BigInteger.ONE;
BigInteger rightOpen = p.subtract(BigInteger.ONE);
// ignore the private key range checking on Solaris at present
if (!provider.getName().equals("SunPKCS11-Solaris")) {
BigInteger x = privateKey.getX();
if ((x.compareTo(leftOpen) <= 0) ||
(x.compareTo(rightOpen) >= 0)) {
throw new Exception(
"X outside range [2, p - 2]: x: " + x + " p: " + p);
}
}
BigInteger y = publicKey.getY();
if ((y.compareTo(leftOpen) <= 0) ||
(y.compareTo(rightOpen) >= 0)) {
throw new Exception(
"Y outside range [2, p - 2]: y: " + y + " p: " + p);
}
}
示例14: jwtRSAEncryption
import java.security.KeyPair; //导入方法依赖的package包/类
/**
* 使用RSA 算法进行加密数据
* 与解密数据
* <p/>
* 128
* RSA_OAEP - A128GCM
* 256
* RSA_OAEP_256 - A256GCM
*
* @throws Exception
*/
@Test
public void jwtRSAEncryption() throws Exception {
// RSA keyPair Generator
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
/**
* 长度 至少 1024, 建议 2048
*/
final int keySize = 2048;
keyPairGenerator.initialize(keySize);
final KeyPair keyPair = keyPairGenerator.genKeyPair();
//公钥
final RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
//私钥
final PrivateKey privateKey = keyPair.getPrivate();
//加密, 生成idToken
//加密的数据放在 JWTClaimsSet 中
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.issuer("https://myoidc.cc")
.subject("Lims")
.audience("https://one-app.com")
.notBeforeTime(new Date())
.issueTime(new Date())
.expirationTime(new Date(new Date().getTime() + 1000 * 60 * 10))
.jwtID(RandomStringUtils.random(16, true, true))
.build();
// JWEHeader header = new JWEHeader(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM);
JWEHeader header = new JWEHeader(JWEAlgorithm.RSA_OAEP_256, EncryptionMethod.A256GCM);
EncryptedJWT jwt = new EncryptedJWT(header, claimsSet);
RSAEncrypter encrypter = new RSAEncrypter(publicKey);
jwt.encrypt(encrypter);
final String idToken = jwt.serialize();
assertNotNull(idToken);
//解密
final EncryptedJWT parseJWT = EncryptedJWT.parse(idToken);
RSADecrypter decrypter = new RSADecrypter(privateKey);
parseJWT.decrypt(decrypter);
final JWTClaimsSet jwtClaimsSet = parseJWT.getJWTClaimsSet();
assertNotNull(jwtClaimsSet);
assertNotNull(jwtClaimsSet.getAudience());
}
示例15: jwtRSA
import java.security.KeyPair; //导入方法依赖的package包/类
/**
* JWT
* 使用 RSA 算法 生成 id_token
* 以及对其进行校验(verify)
* 需要公私钥对
* <p/>
* 支持算法
* RS256
* RS384
* RS512
*
* @throws Exception
*/
@Test
public void jwtRSA() throws Exception {
// RSA keyPair Generator
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
/**
* 长度 至少 1024, 建议 2048
*/
final int keySize = 2048;
keyPairGenerator.initialize(keySize);
final KeyPair keyPair = keyPairGenerator.genKeyPair();
//公钥
final RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
//私钥
final PrivateKey privateKey = keyPair.getPrivate();
//keyId
String keyId = RandomUtils.randomNumber();
//生成id_token
JWSSigner jwsSigner = new RSASSASigner(privateKey);
// JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(keyId).build();
// JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS384).keyID(keyId).build();
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS512).keyID(keyId).build();
final String payloadText = "I am MyOIDC [RSA]";
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.subject("subject")
.issuer("Issuer")
.audience("Audience")
.claim("payloadText", payloadText)
.expirationTime(new Date(new Date().getTime() + 60 * 1000))
.build();
SignedJWT signedJWT = new SignedJWT(header, claimsSet);
signedJWT.sign(jwsSigner);
final String idToken = signedJWT.serialize();
System.out.println(payloadText + " -> id_token: " + idToken);
//校验 id_token
final SignedJWT parseJWT = SignedJWT.parse(idToken);
JWSVerifier verifier = new RSASSAVerifier(publicKey);
final boolean verify = parseJWT.verify(verifier);
assertTrue(verify);
final JWTClaimsSet jwtClaimsSet = parseJWT.getJWTClaimsSet();
assertNotNull(jwtClaimsSet);
assertEquals(payloadText, jwtClaimsSet.getStringClaim("payloadText"));
}