private static final int MAX_ENCRYPT = 117; private static final int MAX_DNCRYPT = 128;
/** * RSA公钥加密 * * @param str 加密字符串 * @param publicKey 公钥 * @return 密文 * @throws Exception 加密过程中的异常信息 */ public static String encryptRSA( String str, String publicKey ) { String outStr = null; try { //base64编码的公钥 byte[] decoded = Base64.decodeBase64(publicKey); byte[] strBytes = Base64.encodeBase64(str.getBytes("UTF-8")); X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(decoded); KeyFactory rsa = KeyFactory.getInstance("RSA"); PublicKey publicKey1 = rsa.generatePublic(encodedKeySpec); //RSA加密 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey1); int strByteSize = strBytes.length; // 标识 int cursor = 0; byte[] resByte = {}; byte[] temp = {}; while (strByteSize - cursor > 0) { if (strByteSize - cursor > MAX_ENCRYPT) { temp = cipher.doFinal(strBytes, cursor, MAX_ENCRYPT); cursor += MAX_ENCRYPT; } else { temp = cipher.doFinal(strBytes, cursor, strByteSize - cursor); cursor = strByteSize; } resByte = ArrayUtils.addAll(temp, resByte); } outStr = new String(Base64.encodeBase64(resByte)); } catch (InvalidKeySpecException e) { LOGGER.error("EncipherUtil|encrypt|InvalidKeySpecException->",e); } catch (NoSuchAlgorithmException e) { LOGGER.error("EncipherUtil|encrypt|NoSuchAlgorithmException->",e); } catch (BadPaddingException e) { LOGGER.error("EncipherUtil|encrypt|BadPaddingException->",e); } catch (InvalidKeyException e) { LOGGER.error("EncipherUtil|encrypt|InvalidKeyException->",e); } catch (NoSuchPaddingException e) { LOGGER.error("EncipherUtil|encrypt|NoSuchPaddingException->",e); } catch (IllegalBlockSizeException e) { LOGGER.error("EncipherUtil|encrypt|IllegalBlockSizeException->",e); } catch (UnsupportedEncodingException e) { LOGGER.error("EncipherUtil|encrypt|UnsupportedEncodingException->",e); } return outStr; }
/** * RSA私钥解密 * * @param st 加密字符串 * @param privateKey 私钥 * @return 铭文 * @throws Exception 解密过程中的异常信息 */ public static String decryptRSA(String str, String privateKey) { String outStr = null; try { //base64编码的私钥 byte[] decoded = Base64.decodeBase64(privateKey); //64位解码加密后的字符串 byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8")); KeyFactory rsa = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(decoded); PrivateKey privateKey1 = rsa.generatePrivate(pkcs8EncodedKeySpec); //RSA解密 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey1); int inputLength = inputByte.length; // 标识 int cursor = 0; byte[] result = {}; byte[] temp = {}; while (inputLength - cursor > 0) { if (inputLength - cursor > MAX_DNCRYPT) { temp = cipher.doFinal(inputByte, cursor, MAX_DNCRYPT); cursor += MAX_DNCRYPT; } else { temp = cipher.doFinal(inputByte, cursor, inputLength - cursor); cursor = inputLength; } result = ArrayUtils.addAll(temp, result); } outStr = new String(Base64.decodeBase64(result)); } catch (UnsupportedEncodingException e) { LOGGER.error("EncipherUtil|decrypt|UnsupportedEncodingException->",e); } catch (NoSuchPaddingException e) { LOGGER.error("EncipherUtil|decrypt|NoSuchPaddingException->",e); } catch (NoSuchAlgorithmException e) { LOGGER.error("EncipherUtil|decrypt|NoSuchAlgorithmException->",e); } catch (InvalidKeySpecException e) { LOGGER.error("EncipherUtil|decrypt|InvalidKeySpecException->",e); } catch (BadPaddingException e) { LOGGER.error("EncipherUtil|decrypt|BadPaddingException->",e); } catch (IllegalBlockSizeException e) { LOGGER.error("EncipherUtil|decrypt|IllegalBlockSizeException->",e); } catch (InvalidKeyException e) { LOGGER.error("EncipherUtil|decrypt|InvalidKeyException->",e); } return outStr; }
======================ge========ge======================================
private static final String AESCBC_ALGORITHM ="AES/CBC/PKCS5Padding"; private static final String CIPHER_AES ="AES"; private static final String SALT = "SUFwYzlBc3VutQ=="; private static final String VIPARA = "9L1i0sIn5@1_3g03"; private static final String CODE_TYPE = "UTF-8"; /** * 加密 有偏移量 * @param encryptedData * @return */ public static String encryptAES(String encryptedData) { String result = null ; try { //原始数据 先base64加密 byte[] encryptedDataByte = Base64.encodeBase64(encryptedData.getBytes(CODE_TYPE)); Cipher cipher = Cipher.getInstance(AESCBC_ALGORITHM); //偏移量 IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes(CODE_TYPE)); //使用加密秘钥 SecretKeySpec skeySpec = new SecretKeySpec(SALT.getBytes(CODE_TYPE), CIPHER_AES); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, zeroIv); byte[] result1 = cipher.doFinal(encryptedDataByte); result = Base64.encodeBase64String(result1); } catch (NoSuchAlgorithmException e) { LOGGER.error("DecryptionUtil|decrypt NoSuchAlgorithmException"+e.getMessage(),e); } catch (NoSuchPaddingException e) { LOGGER.error("DecryptionUtil|decrypt NoSuchPaddingException"+e.getMessage(),e); } catch (InvalidKeyException e) { LOGGER.error("DecryptionUtil|decrypt InvalidKeyException"+e.getMessage(),e); } catch (IllegalBlockSizeException e) { LOGGER.error("DecryptionUtil|decrypt IllegalBlockSizeException"+e.getMessage(),e); } catch (BadPaddingException e) { LOGGER.error("DecryptionUtil|decrypt BadPaddingException"+e.getMessage(),e); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } /** * 解密 有偏移量 * @param encryptedData * @return */ public static String decryptAES(String encryptedData) { //被加密的数据 byte[] encryptedDataByte = Base64.decodeBase64(encryptedData); String result = null ; try { Cipher cipher = Cipher.getInstance(AESCBC_ALGORITHM); IvParameterSpec ivSpec = new IvParameterSpec(VIPARA.getBytes(CODE_TYPE)); SecretKeySpec keySpec = new SecretKeySpec(SALT.getBytes(), CIPHER_AES); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] resultByte1 = cipher.doFinal(encryptedDataByte); if (null != resultByte1 && resultByte1.length > 0) { result = new String(Base64.decodeBase64(resultByte1), "UTF-8"); } } catch (NoSuchAlgorithmException e) { LOGGER.error("DecryptionUtil|decrypt NoSuchAlgorithmException"+e.getMessage(),e); } catch (NoSuchPaddingException e) { LOGGER.error("DecryptionUtil|decrypt NoSuchPaddingException"+e.getMessage(),e); } catch (InvalidKeyException e) { LOGGER.error("DecryptionUtil|decrypt InvalidKeyException"+e.getMessage(),e); } catch (InvalidAlgorithmParameterException e) { LOGGER.error("DecryptionUtil|decrypt InvalidAlgorithmParameterException"+e.getMessage(),e); } catch (IllegalBlockSizeException e) { LOGGER.error("DecryptionUtil|decrypt IllegalBlockSizeException"+e.getMessage(),e); } catch (BadPaddingException e) { LOGGER.error("DecryptionUtil|decrypt BadPaddingException"+e.getMessage(),e); } catch (UnsupportedEncodingException e) { LOGGER.error("DecryptionUtil|decrypt UnsupportedEncodingException"+e.getMessage(),e); } return result; }