CryptoJS v3.1.2實現md5資料加密執行個體

來源:互聯網
上載者:User

標籤:cryptojs v3.1.2   實現md5資料加密   執行個體   

CryptoJS v3.1.2: https://code.google.com/p/crypto-js/downloads/list   或  http://www.oschina.net/p/crypto-js/

            

1、項目結構


2、項目需要匯入的jar包

本項目後台用的是struts2,所以需要匯入struts2的jar包。把下面的包匯入到項目的WebRoot/WEB-INF/lib目錄下


3、項目需要匯入的js檔案


下載CryptoJS v3.1.2後,解壓,解壓後的檔案夾的根目錄下有兩個目錄,其中core-min.js 在components目錄下,md5.js在rollups目錄下。找到這兩個js檔案,然後在項目的WebRoot根目錄下建立js檔案夾,把這兩個js檔案放在WebRoot/js目錄下

4、各個檔案的源碼(1) 在src根目錄下建立包com.md5.action  , 在該包下建立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)  在src根目錄下建立xml檔案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> <!-- 配置 Struts 2 應用中的常量 -->  <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) 修改web.xml檔案 

web.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">  <!-- 設定struts 2過濾器 -->  <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>      <!-- 設定歡迎頁面 -->  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <!-- session逾時定義,單位為分鐘 -->  <session-config><session-timeout>30</session-timeout>  </session-config>  </web-app>

(4) 在WebRoot根目錄下建立分別index.jsp 、md5Demo.jsp、success.jsp

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"><html>  <head>    <base href="<%=basePath%>">    <title>首頁</title>  </head>  <body>    <a href="md5Demo.jsp">去測試md5執行個體</a>  </body></html>
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"><html>  <head>    <base href="<%=basePath%>">    <title>md5加密執行個體</title><script type="text/javascript" src="js/core-min.js"></script><script type="text/javascript" src="js/md5.js"></script><script  type="text/javascript">    //md5加密函數    function EncryptByMd5(){        //擷取表單中的密碼文字框的值    var password = document.getElementById("password").value;    //用CryptoJS庫的md5密碼編譯演算法給密碼加密    var passwordMd5 = CryptoJS.MD5(password);    //把加密後的密碼值重新賦值給表單中的密碼    document.getElementById("password").value = passwordMd5;    }</script>  </head>  <body>  <form action="md5Action" method="post">   使用者名稱:<input type="text" name="userName" id="userName" /><br />  密  碼:<input type="password" name="password" id="password" /><br />  <input type="submit" value="提交" onclick="EncryptByMd5()" /><br />  </form>  </body></html>
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"><html>  <head>    <base href="<%=basePath%>">    <title>成功跳轉</title>  </head>  <body>  使用者名稱:<s:property value="#request.userName" /><br />  密  碼:<s:property value="#request.password" /><br />  </body></html>

完成以上所有的步驟後,把項目發布到伺服器,用瀏覽器訪問md5Demo.jsp ,輸入使用者名稱、密碼後,提交,你會看到密碼加密後的資料

提交前(密碼輸入的是:123456789)



提交後

即把123456789通過md5演算法加了密後,就變成了25f9e794323b453885f5181f1b624d0b


說明:本執行個體只展示了CryptoJS v3.1.2實現md5資料加密的簡單用法。在實際項目中,例如,在使用者註冊的時候,在密碼輸入框 用上以上的md5演算法,給密碼

加密,通過表單提交給後台,幕後處理後,就把表單的內容存入到資料庫(密碼是表單裡的屬性,加了密的密碼也存進資料庫)。 使用者註冊了之後,在登入的時

候,使用者輸入密碼,這時候,只需要給使用者輸入的該密碼也用上以上密碼編譯演算法,然後再用表單提交給後台,後台就拿著表單中已經加了密的使用者輸入的密碼,跟數

據庫中已經加了密的密碼匹配驗證就行了。 而不是把資料庫裡的密碼先解密,然後再用使用者提交的沒有加密的密碼跟它匹配。 因為md5演算法是無法復原的,所以解

密後的資料可能不是原來的資料,而是別的資料,而某一資料通過md5演算法加密後的密文是唯一的。所以采有以上方法才是適合的做法。



CryptoJS v3.1.2實現md5資料加密執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.