private String encrypt(String encodeString) { //空字符串不加密 if (StringUtil.isEmpty(encodeString)) { return ""; } try { byte[] modulusBytes = new BASE64Decoder().decodeBuffer(PropertiesHelp.getProperty( configPath, "encrypt.modulus")); byte[] exponentBytes = new BASE64Decoder().decodeBuffer(PropertiesHelp.getProperty( configPath, "encrypt.exponent")); BigInteger modulus = new BigInteger(1, modulusBytes); BigInteger exponent = new BigInteger(1, exponentBytes); RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey pubKey = fact.generatePublic(rsaPubKey); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, pubKey); byte[] bytes = encodeString.getBytes(); byte[] encodedByteArray = new byte[] {}; for (int i = 0; i < bytes.length; i += 102) { byte[] subarray = ArrayUtils.subarray(bytes, i, i + 102); byte[] doFinal = cipher.doFinal(subarray); encodedByteArray = ArrayUtils.addAll(encodedByteArray, doFinal); } return new BASE64Encoder().encode(encodedByteArray); } catch (Exception e) { logger.error(e.getMessage()); } return null; }
近期评论