本文整理汇总了Java中java.security.interfaces.RSAPublicKey类的典型用法代码示例。如果您正苦于以下问题:Java RSAPublicKey类的具体用法?Java RSAPublicKey怎么用?Java RSAPublicKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RSAPublicKey类属于java.security.interfaces包,在下文中一共展示了RSAPublicKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: s_genkeys_map
import java.security.interfaces.RSAPublicKey; //导入依赖的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: initialize
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
/**
* Initialize this component by building up the consumer for JWT using the
* pre-configured secret
*
* @param rsaPublicKey the RSA public key to be used for verification.
*
* @return Fluent interface.
*/
SSOFacade initialize(final RSAPublicKey rsaPublicKey) {
if (logger.isDebugEnabled()) {
logger.debug("Initializing single-sign-on manager SSOFacade. ");
}
Objects.requireNonNull(rsaPublicKey);
// Build up the algorithm constraints by only accepting RSA_USING_SHA256.
final AlgorithmConstraints algorithmConstraints = new AlgorithmConstraints(
AlgorithmConstraints.ConstraintType.WHITELIST, AlgorithmIdentifiers.RSA_USING_SHA256);
this.jwtConsumer = new JwtConsumerBuilder()
.setJwsAlgorithmConstraints(algorithmConstraints)
.setSkipDefaultAudienceValidation()
.setVerificationKey(rsaPublicKey)
.setRequireExpirationTime()
.setRequireNotBefore()
.setRequireIssuedAt()
.build();
return this;
}
示例3: GameServerThread
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
public GameServerThread(Socket con)
{
_connection = con;
_connectionIp = con.getInetAddress().getHostAddress();
try
{
_in = _connection.getInputStream();
_out = new BufferedOutputStream(_connection.getOutputStream());
}
catch (IOException e)
{
_log.warning(getClass().getSimpleName() + ": " + e.getMessage());
}
final KeyPair pair = GameServerTable.getInstance().getKeyPair();
_privateKey = (RSAPrivateKey) pair.getPrivate();
_publicKey = (RSAPublicKey) pair.getPublic();
_blowfish = new NewCrypt("_;v.]05-31!|+-%xT!^[$\00");
setName(getClass().getSimpleName() + "-" + getId() + "@" + _connectionIp);
start();
}
示例4: extractOpenSSHPublic
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
/**
* @param pair KeyPair to convert to an OpenSSH public key
* @return OpenSSH-encoded pubkey
*/
public static byte[] extractOpenSSHPublic(KeyPair pair) {
try {
PublicKey pubKey = pair.getPublic();
if (pubKey instanceof RSAPublicKey) {
return RSASHA1Verify.encodeSSHRSAPublicKey((RSAPublicKey) pubKey);
} else if (pubKey instanceof DSAPublicKey) {
return DSASHA1Verify.encodeSSHDSAPublicKey((DSAPublicKey) pubKey);
} else if (pubKey instanceof ECPublicKey) {
return ECDSASHA2Verify.encodeSSHECDSAPublicKey((ECPublicKey) pubKey);
} else if (pubKey instanceof EdDSAPublicKey) {
return Ed25519Verify.encodeSSHEd25519PublicKey((EdDSAPublicKey) pubKey);
} else {
return null;
}
} catch (IOException e) {
return null;
}
}
示例5: main
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
String FILE = "newsize7-ks";
new File(FILE).delete();
sun.security.tools.keytool.Main.main(("-debug -genkeypair -keystore " +
FILE +
" -alias a -dname cn=c -storepass changeit" +
" -keypass changeit -keyalg rsa").split(" "));
KeyStore ks = KeyStore.getInstance("JKS");
try (FileInputStream fin = new FileInputStream(FILE)) {
ks.load(fin, "changeit".toCharArray());
}
Files.delete(Paths.get(FILE));
RSAPublicKey r = (RSAPublicKey)ks.getCertificate("a").getPublicKey();
if (r.getModulus().bitLength() != 2048) {
throw new Exception("Bad keysize");
}
X509Certificate x = (X509Certificate)ks.getCertificate("a");
if (!x.getSigAlgName().equals("SHA256withRSA")) {
throw new Exception("Bad sigalg");
}
}
示例6: shouldDoRSA512SigningWithBothKeys
import java.security.interfaces.RSAPublicKey; //导入依赖的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);
}
示例7: shouldThrowWhenTheSignatureIsNotPrepared
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
@Test
public void shouldThrowWhenTheSignatureIsNotPrepared() 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(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);
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);
}
示例8: BlowFishKey
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
/**
* @param blowfishKey
* @param publicKey
*/
public BlowFishKey(byte[] blowfishKey, RSAPublicKey publicKey)
{
writeC(0x00);
byte[] encrypted =null;
try
{
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/nopadding");
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
encrypted = rsaCipher.doFinal(blowfishKey);
}
catch(GeneralSecurityException e)
{
_log.severe("Error While encrypting blowfish key for transmision (Crypt error)");
e.printStackTrace();
}
writeD(encrypted.length);
writeB(encrypted);
}
示例9: shouldDoRSA512SigningWithProvidedPrivateKey
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
@Test
public void shouldDoRSA512SigningWithProvidedPrivateKey() 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.RSA512(provider);
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);
assertThat(signatureBytes, is(notNullValue()));
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例10: shouldThrowWhenMacAlgorithmDoesNotExists
import java.security.interfaces.RSAPublicKey; //导入依赖的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);
}
示例11: equals
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
public boolean equals(Object o)
{
if (o == this)
{
return true;
}
if (!(o instanceof RSAPublicKey))
{
return false;
}
RSAPublicKey key = (RSAPublicKey)o;
return getModulus().equals(key.getModulus())
&& getPublicExponent().equals(key.getPublicExponent());
}
示例12: initKey
import java.security.interfaces.RSAPublicKey; //导入依赖的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;
}
示例13: decodeRSAPublicKey
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
public static RSAPublicKey decodeRSAPublicKey(String key, ExceptionInterceptor interceptor) throws SQLException {
try {
if (key == null) {
throw new SQLException("key parameter is null");
}
int offset = key.indexOf("\n") + 1;
int len = key.indexOf("-----END PUBLIC KEY-----") - offset;
// TODO: use standard decoders with Java 6+
byte[] certificateData = Base64Decoder.decode(key.getBytes(), offset, len);
X509EncodedKeySpec spec = new X509EncodedKeySpec(certificateData);
KeyFactory kf = KeyFactory.getInstance("RSA");
return (RSAPublicKey) kf.generatePublic(spec);
} catch (Exception ex) {
throw SQLError.createSQLException("Unable to decode public key", SQLError.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);
}
}
示例14: encryptByPublicKey
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
/**
* 公钥加密
*
* @param data
* @param publicKey
* @return
* @throws Exception
*/
public static String encryptByPublicKey(String data, RSAPublicKey publicKey)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 模长
int key_len = publicKey.getModulus().bitLength() / 8;
// 加密数据长度 <= 模长-11
String[] datas = splitString(data, key_len - 11);
String mi = "";
//如果明文长度大于模长-11则要分组加密
for (String s : datas) {
mi += bcd2Str(cipher.doFinal(s.getBytes()));
}
return mi;
}
示例15: providerForKeys
import java.security.interfaces.RSAPublicKey; //导入依赖的package包/类
static RSAKeyProvider providerForKeys(final RSAPublicKey publicKey, final RSAPrivateKey privateKey) {
if (publicKey == null && privateKey == null) {
throw new IllegalArgumentException("Both provided Keys cannot be null.");
}
return new RSAKeyProvider() {
@Override
public RSAPublicKey getPublicKeyById(String keyId) {
return publicKey;
}
@Override
public RSAPrivateKey getPrivateKey() {
return privateKey;
}
@Override
public String getPrivateKeyId() {
return null;
}
};
}