public static String encrypt(byte[] keyBytes, String plainText) throws Exception { PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory factory = KeyFactory.getInstance("RSA", "SunRsaSign"); PrivateKey privateKey = factory.generatePrivate(spec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); try { cipher.init(Cipher.ENCRYPT_MODE, privateKey); } catch (InvalidKeyException e) { //For IBM JDK, 原因请看解密方法中的说明 RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey; RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent()); Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec); cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey); } byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8")); String encryptedString = Base64.byteArrayToBase64(encryptedBytes); return encryptedString; }
RawRSAPrivateKey(final RSAPrivateKey original) { super(original); privateExponent = original.getPrivateExponent(); modulus = original.getModulus(); }
boolean isEqual(final RSAPrivateKey key) { return super.isEqual(key) && Objects.equals(privateExponent, key.getPrivateExponent()) && Objects.equals(modulus, key.getModulus()); } }
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)); }
public static String encrypt(byte[] keyBytes, String plainText) throws Exception { PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory factory = KeyFactory.getInstance("RSA", "SunRsaSign"); PrivateKey privateKey = factory.generatePrivate(spec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); try { cipher.init(Cipher.ENCRYPT_MODE, privateKey); } catch (InvalidKeyException e) { //For IBM JDK, 原因请看解密方法中的说明 RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey; RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent()); Key fakePublicKey = KeyFactory.getInstance("RSA").generatePublic(publicKeySpec); cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, fakePublicKey); } byte[] encryptedBytes = cipher.doFinal(plainText.getBytes("UTF-8")); String encryptedString = Base64.byteArrayToBase64(encryptedBytes); return encryptedString; }
/** * Decrypts the specified encrypted Content Encryption Key (CEK). * * @param priv The private RSA key. Must not be {@code null}. * @param encryptedCEK The encrypted Content Encryption Key (CEK) to * decrypt. Must not be {@code null}. * * @return The decrypted Content Encryption Key (CEK). * * @throws RuntimeException If decryption failed. */ public static SecretKey decryptCEK(final RSAPrivateKey priv, final byte[] encryptedCEK) throws RuntimeException { try { RSAEngine engine = new RSAEngine(); OAEPEncoding cipher = new OAEPEncoding(engine); BigInteger mod = priv.getModulus(); BigInteger exp = priv.getPrivateExponent(); RSAKeyParameters keyParams = new RSAKeyParameters(true, mod, exp); cipher.init(false, keyParams); byte[] secretKeyBytes = cipher.processBlock(encryptedCEK, 0, encryptedCEK.length); return new SecretKeySpec(secretKeyBytes, "AES"); } catch (Exception e) { // org.bouncycastle.crypto.InvalidCipherTextException throw new RuntimeException(Messages.MESSAGES.couldntDecryptCEK(e.getLocalizedMessage()), e); } }
/** * As of 0.9.31, if pk is a RSAPrivateCrtKey, * this will return a RSASigningPrivateCrtKey. */ public static SigningPrivateKey fromJavaKey(RSAPrivateKey pk, SigType type) throws GeneralSecurityException { // private key is modulus (pubkey) + exponent BigInteger n = pk.getModulus(); BigInteger d = pk.getPrivateExponent(); byte[] b = combine(n, d, type.getPrivkeyLen()); if (pk instanceof RSAPrivateCrtKey) return RSASigningPrivateCrtKey.fromJavaKey((RSAPrivateCrtKey) pk); return new SigningPrivateKey(type, b); }
public static BigInteger extractRSAPrivateExponent(PrivateKey key) throws IOException { if (key instanceof RSAPrivateKey) { return ((RSAPrivateKey) ((RSAPrivateKey) key)).getPrivateExponent(); } else { return null; } }
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; }
BCRSAPrivateKey( RSAPrivateKey key) { this.modulus = key.getModulus(); this.privateExponent = key.getPrivateExponent(); }
JCERSAPrivateKey( RSAPrivateKey key) { this.modulus = key.getModulus(); this.privateExponent = key.getPrivateExponent(); }
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; }
RawRSAPrivateKey(final RSAPrivateKey original) { super(original); privateExponent = original.getPrivateExponent(); modulus = original.getModulus(); }
JCERSAPrivateKey( RSAPrivateKey key) { this.modulus = key.getModulus(); this.privateExponent = key.getPrivateExponent(); }
RawRSAPrivateKey(final RSAPrivateKey original) { super(original); privateExponent = original.getPrivateExponent(); modulus = original.getModulus(); }
JCERSAPrivateKey( RSAPrivateKey key) { this.modulus = key.getModulus(); this.privateExponent = key.getPrivateExponent(); }
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; }
RawRSAPrivateKey(final RSAPrivateKey original) { super(original); privateExponent = original.getPrivateExponent(); modulus = original.getModulus(); }
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; }
public static RSAKeyParameters generateRSAPrivateKeyParameter(final RSAPrivateKey key) { ParamUtil.requireNonNull("key", key); if (key instanceof RSAPrivateCrtKey) { RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key; return new RSAPrivateCrtKeyParameters(rsaKey.getModulus(), rsaKey.getPublicExponent(), rsaKey.getPrivateExponent(), rsaKey.getPrimeP(), rsaKey.getPrimeQ(), rsaKey.getPrimeExponentP(), rsaKey.getPrimeExponentQ(), rsaKey.getCrtCoefficient()); } else { return new RSAKeyParameters(true, key.getModulus(), key.getPrivateExponent()); } }