/** * 用私钥解密通过公钥加密的数据 * * @param data 通过公钥加密的数据 * @param key 用来解密的私钥 * @return 解密后的数据 * @throws NoSuchAlgorithmException 假如用户的JDK不支持RSA * @throws InvalidKeySpecException 假如根据privateKey生成密钥失败 * @throws InvalidKeyException 假如输入的RSA私钥不合法 * @throws SignatureException 假如根据privateKey生成密钥失败 * @throws UnsupportedEncodingException 假如privateKey不是使用UTF-8进行编码 * @throws NoSuchPaddingException 假如产生的密钥对有问题 * @throws BadPaddingException 假如输入的加密的数据填充数据错误 * @throws IllegalBlockSizeException 假如输入的加密的数据字节数不是BlockSize的整数倍 * @throws ShortBufferException */ public static byte[] decryptByPrivateKey(byte[] data, Key privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException, UnsupportedEncodingException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ShortBufferException { // 对数据解密 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); return CipherUtil.process(cipher, 128, data); }
/** * 用私钥加密数据 * * @param data 等待加密的原始数据 * @param key 用来加密的私钥 * @return 加密后的数据 * @throws NoSuchAlgorithmException 假如用户的JDK不支持RSA * @throws InvalidKeySpecException 假如根据privateKey生成密钥失败 * @throws InvalidKeyException 假如输入的RSA私钥不合法 * @throws SignatureException 假如根据privateKey生成密钥失败 * @throws UnsupportedEncodingException 假如privateKey不是使用UTF-8进行编码 * @throws NoSuchPaddingException 假如产生的密钥对有问题 * @throws BadPaddingException 假如输入的加密的数据填充数据错误 * @throws IllegalBlockSizeException 假如输入的加密的数据字节数不是BlockSize的整数倍 * @throws ShortBufferException */ public static byte[] encryptByPrivateKey(byte[] data, String key) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException, UnsupportedEncodingException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ShortBufferException { // 对密钥解密 byte[] keyBytes = Base64ForServer.base64toByte(key); // 取得私钥 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // 对数据加密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return CipherUtil.process(cipher, 117, data); }
/** * 用公钥解密通过私钥加密的数据 * * @param data 通过私钥加密的数据 * @param key 用来解密的公钥 * @return 解密后的数据 * @throws NoSuchAlgorithmException 假如用户的JDK不支持RSA * @throws InvalidKeySpecException 假如根据privateKey生成密钥失败 * @throws InvalidKeyException 假如输入的RSA私钥不合法 * @throws SignatureException 假如根据privateKey生成密钥失败 * @throws UnsupportedEncodingException 假如privateKey不是使用UTF-8进行编码 * @throws NoSuchPaddingException 假如产生的密钥对有问题 * @throws BadPaddingException 假如输入的加密的数据填充数据错误 * @throws IllegalBlockSizeException 假如输入的加密的数据字节数不是BlockSize的整数倍 * @throws ShortBufferException */ public static byte[] decryptByPublicKey(byte[] data, String key) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException, UnsupportedEncodingException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ShortBufferException { // 对密钥解密 byte[] keyBytes = Base64ForServer.base64toByte(key); // 取得公钥 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicKey = keyFactory.generatePublic(x509KeySpec); // 对数据解密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicKey); return CipherUtil.process(cipher, 128, data); }