Refine search
/** * Validates RSA public and private key. * * @param keyPair the keypair * @return true if keys matches */ public static boolean validateKeyPair(KeyPair keyPair) { RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); if (publicKey.getModulus().bitLength() != privateKey.getModulus().bitLength()) { LOG.error("Keypair length matching error"); return false; } byte[] rawPayload = new byte[64]; new Random().nextBytes(rawPayload); MessageEncoderDecoder encDec = new MessageEncoderDecoder(privateKey, publicKey); byte[] encodedPayload; byte[] decodedPayload; try { encodedPayload = encDec.encodeData(rawPayload); decodedPayload = encDec.decodeData(encodedPayload); } catch (GeneralSecurityException ex) { LOG.error("Validation keypair error ", ex); return false; } return Arrays.equals(rawPayload, decodedPayload); }
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)); }
priv = (RSAPrivateKey) keys.getPrivate(); pub = (RSAPublicKey) keys.getPublic(); store(priv.getEncoded()); } else { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); new RSAPublicKeySpec(pks.getModulus(), pks.getPublicExponent()));
RawRSAPrivateKey(final RSAPrivateKey original) { super(original); privateExponent = original.getPrivateExponent(); modulus = original.getModulus(); }
RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey; RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey; return rsaPublicKey.getModulus().equals( rsaPrivateKey.getModulus() ) && BigInteger.valueOf( 2 ).modPow( rsaPublicKey.getPublicExponent().multiply( rsaPrivateKey.getPrivateExponent() ) .subtract( BigInteger.ONE ), rsaPublicKey.getModulus() ).equals( BigInteger.ONE );
public void init(int key_size) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(key_size, new SecureRandom()); KeyPair pair = keyGen.generateKeyPair(); PublicKey pubKey = pair.getPublic(); PrivateKey prvKey = pair.getPrivate(); d = ((RSAPrivateKey) prvKey).getPrivateExponent().toByteArray(); e = ((RSAPublicKey) pubKey).getPublicExponent().toByteArray(); n = ((RSAPrivateKey) prvKey).getModulus().toByteArray(); c = ((RSAPrivateCrtKey) prvKey).getCrtCoefficient().toByteArray(); ep = ((RSAPrivateCrtKey) prvKey).getPrimeExponentP().toByteArray(); eq = ((RSAPrivateCrtKey) prvKey).getPrimeExponentQ().toByteArray(); p = ((RSAPrivateCrtKey) prvKey).getPrimeP().toByteArray(); q = ((RSAPrivateCrtKey) prvKey).getPrimeQ().toByteArray(); }
public static boolean validateRSAKeyPair(RSAPrivateCrtKey privateKey, RSAPublicKey publicKey) { BigInteger n = publicKey.getModulus(); BigInteger e = publicKey.getPublicExponent(); BigInteger d = privateKey.getPrivateExponent(); BigInteger p = privateKey.getPrimeP(); BigInteger q = privateKey.getPrimeQ(); BigInteger pq = p.multiply(q);//shold equal to n BigInteger eulerMod = p.add(NEGATIVE_ONE).multiply(q.add(NEGATIVE_ONE));// φ(n)=(p-1)*(q-1) BigInteger mod = d.multiply(e).mod(eulerMod);// (d*e) mod φ(n), should be 1 return n.equals(pq) && BigInteger.ONE.equals(mod); }
public boolean equals(Object o) { if (!(o instanceof RSAPrivateKey)) { return false; } if (o == this) { return true; } RSAPrivateKey key = (RSAPrivateKey)o; return getModulus().equals(key.getModulus()) && getPrivateExponent().equals(key.getPrivateExponent()); }
protected int getKeyCipherBlockSize() { return ((RSAPrivateKey)getCekDecryptionKey()).getModulus().toByteArray().length; } @Override
static public RSAKeyParameters generatePrivateKeyParameter(RSAPrivateKey key) { if (key instanceof RSAPrivateCrtKey) { RSAPrivateCrtKey k = (RSAPrivateCrtKey) key; return new RSAPrivateCrtKeyParameters(k.getModulus(), k.getPublicExponent(), k.getPrivateExponent(), k.getPrimeP(), k.getPrimeQ(), k.getPrimeExponentP(), k.getPrimeExponentQ(), k.getCrtCoefficient()); } else { RSAPrivateKey k = key; return new RSAKeyParameters(true, k.getModulus(), k.getPrivateExponent()); } }
String modulus = publicKey.getModulus().toString(); String public_exponent = publicKey.getPublicExponent().toString(); String private_exponent = privateKey.getPrivateExponent().toString(); String pubKeyStr = encryptBASE64(pubKey.getEncoded()); System.err.println("pubKey:" + pubKeyStr); RSAPublicKey pub = getPublicKey(pubKeyStr); String priKeyStr = encryptBASE64(priKey.getEncoded()); System.err.println("priKey:" + priKeyStr); RSAPrivateKey pri = getPrivateKey(priKeyStr);
protected void verifyDelegatedCert(X509Certificate certificate) throws GeneralSecurityException { RSAPublicKey pubKey = (RSAPublicKey) certificate.getPublicKey(); RSAPrivateKey privKey = (RSAPrivateKey) keyPair.getPrivate(); if (!pubKey.getModulus().equals(privKey.getModulus())) { throw new GeneralSecurityException("Client delegated credentials do not match certificate request."); } }
public static String toString(Object key) { if (key instanceof RSAPrivateKey) { RSAPrivateKey pk = (RSAPrivateKey) key; return "RSA.Private(" + pk.getModulus() + ":" + pk.getPrivateExponent() + ")"; } if (key instanceof RSAPublicKey) { RSAPublicKey pk = (RSAPublicKey) key; return "RSA.Private(" + pk.getModulus() + ":" + pk.getPublicExponent() + ")"; } return null; }
/** * 私钥解密 * * @param data 待加密数据 * @param privateKey 私钥 * @return 解密后的值 */ public static byte[] decryptByPrivateKey(byte[] data, RSAPrivateKey privateKey) { try { Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_PADDING); cipher.init(Cipher.DECRYPT_MODE, privateKey); //模长 int key_len = privateKey.getModulus().bitLength() / 8; //如果密文长度大于模长则要分组解密 return doFinal(cipher, data, key_len); } catch (Exception e) { LOGGER.error("decryptByPrivateKey ex", e); throw new CryptoException("RSA decrypt ex", e); } }
@Override protected int engineGetKeySize(Key key) throws InvalidKeyException { if (key instanceof OpenSSLRSAPrivateKey) { return ((OpenSSLRSAPrivateKey) key).getModulus().bitLength(); } if (key instanceof RSAPrivateCrtKey) { return ((RSAPrivateCrtKey) key).getModulus().bitLength(); } if (key instanceof RSAPrivateKey) { return ((RSAPrivateKey) key).getModulus().bitLength(); } if (key instanceof OpenSSLRSAPublicKey) { return ((OpenSSLRSAPublicKey) key).getModulus().bitLength(); } if (key instanceof RSAPublicKey) { return ((RSAPublicKey) key).getModulus().bitLength(); } if (null == key) { throw new InvalidKeyException("RSA private or public key is null"); } throw new InvalidKeyException("Need RSA private or public key"); }
RSAPrivateCrtKey asCrt = (RSAPrivateCrtKey) priv; System.out.printf("%s, %d bits%n", asCrt.getAlgorithm(), asCrt.getModulus().bitLength()); System.out.printf(" modulus: %d%n", asCrt.getModulus()); System.out.printf(" private exponent: %d%n", asCrt.getPrivateExponent());
/** * The entry point of the RsaKeyGenerator. * * @param args The application arguments. */ public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); keyPairGenerator.initialize(BIT_COUNT); KeyPair keyPair = keyPairGenerator.generateKeyPair(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); System.out.println("Place these keys in the client:"); System.out.println("--------------------"); System.out.println("public key: " + publicKey.getPublicExponent()); System.out.println("modulus: " + publicKey.getModulus()); try (PemWriter writer = new PemWriter(new FileWriter(PRIVATE_KEY_FILE))) { writer.writeObject(new PemObject("RSA PRIVATE KEY", privateKey.getEncoded())); } catch (Exception e) { System.err.println("Failed to write private key to " + PRIVATE_KEY_FILE); e.printStackTrace(); } }
protected void fillPrivateTypeSpecificParams(Map<String,Object> params) { RSAPrivateKey rsaPrivateKey = getRsaPrivateKey(); if (rsaPrivateKey != null) { putBigIntAsBase64UrlEncodedParam(params, PRIVATE_EXPONENT_MEMBER_NAME, rsaPrivateKey.getPrivateExponent()); if (rsaPrivateKey instanceof RSAPrivateCrtKey) { RSAPrivateCrtKey crt = (RSAPrivateCrtKey) rsaPrivateKey; putBigIntAsBase64UrlEncodedParam(params, FIRST_PRIME_FACTOR_MEMBER_NAME, crt.getPrimeP()); putBigIntAsBase64UrlEncodedParam(params, SECOND_PRIME_FACTOR_MEMBER_NAME, crt.getPrimeQ()); putBigIntAsBase64UrlEncodedParam(params, FIRST_FACTOR_CRT_EXPONENT_MEMBER_NAME, crt.getPrimeExponentP()); putBigIntAsBase64UrlEncodedParam(params, SECOND_FACTOR_CRT_EXPONENT_MEMBER_NAME, crt.getPrimeExponentQ()); putBigIntAsBase64UrlEncodedParam(params, FIRST_CRT_COEFFICIENT_MEMBER_NAME, crt.getCrtCoefficient()); } } }
@Override public String toString() { return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; }
RSAPrivateKey rsapriv = SigUtil.toJavaRSAKey(priv); BigInteger exp = ((RSAKeyGenParameterSpec)type.getParams()).getPublicExponent(); RSAPublicKeySpec rsaks = new RSAPublicKeySpec(rsapriv.getModulus(), exp); KeyFactory rsakf = KeyFactory.getInstance("RSA"); RSAPublicKey rsapub = (RSAPublicKey) rsakf.generatePublic(rsaks);