AES加密

@param content
@param aesKey
@param charset
@return
@throws AlipayApiException
514725
private static String aesEncrypt(String content, String aesKey, String charset)
                                                                                   throws AlipayApiException {

        try {
            Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG);

            IvParameterSpec iv = new IvParameterSpec(AES_IV);
            cipher.init(Cipher.ENCRYPT_MODE,
                new SecretKeySpec(Base64.decodeBase64(aesKey.getBytes()), AES_ALG), iv);

            byte[] encryptBytes = cipher.doFinal(content.getBytes(charset));
            return new String(Base64.encodeBase64(encryptBytes));
        } catch (Exception e) {
            throw new AlipayApiException("AES加密失败：Aescontent = " + content + "; charset = "
                                         + charset, e);
        }

    }
340806
public static byte[] AesDecryptByBytes(byte[] encryptBytes,byte[] decryptKey) throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
		secureRandom.setSeed(decryptKey);
		kgen.init(128,secureRandom);
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
		byte[] decryptBytes = cipher.doFinal(encryptBytes);
		return decryptBytes;
	}
1251909
public static void decodeTo(String content, MultiMap<String> map, String charset) {
        decodeTo(content, map, charset == null ? null : Charset.forName(charset));
    }
1004941
public static byte[] aesEncrypt(byte[] input, byte[] key, byte[] iv) {
		return aes(input, key, iv, Cipher.ENCRYPT_MODE);
	}
858507
public static String encryptByAES(final String content, final String key) {
        try {
            final KeyGenerator kgen = KeyGenerator.getInstance("AES");
            final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(key.getBytes());
            kgen.init(128, secureRandom);
            final SecretKey secretKey = kgen.generateKey();
            final byte[] enCodeFormat = secretKey.getEncoded();
            final SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
            final Cipher cipher = Cipher.getInstance("AES");
            final byte[] byteContent = content.getBytes("UTF-8");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            final byte[] result = cipher.doFinal(byteContent);

            return Hex.encodeHexString(result);
        } catch (final Exception e) {
            LOGGER.log(Level.WARN, "Encrypt failed", e);

            return null;
        }
    }
589940
public static String decodeBase64(String content, Charset charset, TestContext context) {
        return new DecodeBase64Function().execute(Arrays.asList(content, charset.displayName()), context);
    }
483051
public static void decodeTo(String content, MultiMap<String> map, Charset charset) {
        if (charset == null)
            charset = ENCODING;

        if (charset == StandardCharsets.UTF_8) {
            decodeUtf8To(content, 0, content.length(), map);
            return;
        }

        synchronized (map) {
            String key = null;
            String value = null;
            int mark = -1;
            boolean encoded = false;
            for (int i = 0; i < content.length(); i++) {
                char c = content.charAt(i);
                switch (c) {
                    case '&':
                        int l = i - mark - 1;
                        value = l == 0 ? "" :
                                (encoded ? decodeString(content, mark + 1, l, charset) : content.substring(mark + 1, i));
                        mark = i;
                        encoded = false;
                        if (key != null) {
                            map.add(key, value);
                        } else if (value != null && value.length() > 0) {
                            map.add(value, "");
                        }
                        key = null;
                        value = null;
                        break;
                    case '=':
                        if (key != null)
                            break;
                        key = encoded ? decodeString(content, mark + 1, i - mark - 1, charset) : content.substring(mark + 1, i);
                        mark = i;
                        encoded = false;
                        break;
                    case '+':
                        encoded = true;
                        break;
                    case '%':
                        encoded = true;
                        break;
                }
            }

            if (key != null) {
                int l = content.length() - mark - 1;
                value = l == 0 ? "" : (encoded ? decodeString(content, mark + 1, l, charset) : content.substring(mark + 1));
                map.add(key, value);
            } else if (mark < content.length()) {
                key = encoded
                        ? decodeString(content, mark + 1, content.length() - mark - 1, charset)
                        : content.substring(mark + 1);
                if (key != null && key.length() > 0) {
                    map.add(key, "");
                }
            }
        }
    }
