赞
踩
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
-
- public class AES {
-
- public static final String KEY_ALGORITHM = "AES";
- public static final String CIPHER_ALGORITHM = "AES/CTR/PKCS5Padding";
- public static final String ivParameter = "1234123412341234";
-
- public static String Encrypt(String key, String text) {
- try {
- byte[] keyBytes = key.getBytes();
- IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
- SecretKeySpec sKeySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
- Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
- cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
- byte[] encrypted = cipher.doFinal(text.getBytes("utf-8"));
- return new BASE64Encoder().encode(encrypted);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
- public static String Decrypt(String Key, String text) {
- try {
- byte[] raw = Key.getBytes("ASCII");
- SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
- Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
- IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
- cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
- byte[] encrypted1 = new BASE64Decoder().decodeBuffer(text);
- byte[] original = cipher.doFinal(encrypted1);
- String originalString = new String(original, "utf-8");
- return originalString;
- } catch (Exception ex) {
- ex.printStackTrace();
- return null;
- }
-
- }
-
- }
如果文章写到这里,那我也太次了!!!
事情的起因是这样的,我需要调用堡垒机的接口,接口的传参是要经过aes加密的,于是乎,我咨询了厂商:
我:请问咱们使用的是哪种aes?
厂商:aes256
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。