Refine search
- Common ways to obtain RSAPublicKey
private void myMethod () {RSAPublicKey r =
KeyPair keyPair;(RSAPublicKey) keyPair.getPublic()
KeyFactory keyFactory;KeySpec keySpec;(RSAPublicKey) keyFactory.generatePublic(keySpec)
X509Certificate x509Certificate;(RSAPublicKey) x509Certificate.getPublicKey()
- Smart code suggestions by Codota
}
public static String decrypt(PublicKey publicKey, String cipherText) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); try { cipher.init(Cipher.DECRYPT_MODE, publicKey); } catch (InvalidKeyException e) { // 因为 IBM JDK 不支持私钥加密, 公钥解密, 所以要反转公私钥 // 也就是说对于解密, 可以通过公钥的参数伪造一个私钥对象欺骗 IBM JDK RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey; RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent()); Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec); cipher = Cipher.getInstance("RSA"); //It is a stateful object. so we need to get new one. cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey); } if (cipherText == null || cipherText.length() == 0) { return cipherText; } byte[] cipherBytes = Base64.base64ToByteArray(cipherText); byte[] plainBytes = cipher.doFinal(cipherBytes); return new String(plainBytes); }
String generateRSAPublicKey() { RSAPublicKey pub = (RSAPublicKey) cert.getPublicKey(); StringBuilder builder = new StringBuilder(); append(builder, NLS.str("certificate.serialPubKeyType"), pub.getAlgorithm()); append(builder, NLS.str("certificate.serialPubKeyExponent"), pub.getPublicExponent().toString(10)); append(builder, NLS.str("certificate.serialPubKeyModulusSize"), Integer.toString( pub.getModulus().toString(2).length())); append(builder, NLS.str("certificate.serialPubKeyModulus"), pub.getModulus().toString(10)); return builder.toString(); }
@Override protected void writePubKeyContentsIntoBuffer(PublicKey pk, Buffer<?> buf) { final RSAPublicKey rsaKey = (RSAPublicKey) pk; buf.putMPInt(rsaKey.getPublicExponent()) // e .putMPInt(rsaKey.getModulus()); // n }
String modulus = publicKey.getModulus().toString(); String public_exponent = publicKey.getPublicExponent().toString();
private static void test() { Pair<RSAPublicKey, RSAPrivateKey> pair = RSAUtils.genKeyPair(RAS_KEY_SIZE); //生成公钥和私钥 RSAPublicKey publicKey = pair.key; RSAPrivateKey privateKey = pair.value; //模 String modulus = publicKey.getModulus().toString(); //公钥指数 String public_exponent = publicKey.getPublicExponent().toString(); //私钥指数 String private_exponent = privateKey.getPrivateExponent().toString(); //明文 byte[] ming = "123456789".getBytes(Constants.UTF_8); System.out.println("明文:" + new String(ming, Constants.UTF_8)); //使用模和指数生成公钥和私钥 RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent); RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); System.out.println("privateKey=" + priKey); System.out.println("publicKey=" + pubKey); //加密后的密文 byte[] mi = RSAUtils.encryptByPublicKey(ming, pubKey); System.out.println("密文:" + new String(mi, Constants.UTF_8)); //解密后的明文 ming = RSAUtils.decryptByPrivateKey(mi, priKey); System.out.println("解密:" + new String(ming, Constants.UTF_8)); }
@Override public Map<String, Object> getJwkMap() { Map<String, Object> result = new HashMap<>(); result.put("alg", this.algorithm()); result.put("value", this.verifierKey); //new values per OpenID and JWK spec result.put("use", JsonWebKey.KeyUse.sig.name()); result.put("kid", this.keyId); result.put("kty", RSA.name()); RSAPublicKey rsaKey = (RSAPublicKey) parseKeyPair(verifierKey).getPublic(); if (rsaKey != null) { java.util.Base64.Encoder encoder = java.util.Base64.getUrlEncoder().withoutPadding(); String n = encoder.encodeToString(rsaKey.getModulus().toByteArray()); String e = encoder.encodeToString(rsaKey.getPublicExponent().toByteArray()); result.put("n", n); result.put("e", e); } return result; }
n = pubkey.getModulus(); r = BigInteger.ZERO.setBit(KEY_LENGTH_WORDS * 32); rr = r.modPow(BigInteger.valueOf(2), n); bbuf.putInt(i); bbuf.putInt(pubkey.getPublicExponent().intValue()); return bbuf.array();
static JsonObject getJwk(PublicKey publicKey, String algHeader) { if (publicKey instanceof RSAPublicKey) { RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey; return Json.createObjectBuilder() .add(EXPONENT, base64UrlEncode(rsaPublicKey.getPublicExponent().toByteArray())) .add(KEY_TYPE, "RSA") .add(MODULUS, base64UrlEncode(modulusToByteArray(rsaPublicKey.getModulus()))) .build(); } else if (publicKey instanceof ECPublicKey) { ECPublicKey ecPublicKey = (ECPublicKey) publicKey; int fieldSize = ecPublicKey.getParams().getCurve().getField().getFieldSize(); return Json.createObjectBuilder() .add(CURVE, getCurveParameterFromAlgHeader(algHeader)) .add(KEY_TYPE, "EC") .add(X_COORDINATE, base64UrlEncode(coordinateToByteArray(fieldSize, ecPublicKey.getW().getAffineX()))) .add(Y_COORDINATE, base64UrlEncode(coordinateToByteArray(fieldSize, ecPublicKey.getW().getAffineY()))) .build(); } else { throw acme.unsupportedAcmeAccountPublicKeyType(publicKey.getAlgorithm()); } }
@Test public void create_key_from_pem_string() { KeyInfo keyInfo = KeyInfoBuilder.build("id", sampleRsaPrivateKey, ISSUER); assertEquals("RSA", keyInfo.type()); assertNotNull(keyInfo.getVerifier()); JsonWebKey key = new JsonWebKey(KeyInfoBuilder.build("id", sampleRsaPrivateKey, ISSUER).getJwkMap()).setKid("id"); assertEquals(RSA, key.getKty()); assertEquals("RSA", key.getKeyProperties().get("kty")); assertEquals("id", key.getKid()); assertEquals(sig, key.getUse()); assertEquals("sig", key.getKeyProperties().get("use")); assertNotNull(key.getValue()); PublicKey pk = parseKeyPair(keyInfo.verifierKey()).getPublic(); BigInteger exponent = ((RSAPublicKey) pk).getPublicExponent(); BigInteger modulus = ((RSAPublicKey) pk).getModulus(); java.util.Base64.Encoder encoder = java.util.Base64.getUrlEncoder().withoutPadding(); assertEquals(encoder.encodeToString(exponent.toByteArray()), key.getKeyProperties().get("e")); assertEquals(encoder.encodeToString(modulus.toByteArray()), key.getKeyProperties().get("n")); }
@Test public void create_key_from_public_pem_string() { KeyInfo keyInfo = KeyInfoBuilder.build("id", sampleRsaPrivateKey, ISSUER); assertEquals("RSA", keyInfo.type()); assertNotNull(keyInfo.getVerifier()); Map<String, Object> jwkMap = keyInfo.getJwkMap(); JsonWebKey jsonWebKey = new JsonWebKey(jwkMap); JsonWebKey key = jsonWebKey.setKid("id"); assertEquals(RSA, key.getKty()); assertEquals("RSA", key.getKeyProperties().get("kty")); assertEquals("id", key.getKid()); assertEquals(sig, key.getUse()); assertEquals("sig", key.getKeyProperties().get("use")); assertNotNull(key.getValue()); PublicKey pk = parseKeyPair(keyInfo.verifierKey()).getPublic(); BigInteger exponent = ((RSAPublicKey) pk).getPublicExponent(); BigInteger modulus = ((RSAPublicKey) pk).getModulus(); java.util.Base64.Encoder encoder = java.util.Base64.getUrlEncoder().withoutPadding(); assertEquals(encoder.encodeToString(exponent.toByteArray()), key.getKeyProperties().get("e")); assertEquals(encoder.encodeToString(modulus.toByteArray()), key.getKeyProperties().get("n")); }
public static String decrypt(PublicKey publicKey, String cipherText) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); try { cipher.init(Cipher.DECRYPT_MODE, publicKey); } catch (InvalidKeyException e) { // 因为 IBM JDK 不支持私钥加密, 公钥解密, 所以要反转公私钥 // 也就是说对于解密, 可以通过公钥的参数伪造一个私钥对象欺骗 IBM JDK RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey; RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent()); Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec); cipher = Cipher.getInstance("RSA"); //It is a stateful object. so we need to get new one. cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey); } if (cipherText == null || cipherText.length() == 0) { return cipherText; } byte[] cipherBytes = Base64.base64ToByteArray(cipherText); byte[] plainBytes = cipher.doFinal(cipherBytes); return new String(plainBytes); }
/** * Encrypts the specified Content Encryption Key (CEK). * * @param pub The public RSA key. Must not be {@code null}. * @param cek The Content Encryption Key (CEK) to encrypt. Must not be * {@code null}. * * @return The encrypted Content Encryption Key (CEK). * * @throws RuntimeException If encryption failed. */ public static byte[] encryptCEK(final RSAPublicKey pub, final SecretKey cek) throws RuntimeException { try { AsymmetricBlockCipher engine = new RSAEngine(); // JCA identifier RSA/ECB/OAEPWithSHA-1AndMGF1Padding ? OAEPEncoding cipher = new OAEPEncoding(engine); BigInteger mod = pub.getModulus(); BigInteger exp = pub.getPublicExponent(); RSAKeyParameters keyParams = new RSAKeyParameters(false, mod, exp); cipher.init(true, keyParams); int inputBlockSize = cipher.getInputBlockSize(); int outputBlockSize = cipher.getOutputBlockSize(); byte[] keyBytes = cek.getEncoded(); return cipher.processBlock(keyBytes, 0, keyBytes.length); } catch (Exception e) { // org.bouncycastle.crypto.InvalidCipherTextException throw new RuntimeException(Messages.MESSAGES.couldntEncryptCEK(e.getLocalizedMessage()), e); } }
JCERSAPublicKey( RSAPublicKey key) { this.modulus = key.getModulus(); this.publicExponent = key.getPublicExponent(); }
BCRSAPublicKey( RSAPublicKey key) { this.modulus = key.getModulus(); this.publicExponent = key.getPublicExponent(); }
JCERSAPublicKey( RSAPublicKey key) { this.modulus = key.getModulus(); this.publicExponent = key.getPublicExponent(); }
JCERSAPublicKey( RSAPublicKey key) { this.modulus = key.getModulus(); this.publicExponent = key.getPublicExponent(); }
public static boolean compareRSAKeys(RSAPublicKey k1, RSAPublicKey k2) { if (Objects.equals(k1, k2)) { return true; } else if (k1 == null || k2 == null) { return false; // both null is covered by Objects#equals } else { return Objects.equals(k1.getPublicExponent(), k2.getPublicExponent()) && Objects.equals(k1.getModulus(), k2.getModulus()); } }
public static boolean compareRSAKeys(RSAPublicKey k1, RSAPublicKey k2) { if (Objects.equals(k1, k2)) { return true; } else if (k1 == null || k2 == null) { return false; // both null is covered by Objects#equals } else { return Objects.equals(k1.getPublicExponent(), k2.getPublicExponent()) && Objects.equals(k1.getModulus(), k2.getModulus()); } }
static RSAKeyParameters generatePublicKeyParameter( RSAPublicKey key) { return new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent()); }
static public RSAKeyParameters generatePublicKeyParameter( RSAPublicKey key) { return new RSAKeyParameters(false, key.getModulus(), key.getPublicExponent()); }