本文整理匯總了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"));
}