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()); } }
/** * 私钥解密 * * @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); } }
/** * 私钥解密 * * @param data * @param privateKey * @return * @throws Exception */ public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); //模长 int key_len = privateKey.getModulus().bitLength() / 8; byte[] bytes = data.getBytes(); byte[] bcd = ASCII_To_BCD(bytes, bytes.length); System.err.println(bcd.length); //如果密文长度大于模长则要分组解密 String ming = ""; byte[][] arrays = splitArray(bcd, key_len); for (byte[] arr : arrays) { ming += new String(cipher.doFinal(arr)); } return ming; }
/** * 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); }
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; }
/** * 私钥解密 * * @param data * @param privateKey * @return * @throws Exception */ public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); // 模长 int key_len = privateKey.getModulus().bitLength() / 8; byte[] bytes = data.getBytes(); byte[] bcd = ASCII_To_BCD(bytes, bytes.length); // 如果密文长度大于模长则要分组解密 String ming = ""; byte[][] arrays = splitArray(bcd, key_len); for (byte[] arr : arrays) { ming += new String(cipher.doFinal(arr)); } return ming; }
/** * 私钥解密 * * @param data * @param privateKey * @return * @throws Exception */ public static String decryptByPrivateKey(String data) throws Exception { RSAPrivateKey privateKey = RSAUtils.getPrivateKey(modulus, private_exponent); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); // 模长 int key_len = privateKey.getModulus().bitLength() / 8; byte[] bytes = data.getBytes(); byte[] bcd = ASCII_To_BCD(bytes, bytes.length); // 如果密文长度大于模长则要分组解密 String ming = ""; byte[][] arrays = splitArray(bcd, key_len); for (byte[] arr : arrays) { ming += new String(cipher.doFinal(arr)); } return ming; }
/** * 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); }
int sz = k.getModulus().bitLength(); SigType type; if (sz <= ((RSAKeyGenParameterSpec) SigType.RSA_SHA256_2048.getParams()).getKeysize())
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);
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 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(); }
RawRSAPrivateKey(final RSAPrivateKey original) { super(original); privateExponent = original.getPrivateExponent(); modulus = original.getModulus(); }
RawRSAPrivateKey(final RSAPrivateKey original) { super(original); privateExponent = original.getPrivateExponent(); modulus = original.getModulus(); }
static OpenSSLKey wrapPlatformKey(RSAPrivateKey rsaPrivateKey) throws InvalidKeyException { OpenSSLKey wrapper = Platform.wrapRsaKey(rsaPrivateKey); if (wrapper != null) { return wrapper; } return new OpenSSLKey(NativeCrypto.getRSAPrivateKeyWrapper(rsaPrivateKey, rsaPrivateKey .getModulus().toByteArray()), true); }