[]java using AES for parameter encryption of URLs

Source: Internet
Author: User

Original address: http://h5566h.iteye.com/blog/1465426

Many times need to be in the URL parameter, I hope the URL parameters can be encrypted, here I combine the article http://www.2cto.com/kf/201112/114046.html provide ideas, and then combined with the Java ASE Encryption implementation, wrote the following code:

The code mainly considers two problems: 1, encrypted characters must be able to use URL transmission 2, encryption algorithm must be symmetric algorithm, through the private key can be decrypted

In addition: Why the code to convert binary into 16, because the force of the byte array into a string, there will be garbled, the second is forced to convert the string, and then back to the byte array, the binary will change, Moreover, the binary bits are not multiples of 16 (the size of the input binary array in the decryption algorithm must be a multiple of 16). Therefore, binary conversions are required

The code is as follows:

Java code
  1. Package p;
  2. Import Java.security.SecureRandom;
  3. Import Javax.crypto.Cipher;
  4. Import Javax.crypto.KeyGenerator;
  5. Import Javax.crypto.SecretKey;
  6. Public class Aestest {
  7. public static void Main (string[] args) throws Exception {
  8. //TODO auto-generated method stub
  9. String str = "User=admin&pwd=admin";
  10. String key = "12345678";
  11. String Encrytstr;
  12. byte[] Encrytbyte;
  13. byte[] Bytere = EnCrypt (Str,key);
  14. ///encrypted binary array converted to 16 binary string
  15. Encrytstr = Parsebyte2hexstr (Bytere);
  16. System.out.println ("after encryption:" +ENCRYTSTR);
  17. ///encrypted 16 binary string converted to binary array
  18. Encrytbyte = Parsehexstr2byte (ENCRYTSTR);
  19. System.out.println ("After decryption:" +decrypt (Encrytbyte,key));
  20. }
  21. /** 
  22. * Cryptographic functions
  23. * @param content Encryption
  24. * @param strkey Key
  25. * @return Returns an array of binary characters
  26. * @throws Exception
  27. */
  28. public static byte[] EnCrypt (String content,string strkey) throws exception{
  29. Keygenerator keygen;
  30. Secretkey Deskey;
  31. Cipher C;
  32. byte[] CByte;
  33. String str = content;
  34. keygen = keygenerator.getinstance ("AES");
  35. Keygen.init (+, new SecureRandom (Strkey.getbytes ()));
  36. Deskey = Keygen.generatekey ();
  37. c = cipher.getinstance ("AES");
  38. C.init (Cipher.encrypt_mode, Deskey);
  39. CByte = C.dofinal (Str.getbytes ("UTF-8"));
  40. return cbyte;
  41. }
  42. /** decryption Function
  43. * @param src-encrypted binary character array
  44. * @param strkey Key
  45. * @return
  46. * @throws Exception
  47. */
  48. public static String DeCrypt (byte[] src,string strkey) throws exception{
  49. Keygenerator keygen;
  50. Secretkey Deskey;
  51. Cipher C;
  52. byte[] CByte;
  53. keygen = keygenerator.getinstance ("AES");
  54. Keygen.init (+, new SecureRandom (Strkey.getbytes ()));
  55. Deskey = Keygen.generatekey ();
  56. c = cipher.getinstance ("AES");
  57. C.init (Cipher.decrypt_mode, Deskey);
  58. CByte = c.dofinal (src);
  59. return New String (CByte,"UTF-8");
  60. }
  61. /**2 into 16 binary
  62. * @param buf
  63. * @return
  64. */
  65. public static String parsebyte2hexstr (byte buf[]) {
  66. StringBuffer sb = new StringBuffer ();
  67. For (int i = 0; i < buf.length; i++) {
  68. String hex = integer.tohexstring (Buf[i] & 0xFF);
  69. if (hex.length () = = 1) {
  70. Hex = ' 0 ' + hex;
  71. }
  72. Sb.append (Hex.touppercase ());
  73. }
  74. return sb.tostring ();
  75. }
  76. /** converting 16 binary into binary
  77. * @param hexstr
  78. * @return
  79. */
  80. public static byte[] Parsehexstr2byte (String hexstr) {
  81. if (Hexstr.length () < 1)
  82. return null;
  83. byte[] result = new byte[hexstr.length ()/2];
  84. For (int i = 0;i< hexstr.length ()/2; i++) {
  85. int high = Integer.parseint (hexstr.substring (i*2, i*1), 16);
  86. int low = Integer.parseint (hexstr.substring (i*1, i*2), 16);
  87. Result[i] = (byte) (High * + low);
  88. }
  89. return result;
  90. }
  91. }

[]java using AES for parameter encryption of URLs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.