国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

java易懂易用的MD5加密(可直接運行) (1)第1/2頁

2020-07-27 21:42:39
字體:
來源:轉載
供稿:網友
1、md5加密,該加密算法是單向加密,即加密的數據不能再通過解密還原。相關類包含在java.security.MessageDigest包中。
2、3-DES加密,該加密算法是可逆的,解密方可以通過與加密方約定的密鑰匙進行解密。相關類包含在javax.crypto.*包中。
3、base64編碼,是用于傳輸8bit字節(jié)代碼最常用的編碼方式。相關類在sun.misc.BASE64Decoder 和sun.misc.BASE64Encoder 中。
4、URLEncoder編碼,是一種字符編碼,保證被傳送的參數由遵循規(guī)范的文本組成。相關類在java.net.URLEncoder包中。
細節(jié):
1、進行MD5加密,得到byte[] 
復制代碼 代碼如下:

/**
* 進行MD5加密
* @param String 原始的SPKEY
* @return byte[] 指定加密方式為md5后的byte[]
*/
private byte[] md5(String strSrc)
{
byte[] returnByte = null;
try
{
MessageDigest md5 = MessageDigest.getInstance("MD5");
returnByte = md5.digest(strSrc.getBytes("GBK"));
}
catch(Exception e)
{
e.printStackTrace();
}
return returnByte;
}

2、得到3-DES的密鑰匙 
復制代碼 代碼如下:

/**
* 得到3-DES的密鑰匙
* 根據根據需要,如密鑰匙為24個字節(jié),md5加密出來的是16個字節(jié),因此后面補8個字節(jié)的0
* @param String 原始的SPKEY
* @return byte[] 指定加密方式為md5后的byte[]
*/
private byte[] getEnKey(String spKey)
{
byte[] desKey=null;
try
{
byte[] desKey1 = md5(spKey);
desKey = new byte[24];
int i = 0;
while (i < desKey1.length && i < 24) {
desKey[i] = desKey1[i];
i++;
}
if (i < 24) {
desKey[i] = 0;
i++;
}
}
catch(Exception e){
e.printStackTrace();
}
return desKey;
}

3、3-DES加密
復制代碼 代碼如下:

/**
* 3-DES加密
* @param byte[] src 要進行3-DES加密的byte[]
* @param byte[] enKey 3-DES加密密鑰
* @return byte[] 3-DES加密后的byte[]
*/
public byte[] Encrypt(byte[] src,byte[] enKey)
{
byte[] encryptedData = null;
try
{
DESedeKeySpec dks = new DESedeKeySpec(enKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
encryptedData = cipher.doFinal(src);
}
catch(Exception e)
{
e.printStackTrace();
}
return encryptedData;
}

4、對字符串進行Base64編碼
復制代碼 代碼如下:

/**
* 對字符串進行Base64編碼
* @param byte[] src 要進行編碼的字符
*
* @return String 進行編碼后的字符串
*/
public String getBase64Encode(byte[] src)
{
String requestValue="";
try{
BASE64Encoder base64en = new BASE64Encoder();
requestValue=base64en.encode(src);
//System.out.println(requestValue);
}
catch(Exception e){
e.printStackTrace();
}

return requestValue;
}

12下一頁閱讀全文
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 曲靖市| 堆龙德庆县| 秦安县| 石渠县| 许昌县| 江陵县| 喀喇沁旗| 庐江县| 瑞安市| 佛坪县| 萨嘎县| 西林县| 贞丰县| 潍坊市| 顺平县| 荆门市| 莎车县| 双牌县| 凤山市| 德钦县| 饶阳县| 望奎县| 昌乐县| 胶州市| 十堰市| 龙州县| 廉江市| 筠连县| 河间市| 疏附县| 永修县| 定州市| 桃江县| 清徐县| 古丈县| 青铜峡市| 临清市| 固镇县| 济南市| 徐汇区| 武定县|