密码管理
大家一定对自己的密码时刻保持的警惕,谁都不想让自己的密码轻易的让第二个人知道,随着互联网的不断发展,大家在各种各样的平台设置密码,随着而来的密码安全问题,又一次来到我们的视线,如何保护自己的注册用户的个人密码安全,相信所有平台都一直在努力。记得前段时间网上曝出小米网的数据库被盗了,估计很多人听到这个消息,死的心都有了,不过好的是小米公司一开始就对密码安全做了处理,以至于虽然数据被盗了,但你的密码仍然是加密的。通过这件事我更加认识到密码安全的重要性,所以现在就把我刚刚写的一个数据加密的算法分享给大家。
加密算法:
public class ALG { int m ; String mima=""; char [] pa = new char [10]; char [] pawd = new char [10]; char [] abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; char [] ABC = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; char [] num = {'0','1','2','3','4','5','6','7','8','9'}; public String getEncrypt(String pass){ int len = pass.length(); for(int i=0; i<len; i++){ pa[i]=pass.charAt(i); /** * 判断密码中是否存在特殊字符 */ switch (pa[i]) { case '~': return mima; case '`': return mima; case '!': return mima; case '#': return mima; case '$': return mima; case '%': return mima; case '^': return mima; case '&': return mima; case '*': return mima; case '(': return mima; case ')': return mima; case '-': return mima; case '_': return mima; case '+': return mima; case '=': return mima; case '|': return mima; case '\\': return mima; case '、': return mima; case '{': return mima; case '}': return mima; case '【': return mima; case '】': return mima; case '[': return mima; case ']': return mima; case ';': return mima; case ';': return mima; case ':': return mima; case ':': return mima; case '‘': return mima; case '’': return mima; case '\'': return mima; case '“': return mima; case '”': return mima; case '<': return mima; case '>': return mima; case '?': return mima; case ',': return mima; case ',': return mima; case '。': return mima; case '.': return mima; case '/': return mima; case ' ': return mima; } } for(int j=0; j<len; j++){ for(int a=0; a<abc.length; a++){ if(pa[j]==abc[a]){ m = (a+5)%26; mima = mima + ABC[m]; } } for(int A=0; A<ABC.length; A++){ if(pa[j]==ABC[A]){ m = (A+5)%26; mima = mima + abc[m]; } } for(int n=0; n<num.length; n++){ if(pa[j]==num[n]){ m = (n+3)%10; mima = mima + num[m]; } } if(pa[j]=='@'){ mima = mima + "_!#!"; } } int length = mima.length(); for(int i=0; i<length; i++){ pawd[i]=mima.charAt(i); } mima = ""; for(int i=length-1; i>=0; i--){ mima = mima + pawd[i]; } return mima; } }
之前我是用jsp测试的,所以我把select和jsp都给大家分享一下
select代码:
public class encrypt extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); String pass = request.getParameter("pass"); ALG alg = new ALG(); String mima = alg.getEncrypt(pass); if(!mima.equals("")){ request.setAttribute("pass", pass); request.setAttribute("mima", mima); request.getRequestDispatcher("encrypt.jsp").forward(request, response); }else{ request.setAttribute("pass", pass); request.setAttribute("news", "密码请不要包含特殊字符!"); request.getRequestDispatcher("index.jsp").forward(request, response); // response.sendRedirect("index.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> <%String news = (String)request.getAttribute("news");String pass = (String)request.getAttribute("pass"); %> </head> <body> <form action="encrypt.html" method="post"> 密码:<input type="text" name="pass" value="<%if(pass!=null){%><%=pass%><%} %>"><%if(news!=null){ %> <%=news%> <%} %> <input type="submit" value="加密"> </form> </body> </html>
encrypt.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> <%String mima = (String)request.getAttribute("mima");String pass = (String)request.getAttribute("pass"); %> </head> <body> <h1>密码:<%=pass %></h1> <h1>加密结果:<%=mima %></h1> </body> </html>
功底有限,仅仅是一个很简单的加密算法,大家欣赏之余,如有更好的思路,还望赐教。
--------------------以上如有不妥,请留言。谢谢-----------------------