use of java.security.interfaces.RSAPrivateKey in project JustAndroid by chinaltz.
the class AbRsa method loadPrivateKey.
/**
* 从字符串中加载私钥
* 加载时使用的是PKCS8EncodedKeySpec(PKCS#8编码的Key指令)。
*
* @param privateKey
* @return
* @throws Exception
*/
public static PrivateKey loadPrivateKey(String privateKey) throws Exception {
try {
byte[] buffer = AbBase64.decode(privateKey);
// X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("私钥非法");
} catch (NullPointerException e) {
throw new Exception("私钥数据为空");
}
}
use of java.security.interfaces.RSAPrivateKey in project gocd by gocd.
the class HttpTestUtil method generateKeyPair.
private KeyPair generateKeyPair() {
try {
KeyPair seed = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
RSAPrivateKey privateSeed = (RSAPrivateKey) seed.getPrivate();
RSAPublicKey publicSeed = (RSAPublicKey) seed.getPublic();
KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateSeed.getModulus(), privateSeed.getPrivateExponent());
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicSeed.getModulus(), publicSeed.getPublicExponent());
return new KeyPair(fact.generatePublic(publicKeySpec), fact.generatePrivate(privateKeySpec));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.security.interfaces.RSAPrivateKey in project tech by ffyyhh995511.
the class RASUtil method loadPrivateKeyByStr.
/**
* 私钥字符串转换为私钥对象
* @param privateKeyStr
* @return
* @throws Exception
*/
public static RSAPrivateKey loadPrivateKeyByStr(String privateKeyStr) throws Exception {
try {
byte[] buffer = decryptBASE64(privateKeyStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("私钥非法");
} catch (NullPointerException e) {
throw new Exception("私钥数据为空");
}
}
use of java.security.interfaces.RSAPrivateKey in project tech by ffyyhh995511.
the class RASUtil method main.
public static void main(String[] args) {
Map<String, Object> keyMap;
try {
keyMap = initKey();
String publicKey = getPublicKey(keyMap);
System.out.println("公钥");
System.out.println(publicKey);
String privateKey = getPrivateKey(keyMap);
System.out.println("私钥");
System.out.println(privateKey);
String str = "方云鹤测试rsa加密";
System.out.println("明文数据:" + str);
RSAPublicKey loadPublicKeyByStr = loadPublicKeyByStr(publicKey);
byte[] encrypt = encrypt(loadPublicKeyByStr, str.getBytes("UTF-8"));
String encryptBASE64 = encryptBASE64(encrypt);
System.out.println("加密后的密文:" + encryptBASE64);
RSAPrivateKey loadPrivateKeyByStr = loadPrivateKeyByStr(privateKey);
byte[] decrypt = decrypt(loadPrivateKeyByStr, decryptBASE64(encryptBASE64));
String a = new String(decrypt, "UTF-8");
System.out.println("密文解密:" + a);
} catch (Exception e) {
e.printStackTrace();
}
}
use of java.security.interfaces.RSAPrivateKey in project cxf by apache.
the class JwkUtilsTest method testToPrivateRsaKeyWithoutE.
@Test
public void testToPrivateRsaKeyWithoutE() throws Exception {
RSAPrivateKey privateKey1 = (RSAPrivateKey) KeyManagementUtils.loadPrivateKey("org/apache/cxf/rs/security/jose/jws/alice.jks", "password", "alice", "password", null);
JsonWebKey jwk1 = JwkUtils.fromRSAPrivateKey(privateKey1, KeyAlgorithm.RSA_OAEP_256.getJwaName());
assertNotNull(jwk1.getProperty(JsonWebKey.RSA_PUBLIC_EXP));
jwk1.asMap().remove(JsonWebKey.RSA_PUBLIC_EXP);
try {
JwkUtils.toRSAPrivateKey(jwk1);
fail("JWK without the public exponent can not be converted to RSAPrivateKey");
} catch (JoseException ex) {
// expected
}
}
Aggregations