当前位置: 首页 > 图文教程 > 网络编程 > JSP > java易懂易用的MD5加密(可直接运行) (1)

JSP
我认为JSP有问题(上)
我认为JSP有问题(下)
jsp“抓”网页代码的程序
关于在bean里面打印html的利弊看法
bean里面如何打印到html页面
jdbc3中的RowSet 接口规范
Apusic Application Server1.0中jsp源代码泄漏漏洞
Unify的eWave ServletExec拒绝服务漏洞
通过提交超长的GET请求导致IBM HTTP Server远程溢出
在HTTP请求中添加特殊字符导致暴露JSP源代码文件
Resin 1.2 重要源代码暴露漏洞
多中WEB服务器的通用JSp源代码暴露漏洞
Tomcat 暴露JSP文件内容
IBM WebSphere Application Server 暴露JSP文件内容
JRun 2.3.x 范例文件暴露站点安全信息
BEA WebLogic 暴露源代码漏洞
IBM WebSphere Application Server 3.0.2 存在暴露源代码漏洞
Tomcat 3.1 存在暴露网站路径问题
Sun Java Web Server 能让攻击者远程执行任意命令
Netscape 修复 JAVA 安全漏洞

JSP 中的 java易懂易用的MD5加密(可直接运行) (1)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 93 ::
收藏到网摘: n/a

出于安全考虑,网络的传输中经常对传输数据做加密和编码处理,其中涉及以下几种 1、md5加密,该加密算法是单向加密,即加密的数据不能再通过解密还原。相关类包含在java.security.MessageDigest包中。
2、3-DES加密,该加密算法是可逆的,解密方可以通过与加密方约定的密钥匙进行解密。相关类包含在javax.crypto.*包中。
3、base64编码,是用于传输8bit字节代码最常用的编码方式。相关类在sun.misc.BASE64Decoder 和sun.misc.BASE64Encoder 中。
4、URLEncoder编码,是一种字符编码,保证被传送的参数由遵循规范的文本组成。相关类在java.net.URLEncoder包中。
细节:
1、进行MD5加密,得到byte[]
复制代码 代码如下:
5、根据需要可以去掉字符串的换行符号
复制代码 代码如下:

/**
* 去掉字符串的换行符号
* base64编码3-DES的数据时,得到的字符串有换行符号,根据需要可以去掉
*/
private String filter(String str)
{
String output = null;
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str.length(); i++)
{
int asc = str.charAt(i);
if(asc != 10 && asc != 13)
sb.append(str.subSequence(i, i + 1));
}
output = new String(sb);
return output;
}

6、对字符串进行URLDecoder.encode(strEncoding)编码
复制代码 代码如下:

/**
* 对字符串进行URLDecoder.encode(strEncoding)编码
* @param String src 要进行编码的字符串
*
* @return String 进行编码后的字符串
*/
public String getURLEncode(String src)
{
String requestValue="";
try{
requestValue = URLEncoder.encode(src);
}
catch(Exception e){
e.printStackTrace();
}
return requestValue;
}

7、对字符串进行URLDecoder.decode(strEncoding)解码
复制代码 代码如下:

/**
* 对字符串进行URLDecoder.decode(strEncoding)解码
* @param String src 要进行解码的字符串
*
* @return String 进行解码后的字符串
*/
public String getURLDecoderdecode(String src)
{
String requestValue="";
try{
requestValue = URLDecoder.decode(src);
}
catch(Exception e){
e.printStackTrace();
}
return requestValue;
}

8、进行3-DES解密(密钥匙等同于加密的密钥匙)
复制代码 代码如下:

/**
*
*进行3-DES解密(密钥匙等同于加密的密钥匙)。
* @param byte[] src 要进行3-DES解密byte[]
* @param String spkey分配的SPKEY
* @return String 3-DES解密后的String
*/
public String deCrypt(byte[] debase64,String spKey)
{
String strDe = null;
Cipher cipher = null;
try
{
cipher=Cipher.getInstance("DESede");
byte[] key = getEnKey(spKey);
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey sKey = keyFactory.generateSecret(dks);
cipher.init(Cipher.DECRYPT_MODE, sKey);
byte ciphertext[] = cipher.doFinal(debase64);
strDe = new String(ciphertext,"UTF-16LE");
}
catch(Exception ex)
{
strDe = "";
ex.printStackTrace();
}
return strDe;
经过以上步骤就可以完成MD5加密,3-DES加密、base64编码传输、base64解码、3-DES解密得到原文。