Cryptojs v3.1.2 Implementation of MD5 data encryption instance

Source: Internet
Author: User

Cryptojs V3.1.2:https://code.google.com/p/crypto-js/downloads/list or http://www.oschina.net/p/crypto-js/

1. Project structure


2, the project needs to import the jar package

This project is struts2, so you need to import the Struts2 jar package. Import the following package into the project's Webroot/web-inf/lib directory


3, the project needs to import the JS file


After downloading Cryptojs v3.1.2, unzip, unzip the folder after the root directory has two directories, wherein core-min.js in the components directory, md5.js in the Rollups directory. Find these two JS files, and then create a new JS folder in the Webroot root directory of the project, and put the two JS files in the Webroot/js directory

4, the source code of each document (1) in the SRC root directory new package com.md5.action, under the package to establish Encryptaction.java

Encryptaction.java

Package Com.md5.action;import Com.opensymphony.xwork2.actioncontext;public class Encryptaction {private String Username;private string Password;public string GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String  EncryptByMd5 () {Actioncontext context = Actioncontext.getcontext (); Context.put ("UserName", This.username) context.put ("password", This.password); return "Success";}}

(2) Create a new XML file in the SRC root directory struts.xml

Struts.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd "><struts> <!--Configure constants in Struts 2 app--  <constant name=" struts.i18n.encoding "value = "UTF-8"/>   <package name= "MD5" extends= "Struts-default" > <action name= "md5action" class= " Com.md5.action.EncryptAction "method=" EncryptByMd5 "><result name=" Success ">/success.jsp</result> </action></package></struts>    

(3) Modify the Web. xml file

Xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">  <!--set Struts 2 Filter--  <filter>      < Filter-name>struts 2</filter-name>      <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>  </filter>  <filter-mapping>      <filter-name>struts 2</filter-name>      <url-pattern>/*</ url-pattern>  </filter-mapping>      <!--settings Welcome page--  <welcome-file-list>    < welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <!--session timeout definition in minutes-- >  <session-config><session-timeout>30</session-timeout>  </session-config>  </web-app>

(4) index.jsp, md5demo.jsp and success.jsp are established under the Webroot root directory.

index.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
md5demo.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >success.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%@ taglib prefix= "s" uri= "/struts-tags" %><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

after completing all the above steps, publish the project to the server, use the browser to access the md5demo.jsp, enter the user name, password, submit, you will see the password encrypted data

Before submission (password entered is: 123456789)



After submission

That is, the 123456789 through the MD5 algorithm is added to the secret, it becomes the 25f9e794323b453885f5181f1b624d0b


Description: This example only demonstrates the simple use of CRYPTOJS v3.1.2 to implement MD5 data encryption. In the actual project, for example, when the user registers, in the Password input box uses above the MD5 algorithm, gives the password

Encryption, through the form submitted to the background, after the background processing, the contents of the form into the database (password is the property of the form, plus a secret password also stored in the database). After the user has registered, at the time of login

Wait, the user input password, this time, only need to give the user input of the password is also used above the encryption algorithm, and then submitted to the background with the form, the background is holding the form has been added a secret user input password, with the number

The database has been added to the password matching authentication is OK. Instead of decrypting the password in the database, and then matching it with a user-submitted password that is not encrypted. because the MD5 algorithm is irreversible, the solution

Dense after the data may not be the original data, but other data, and a certain data through the MD5 algorithm encrypted ciphertext is unique. Therefore, it is appropriate to adopt the above method.



Cryptojs v3.1.2 Implementation of MD5 data encryption instance

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.