1004298
public static String decryptContent(String content, String encryptType, String encryptKey,
                                        String charset) throws AlipayApiException {

        if (AES_ALG.equals(encryptType)) {

            return aesDecrypt(content, encryptKey, charset);

        } else {

            throw new AlipayApiException("当前不支持该算法类型：encrypeType=" + encryptType);
        }

    }
465734
public static byte[] zlib(String content, String charset, int level) {
		return zlib(StrUtil.bytes(content, charset), level);
	}
925096
public static String decryptToBase64(String content) {
        byte[] decodedValue = DatatypeConverter.parseBase64Binary(content);
        return new String(decodedValue, CHARSET);
    }
501522
public static byte[] aesDecrypt(final byte[] input, final byte[] key, final byte[] iv)
		throws GeneralSecurityException {
		return aes(input, key, iv, Cipher.DECRYPT_MODE);
	}
857371
private static String aesDecrypt(String content, String key, String charset)
                                                                                throws AlipayApiException {
        try {
            Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG);
            IvParameterSpec iv = new IvParameterSpec(initIv(AES_CBC_PCK_ALG));
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(key.getBytes()),
                AES_ALG), iv);

            byte[] cleanBytes = cipher.doFinal(Base64.decodeBase64(content.getBytes()));
            return new String(cleanBytes, charset);
        } catch (Exception e) {
            throw new AlipayApiException("AES解密失败：Aescontent = " + content + "; charset = "
                                         + charset, e);
        }
    }
421950
public static ByteArrayInputStream toStream(String content, Charset charset) {
		if (content == null) {
			return null;
		}
		return toStream(StrUtil.bytes(content, charset));
	}
