1. Encryption
1) Page Introduction JS
<script type= "Text/javascript" src= "<%=path%>/js/jquery-1.8.0.min.js" ></script>
<script type= "Text/javascript" src= "./aes/tripledes.js" ></script>
<script type= "Text/javascript" src= "./aes/mode-ecb.js" ></script>
2) DES encryption method
function encryptbydes (message, key) {var keyhex = CryptoJS.enc.Utf8.parse (key); var encrypted = CryptoJS.DES.encrypt (message, Keyhex, {mode:cryptojs.mode.ecb,padding: CRYPTOJS.PAD.PKCS7}); return encrypted.tostring ();}
3) account password encryption
var username= Onm.val (); Username = Encryptbydes (username, "kehrdooxwhcwtfesxvdvgqzq" var password= Ops.val ();p assword = Encryptbydes (password, "kehrdooxwhcwtfesxvdvgqzq" + "/ Login/login.do "" username ": Username, " Password ": password}, function // debugger; if (Result = = null "Login failed! "
2. Decryption
String username = request.getparameter ("username");
Des decrypted Account
String desusername=desutil.decryption (username, "kehrdooxwhcwtfesxvdvgqzq");
String Password = request.getparameter ("password");
Des decryption after password
String despassword=desutil.decryption (password, "Kehrdooxwhcwtfesxvdvgqzq");
Desutil.java
PackageCom.util;Importjava.security.InvalidKeyException;Importjava.security.NoSuchAlgorithmException;Importjava.security.spec.InvalidKeySpecException;Importjavax.crypto.BadPaddingException;ImportJavax.crypto.Cipher;Importjavax.crypto.IllegalBlockSizeException;Importjavax.crypto.NoSuchPaddingException;ImportJavax.crypto.SecretKey;Importjavax.crypto.SecretKeyFactory;ImportJavax.crypto.spec.DESKeySpec;/*** des plus decryption tool class * *@authorWang Xiangyu * * @date July 19, 2017 morning 11:03:51*/ Public classDesutil {Private Static FinalString des_algorithm = "DES"; /*** DES encryption * *@paramPlaindata Raw String *@paramSecretkey Encryption Key *@returnpost-encrypted String *@throwsException*/ Public StaticString encryption (String Plaindata, String secretkey)throwsException {Cipher Cipher=NULL; Try{cipher=cipher.getinstance (Des_algorithm); Cipher.init (Cipher.encrypt_mode, GenerateKey (Secretkey)); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } Catch(nosuchpaddingexception e) {e.printstacktrace (); } Catch(InvalidKeyException e) {}Try { //to prevent the decryption times javax.crypto.IllegalBlockSizeException:Input length must//Be multiple of 8 when decrypting with padded cipher exception,//the encrypted byte array cannot be converted directly to a string byte[] buf =cipher.dofinal (Plaindata.getbytes ()); returnBase64utils.encode (BUF); } Catch(illegalblocksizeexception e) {e.printstacktrace (); Throw NewException ("Illegalblocksizeexception", E); } Catch(badpaddingexception e) {e.printstacktrace (); Throw NewException ("Badpaddingexception", E); } } /*** des decryption *@paramsecretdata Password String *@paramSecretkey decryption Key *@returnRaw String *@throwsException*/ Public StaticString decryption (String secretdata, String secretkey)throwsException {Cipher Cipher=NULL; Try{cipher=cipher.getinstance (Des_algorithm); Cipher.init (Cipher.decrypt_mode, GenerateKey (Secretkey)); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); Throw NewException ("NoSuchAlgorithmException", E); } Catch(nosuchpaddingexception e) {e.printstacktrace (); Throw NewException ("Nosuchpaddingexception", E); } Catch(InvalidKeyException e) {e.printstacktrace (); Throw NewException ("InvalidKeyException", E); } Try { byte[] buf =cipher.dofinal (Base64utils.decode (Secretdata.tochararray ())); return NewString (BUF); } Catch(illegalblocksizeexception e) {e.printstacktrace (); Throw NewException ("Illegalblocksizeexception", E); } Catch(badpaddingexception e) {e.printstacktrace (); Throw NewException ("Badpaddingexception", E); } } /*** Get Secret Key * *@paramSecretkey *@return * @throwsnosuchalgorithmexception *@throwsinvalidkeyspecexception *@throwsinvalidkeyexception*/ Private Staticsecretkey GenerateKey (String secretkey)throwsnosuchalgorithmexception, Invalidkeyspecexception, invalidkeyexception {secretkeyfactory keyFactory=secretkeyfactory.getinstance (Des_algorithm); Deskeyspec KeySpec=NewDeskeyspec (Secretkey.getbytes ()); Keyfactory.generatesecret (KEYSPEC); returnKeyfactory.generatesecret (KEYSPEC); } Static Private classBase64utils {Static Private Char[] alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=". ToCharArray (); Static Private byte[] codes =New byte[256]; Static { for(inti = 0; I < 256; i++) Codes[i]=-1; for(inti = ' A '; I <= ' Z '; i++) Codes[i]= (byte) (I-' A '); for(inti = ' a '; I <= ' z '; i++) Codes[i]= (byte) (+ i-' a '); for(inti = ' 0 '; I <= ' 9 '; i++) Codes[i]= (byte) (+ I-' 0 ')); codes[' + '] = 62; codes['/'] = 63; } /*** Encode raw data as base64 encoding*/ Static PrivateString Encode (byte[] data) { Char[] out =New Char[((Data.length + 2)/3) * 4]; for(inti = 0, index = 0; i < data.length; i + = 3, index + = 4) { BooleanQuad =false; BooleanTrip =false; intval = (0xFF & (int) data[i]); Val<<= 8; if((i + 1) <data.length) {val|= (0xFF & (int) Data[i + 1]); Trip=true; } Val<<= 8; if((i + 2) <data.length) {val|= (0xFF & (int) Data[i + 2]); Quad=true; } Out[index+ 3] = alphabet[(quad? (Val & 0x3F): 64)]; Val>>= 6; Out[index+ 2] = alphabet[(trip?) (Val & 0x3F): 64)]; Val>>= 6; Out[index+ 1] = alphabet[val & 0x3F]; Val>>= 6; Out[index+ 0] = alphabet[val & 0x3F]; } return NewString (out); } /*** DECODE Base64 encoded data into raw data*/ Static Private byte[] Decode (Char[] data) { intLen = ((data.length + 3)/4) * 3; if(Data.length > 0 && data[data.length-1] = = ') --Len; if(Data.length > 1 && data[data.length-2] = = ') --Len; byte[] out =New byte[Len]; intShift = 0; intAccum = 0; intindex = 0; for(intIX = 0; IX < Data.length; ix++) { intValue = Codes[data[ix] & 0xFF]; if(Value >= 0) {Accum<<= 6; Shift+ = 6; Accum|=value; if(Shift >= 8) {Shift-= 8; Out[index++] = (byte) ((Accum >> shift) & 0xFF); } } } if(Index! =out.length)Throw NewError ("Miscalculated Data length!"); returnOut ; } }}
DES encryption algorithm