本文整理汇总了Java中java.security.interfaces.RSAPrivateKey类的典型用法代码示例。如果您正苦于以下问题:Java RSAPrivateKey类的具体用法?Java RSAPrivateKey怎么用?Java RSAPrivateKey使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RSAPrivateKey类属于java.security.interfaces包,在下文中一共展示了RSAPrivateKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: s_genkeys_map
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
/**
* 初始化密钥
*
* @param byte[]
* _arg 种子
* @return
* @throws Exception
*/
public static Map< String , Key > s_genkeys_map( byte[] _arg ) throws Exception {
// 实例化密钥对生成器
KeyPairGenerator key_pair_generator = KeyPairGenerator.getInstance( KEY_ALGORITHM_RSA );
// 初始化密钥对生成器
key_pair_generator.initialize( KEY_SIZE , new SecureRandom( _arg ) );
// 生成密钥对
KeyPair key_pair = key_pair_generator.generateKeyPair();
// 公钥
RSAPublicKey public_key = (RSAPublicKey) key_pair.getPublic();
// 私钥
RSAPrivateKey private_key = (RSAPrivateKey) key_pair.getPrivate();
// 封装密钥
Map< String , Key > key_map = new HashMap< String , Key >( 2 );
key_map.put( RSA_PUBLIC_KEY , public_key );
key_map.put( RSA_PRIVATE_KEY , private_key );
return key_map;
}
示例2: getSignedContent
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
public String getSignedContent(String content) {
Payload contentPayload = new Payload(content);
try {
RSASSASigner rsa = new RSASSASigner((RSAPrivateKey) clientJwk);
JWSAlgorithm alg = JWSAlgorithm.RS256;
JWSHeader header = new JWSHeader.Builder(alg)
.keyID(clientJwk.getKeyID())
.build();
JWSObject jws = new JWSObject(header, contentPayload);
jws.sign(rsa);
return jws.serialize();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例3: shouldDoRSA512SigningWithBothKeys
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
@Test
public void shouldDoRSA512SigningWithBothKeys() throws Exception {
Algorithm algorithm = Algorithm.RSA512((RSAPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA"), (RSAPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA"));
String jwtContent = String.format("%s.%s", RS512Header, auth0IssPayload);
byte[] contentBytes = jwtContent.getBytes(StandardCharsets.UTF_8);
byte[] signatureBytes = algorithm.sign(contentBytes);
String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
String token = String.format("%s.%s", jwtContent, jwtSignature);
String expectedSignature = "THIPVYzNZ1Yo_dm0k1UELqV0txs3SzyMopCyHcLXOOdgYXF4MlGvBqu0CFvgSga72Sp5LpuC1Oesj40v_QDsp2GTGDeWnvvcv_eo-b0LPSpmT2h1Ibrmu-z70u2rKf28pkN-AJiMFqi8sit2kMIp1bwIVOovPvMTQKGFmova4Xwb3G526y_PeLlflW1h69hQTIVcI67ACEkAC-byjDnnYIklA-B4GWcggEoFwQRTdRjAUpifA6HOlvnBbZZlUd6KXwEydxVS-eh1odwPjB2_sfbyy5HnLsvNdaniiZQwX7QbwLNT4F72LctYdHHM1QCrID6bgfgYp9Ij9CRX__XDEA";
assertThat(signatureBytes, is(notNullValue()));
assertThat(jwtSignature, is(expectedSignature));
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例4: LoginController
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
private LoginController() throws GeneralSecurityException
{
_log.info("Loading LoginContoller...");
_hackProtection = new HashMap<>();
_keyPairs = new ScrambledKeyPair[10];
KeyPairGenerator keygen = null;
keygen = KeyPairGenerator.getInstance("RSA");
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
keygen.initialize(spec);
//generate the initial set of keys
for (int i = 0; i < 10; i++)
{
_keyPairs[i] = new ScrambledKeyPair(keygen.generateKeyPair());
}
_log.info("Cached 10 KeyPairs for RSA communication");
testCipher((RSAPrivateKey) _keyPairs[0]._pair.getPrivate());
// Store keys for blowfish communication
generateBlowFishKeys();
}
示例5: GameServerThread
import java.security.interfaces.RSAPrivateKey; //导入依赖的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();
}
示例6: shouldThrowOnSignWhenSignatureAlgorithmDoesNotExists
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
@Test
public void shouldThrowOnSignWhenSignatureAlgorithmDoesNotExists() throws Exception {
exception.expect(SignatureGenerationException.class);
exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: some-algorithm");
exception.expectCause(isA(NoSuchAlgorithmException.class));
CryptoHelper crypto = mock(CryptoHelper.class);
when(crypto.createSignatureFor(anyString(), any(PrivateKey.class), any(byte[].class)))
.thenThrow(NoSuchAlgorithmException.class);
RSAPublicKey publicKey = mock(RSAPublicKey.class);
RSAPrivateKey privateKey = mock(RSAPrivateKey.class);
RSAKeyProvider provider = RSAAlgorithm.providerForKeys(publicKey, privateKey);
Algorithm algorithm = new RSAAlgorithm(crypto, "some-alg", "some-algorithm", provider);
algorithm.sign(RS256Header.getBytes(StandardCharsets.UTF_8));
}
示例7: shouldDoRSA256SigningWithProvidedPrivateKey
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
@Test
public void shouldDoRSA256SigningWithProvidedPrivateKey() throws Exception {
RSAKeyProvider provider = mock(RSAKeyProvider.class);
PrivateKey privateKey = readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA");
PublicKey publicKey = readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA");
when(provider.getPrivateKey()).thenReturn((RSAPrivateKey) privateKey);
when(provider.getPublicKeyById(null)).thenReturn((RSAPublicKey) publicKey);
Algorithm algorithm = Algorithm.RSA256(provider);
String jwtContent = String.format("%s.%s", RS256Header, auth0IssPayload);
byte[] contentBytes = jwtContent.getBytes(StandardCharsets.UTF_8);
byte[] signatureBytes = algorithm.sign(contentBytes);
String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
String token = String.format("%s.%s", jwtContent, jwtSignature);
assertThat(signatureBytes, is(notNullValue()));
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例8: shouldThrowWhenMacAlgorithmDoesNotExists
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
@Test
public void shouldThrowWhenMacAlgorithmDoesNotExists() throws Exception {
exception.expect(AlgorithmMismatchException.class);
exception.expectMessage("The provided Algorithm doesn't match the one defined in the JWT's Header.");
CryptoHelper crypto = mock(CryptoHelper.class);
when(crypto.verifySignatureFor(anyString(), any(PublicKey.class), any(byte[].class), any(byte[].class)))
.thenThrow(NoSuchAlgorithmException.class);
RSAPublicKey publicKey = mock(RSAPublicKey.class);
RSAPrivateKey privateKey = mock(RSAPrivateKey.class);
RSAKeyProvider provider = RSAAlgorithm.providerForKeys(publicKey, privateKey);
Algorithm algorithm = new RSAAlgorithm(crypto, "some-alg", "some-algorithm", provider);
String token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.dxXF3MdsyW-AuvwJpaQtrZ33fAde9xWxpLIg9cO2tMLH2GSRNuLAe61KsJusZhqZB9Iy7DvflcmRz-9OZndm6cj_ThGeJH2LLc90K83UEvvRPo8l85RrQb8PcanxCgIs2RcZOLygERizB3pr5icGkzR7R2y6zgNCjKJ5_NJ6EiZsGN6_nc2PRK_DbyY-Wn0QDxIxKoA5YgQJ9qafe7IN980pXvQv2Z62c3XR8dYuaXBqhthBj-AbaFHEpZapN-V-TmuLNzR2MCB6Xr7BYMuCaqWf_XU8og4XNe8f_8w9Wv5vvgqMM1KhqVpG5VdMJv4o_L4NoCROHhtUQSLRh2M9cA";
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例9: shouldThrowWhenThePublicKeyIsInvalid
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
@Test
public void shouldThrowWhenThePublicKeyIsInvalid() throws Exception {
exception.expect(AlgorithmMismatchException.class);
exception.expectMessage("The provided Algorithm doesn't match the one defined in the JWT's Header.");
CryptoHelper crypto = mock(CryptoHelper.class);
when(crypto.verifySignatureFor(anyString(), any(PublicKey.class), any(byte[].class), any(byte[].class)))
.thenThrow(InvalidKeyException.class);
RSAPublicKey publicKey = mock(RSAPublicKey.class);
RSAPrivateKey privateKey = mock(RSAPrivateKey.class);
RSAKeyProvider provider = RSAAlgorithm.providerForKeys(publicKey, privateKey);
Algorithm algorithm = new RSAAlgorithm(crypto, "some-alg", "some-algorithm", provider);
String token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.dxXF3MdsyW-AuvwJpaQtrZ33fAde9xWxpLIg9cO2tMLH2GSRNuLAe61KsJusZhqZB9Iy7DvflcmRz-9OZndm6cj_ThGeJH2LLc90K83UEvvRPo8l85RrQb8PcanxCgIs2RcZOLygERizB3pr5icGkzR7R2y6zgNCjKJ5_NJ6EiZsGN6_nc2PRK_DbyY-Wn0QDxIxKoA5YgQJ9qafe7IN980pXvQv2Z62c3XR8dYuaXBqhthBj-AbaFHEpZapN-V-TmuLNzR2MCB6Xr7BYMuCaqWf_XU8og4XNe8f_8w9Wv5vvgqMM1KhqVpG5VdMJv4o_L4NoCROHhtUQSLRh2M9cA";
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例10: initKey
import java.security.interfaces.RSAPrivateKey; //导入依赖的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;
}
示例11: initKey
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
/**
* 初始化密钥
*
* @return
* @throws Exception
*/
public static Map<String, Key> initKey() {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
// 公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, Key> keyMap = new HashMap<String, Key>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("初始化密钥出错 " + e.getMessage());
}
}
示例12: makeRSA
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
public RSAKey makeRSA(Integer keySize, KeyUse keyUse, Algorithm keyAlg, String kid) {
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(keySize);
KeyPair kp = generator.generateKeyPair();
RSAPublicKey pub = (RSAPublicKey) kp.getPublic();
RSAPrivateKey priv = (RSAPrivateKey) kp.getPrivate();
return new RSAKey.Builder(pub)
.privateKey(priv)
.keyUse(keyUse)
.algorithm(keyAlg)
.keyID(kid)
.build();
} catch (NoSuchAlgorithmException e) {
// FIXME Auto-generated catch block
e.printStackTrace();
return null;
}
}
示例13: shouldThrowOnSignWhenTheSignatureIsNotPrepared
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
@Test
public void shouldThrowOnSignWhenTheSignatureIsNotPrepared() throws Exception {
exception.expect(SignatureGenerationException.class);
exception.expectMessage("The Token's Signature couldn't be generated when signing using the Algorithm: some-algorithm");
exception.expectCause(isA(SignatureException.class));
CryptoHelper crypto = mock(CryptoHelper.class);
when(crypto.createSignatureFor(anyString(), any(PrivateKey.class), any(byte[].class)))
.thenThrow(SignatureException.class);
RSAPublicKey publicKey = mock(RSAPublicKey.class);
RSAPrivateKey privateKey = mock(RSAPrivateKey.class);
RSAKeyProvider provider = RSAAlgorithm.providerForKeys(publicKey, privateKey);
Algorithm algorithm = new RSAAlgorithm(crypto, "some-alg", "some-algorithm", provider);
algorithm.sign(RS256Header.getBytes(StandardCharsets.UTF_8));
}
示例14: getKeys
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
public static HashMap<String, String> getKeys() {
try {
HashMap<String, String> map = new HashMap<>();
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(SIGN_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
map.put(PUBLIC_KEY, new String(Base64Utils.encode(publicKey.getEncoded())));
map.put(PRIVATE_KEY, new String(Base64Utils.encode(privateKey.getEncoded())));
return map;
} catch (Exception e) {
e.printStackTrace();
throw new CustomException("生成秘钥对失败");
}
}
示例15: engineInitSign
import java.security.interfaces.RSAPrivateKey; //导入依赖的package包/类
@Override
protected void engineInitSign(
PrivateKey privateKey)
throws InvalidKeyException
{
if ( !(privateKey instanceof RSAPrivateKey) )
{
throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
}
CipherParameters param = RSAUtil.generatePrivateKeyParameter((RSAPrivateKey)privateKey);
digest.reset();
cipher.init(true, param);
}