当前位置:   article > 正文

AES加密CTR模式_aes ctr

aes ctr
  1. import javax.crypto.Cipher;
  2. import javax.crypto.spec.IvParameterSpec;
  3. import javax.crypto.spec.SecretKeySpec;
  4. import sun.misc.BASE64Decoder;
  5. import sun.misc.BASE64Encoder;
  6. public class AES {
  7. public static final String KEY_ALGORITHM = "AES";
  8. public static final String CIPHER_ALGORITHM = "AES/CTR/PKCS5Padding";
  9. public static final String ivParameter = "1234123412341234";
  10. public static String Encrypt(String key, String text) {
  11. try {
  12. byte[] keyBytes = key.getBytes();
  13. IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
  14. SecretKeySpec sKeySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
  15. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
  16. cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
  17. byte[] encrypted = cipher.doFinal(text.getBytes("utf-8"));
  18. return new BASE64Encoder().encode(encrypted);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. return null;
  22. }
  23. }
  24. public static String Decrypt(String Key, String text) {
  25. try {
  26. byte[] raw = Key.getBytes("ASCII");
  27. SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
  28. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
  29. IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
  30. cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
  31. byte[] encrypted1 = new BASE64Decoder().decodeBuffer(text);
  32. byte[] original = cipher.doFinal(encrypted1);
  33. String originalString = new String(original, "utf-8");
  34. return originalString;
  35. } catch (Exception ex) {
  36. ex.printStackTrace();
  37. return null;
  38. }
  39. }
  40. }

如果文章写到这里,那我也太次了!!!

事情的起因是这样的,我需要调用堡垒机的接口,接口的传参是要经过aes加密的,于是乎,我咨询了厂商:

我:请问咱们使用的是哪种aes?

厂商:aes256

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/176627
推荐阅读
相关标签