696602
public HppRequest encode(String charset) throws UnsupportedEncodingException {

		if (null != this.account) {
			this.account = new String(Base64.encodeBase64(this.account.getBytes(charset)), charset);
		}
		if (null != this.amount) {
			this.amount = new String(Base64.encodeBase64(this.amount.getBytes(charset)), charset);
		}
		if (null != this.autoSettleFlag) {
			this.autoSettleFlag = new String(Base64.encodeBase64(this.autoSettleFlag.getBytes(charset)), charset);
		}
		if (null != this.billingCode) {
			this.billingCode = new String(Base64.encodeBase64(this.billingCode.getBytes(charset)), charset);
		}
		if (null != this.billingCountry) {
			this.billingCountry = new String(Base64.encodeBase64(this.billingCountry.getBytes(charset)), charset);
		}
		if (null != this.cardPaymentButtonText) {
			this.cardPaymentButtonText = new String(Base64.encodeBase64(this.cardPaymentButtonText.getBytes(charset)), charset);
		}
		if (null != this.cardStorageEnable) {
			this.cardStorageEnable = new String(Base64.encodeBase64(this.cardStorageEnable.getBytes(charset)), charset);
		}
		if (null != this.commentOne) {
			this.commentOne = new String(Base64.encodeBase64(this.commentOne.getBytes(charset)), charset);
		}
		if (null != this.commentTwo) {
			this.commentTwo = new String(Base64.encodeBase64(this.commentTwo.getBytes(charset)), charset);
		}
		if (null != this.currency) {
			this.currency = new String(Base64.encodeBase64(this.currency.getBytes(charset)), charset);
		}
		if (null != this.customerNumber) {
			this.customerNumber = new String(Base64.encodeBase64(this.customerNumber.getBytes(charset)), charset);
		}
		if (null != this.hash) {
			this.hash = new String(Base64.encodeBase64(this.hash.getBytes(charset)), charset);
		}
		if (null != this.language) {
			this.language = new String(Base64.encodeBase64(this.language.getBytes(charset)), charset);
		}
		if (null != this.merchantId) {
			this.merchantId = new String(Base64.encodeBase64(this.merchantId.getBytes(charset)), charset);
		}
		if (null != this.offerSaveCard) {
			this.offerSaveCard = new String(Base64.encodeBase64(this.offerSaveCard.getBytes(charset)), charset);
		}
		if (null != this.orderId) {
			this.orderId = new String(Base64.encodeBase64(this.orderId.getBytes(charset)), charset);
		}
		if (null != this.payerExists) {
			this.payerExists = new String(Base64.encodeBase64(this.payerExists.getBytes(charset)), charset);
		}
		if (null != this.payerReference) {
			this.payerReference = new String(Base64.encodeBase64(this.payerReference.getBytes(charset)), charset);
		}
		if (null != this.paymentReference) {
			this.paymentReference = new String(Base64.encodeBase64(this.paymentReference.getBytes(charset)), charset);
		}
		if (null != this.productId) {
			this.productId = new String(Base64.encodeBase64(this.productId.getBytes(charset)), charset);
		}
		if (null != this.returnTss) {
			this.returnTss = new String(Base64.encodeBase64(this.returnTss.getBytes(charset)), charset);
		}
		if (null != this.shippingCode) {
			this.shippingCode = new String(Base64.encodeBase64(this.shippingCode.getBytes(charset)), charset);
		}
		if (null != this.shippingCountry) {
			this.shippingCountry = new String(Base64.encodeBase64(this.shippingCountry.getBytes(charset)), charset);
		}
		if (null != this.timeStamp) {
			this.timeStamp = new String(Base64.encodeBase64(this.timeStamp.getBytes(charset)), charset);
		}
		if (null != this.variableReference) {
			this.variableReference = new String(Base64.encodeBase64(this.variableReference.getBytes(charset)));
		}

		if (null != this.supplementaryData) {
			Map<String, String> supplementaryDataMap = new HashMap<String, String>();
			for (String key : supplementaryData.keySet()) {
				supplementaryDataMap.put(key, new String(Base64.encodeBase64(supplementaryData.get(key).getBytes(charset)), charset));
			}
			this.supplementaryData.putAll(supplementaryDataMap);
		}
		if (null != this.validateCardOnly) {
			this.validateCardOnly = new String(Base64.encodeBase64(this.validateCardOnly.getBytes(charset)));
		}
		if (null != this.dccEnable) {
			this.dccEnable = new String(Base64.encodeBase64(this.dccEnable.getBytes(charset)));
		}
		if (null != this.hppFraudFilterMode) {
			this.hppFraudFilterMode = new String(Base64.encodeBase64(this.hppFraudFilterMode.getBytes(charset)));
		}
		if (null != this.hppVersion) {
			this.hppVersion = new String(Base64.encodeBase64(this.hppVersion.getBytes(charset)));
		}
		if (null != this.hppSelectStoredCard) {
			this.hppSelectStoredCard = new String(Base64.encodeBase64(this.hppSelectStoredCard.getBytes(charset)));
		}

		return this;
	}
127058
public static String rsaSign(String content, String privateKey, String charset,
                                 String signType) throws AlipayApiException {

        if (AlipayConstants.SIGN_TYPE_RSA.equals(signType)) {

            return rsaSign(content, privateKey, charset);
        } else if (AlipayConstants.SIGN_TYPE_RSA2.equals(signType)) {

            return rsa256Sign(content, privateKey, charset);
        } else {

            throw new AlipayApiException("Sign Type is Not Support : signType=" + signType);
        }

    }
400925
private static byte[] aes(byte[] input, byte[] key, int mode) {
		try {
			SecretKey secretKey = new SecretKeySpec(key, AES_ALG);
			Cipher cipher = Cipher.getInstance(AES_ALG);
			cipher.init(mode, secretKey);
			return cipher.doFinal(input);
		} catch (GeneralSecurityException e) {
			throw ExceptionUtil.unchecked(e);
		}
	}
828317
public int importKeysAndEncrypt(final List<ECKey> keys, KeyParameter aesKey) {
        // TODO: Firstly check if the aes key can decrypt any of the existing keys successfully.
        checkState(keyCrypter != null, "Not encrypted");
        LinkedList<ECKey> encryptedKeys = Lists.newLinkedList();
        for (ECKey key : keys) {
            if (key.isEncrypted())
                throw new IllegalArgumentException("Cannot provide already encrypted keys");
            encryptedKeys.add(key.encrypt(keyCrypter, aesKey));
        }
        return importKeys(encryptedKeys);
    }