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
- Package p;
- Import Java.security.SecureRandom;
- Import Javax.crypto.Cipher;
- Import Javax.crypto.KeyGenerator;
- Import Javax.crypto.SecretKey;
- Public class Aestest {
- public static void Main (string[] args) throws Exception {
- //TODO auto-generated method stub
- String str = "User=admin&pwd=admin";
- String key = "12345678";
- String Encrytstr;
- byte[] Encrytbyte;
- byte[] Bytere = EnCrypt (Str,key);
- ///encrypted binary array converted to 16 binary string
- Encrytstr = Parsebyte2hexstr (Bytere);
- System.out.println ("after encryption:" +ENCRYTSTR);
- ///encrypted 16 binary string converted to binary array
- Encrytbyte = Parsehexstr2byte (ENCRYTSTR);
- System.out.println ("After decryption:" +decrypt (Encrytbyte,key));
- }
- /**
- * Cryptographic functions
- * @param content Encryption
- * @param strkey Key
- * @return Returns an array of binary characters
- * @throws Exception
- */
- public static byte[] EnCrypt (String content,string strkey) throws exception{
- Keygenerator keygen;
- Secretkey Deskey;
- Cipher C;
- byte[] CByte;
- String str = content;
- keygen = keygenerator.getinstance ("AES");
- Keygen.init (+, new SecureRandom (Strkey.getbytes ()));
- Deskey = Keygen.generatekey ();
- c = cipher.getinstance ("AES");
- C.init (Cipher.encrypt_mode, Deskey);
- CByte = C.dofinal (Str.getbytes ("UTF-8"));
- return cbyte;
- }
- /** decryption Function
- * @param src-encrypted binary character array
- * @param strkey Key
- * @return
- * @throws Exception
- */
- public static String DeCrypt (byte[] src,string strkey) throws exception{
- Keygenerator keygen;
- Secretkey Deskey;
- Cipher C;
- byte[] CByte;
- keygen = keygenerator.getinstance ("AES");
- Keygen.init (+, new SecureRandom (Strkey.getbytes ()));
- Deskey = Keygen.generatekey ();
- c = cipher.getinstance ("AES");
- C.init (Cipher.decrypt_mode, Deskey);
- CByte = c.dofinal (src);
- return New String (CByte,"UTF-8");
- }
- /**2 into 16 binary
- * @param buf
- * @return
- */
- public static String parsebyte2hexstr (byte buf[]) {
- StringBuffer sb = new StringBuffer ();
- For (int i = 0; i < buf.length; i++) {
- String hex = integer.tohexstring (Buf[i] & 0xFF);
- if (hex.length () = = 1) {
- Hex = ' 0 ' + hex;
- }
- Sb.append (Hex.touppercase ());
- }
- return sb.tostring ();
- }
- /** converting 16 binary into binary
- * @param hexstr
- * @return
- */
- public static byte[] Parsehexstr2byte (String hexstr) {
- if (Hexstr.length () < 1)
- return null;
- byte[] result = new byte[hexstr.length ()/2];
- For (int i = 0;i< hexstr.length ()/2; i++) {
- int high = Integer.parseint (hexstr.substring (i*2, i*1), 16);
- int low = Integer.parseint (hexstr.substring (i*1, i*2), 16);
- Result[i] = (byte) (High * + low);
- }
- return result;
- }
- }
[]java using AES for parameter encryption of URLs