Encrypt ——MD5、SHA等的Java实现
package com.siwen.security;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.security.*;/**TestEncrypt.java*
·
package com.siwen.security;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.*;
/*
* TestEncrypt.java
* Author: MKing
* Last Date: 2005-11-21
* Description: A test progrm to encrypt a string using MD5 or SHA-1,etc.
*/
/**
* @author seven
* @date 2011-11-16 下午03:05:51
*/
public class TestEncrypt {
public TestEncrypt() {
}
/**
* @param strSrc is a string will be encrypted,
* @param encName is the algorithm name will be used. default to "MD5"
* @return encrypted string
*/
public String Encrypt(String strSrc, String encName) {
MessageDigest md = null;
String strDes = null;
byte[] bt = strSrc.getBytes();
try {
if (encName == null || encName.equals("")) {
encName = "MD5";
}
md = MessageDigest.getInstance(encName);
md.update(bt);
strDes = bytes2Hex(md.digest()); // to HexString
} catch (NoSuchAlgorithmException e) {
System.out.println("Invalid algorithm.");
return null;
}
return strDes;
}
/**
* @param file is a file will be encrypted,
* @param encName is the algorithm name will be used. default to "MD5"
* @return encrypted string
*/
public String Encrypt(File file, String encName) {
MessageDigest md = null;
String strDes = null;
try {
FileInputStream fis = new FileInputStream(file);
byte[] bt = new byte[fis.available()];
fis.read(bt);
System.out.println("bt.length:"+bt.length);
if (encName == null || encName.equals("")) {
encName = "MD5";
}
md = MessageDigest.getInstance(encName);
md.update(bt);
strDes = bytes2Hex(md.digest()); // to HexString
} catch (NoSuchAlgorithmException e) {
System.out.println("Invalid algorithm.");
return null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strDes;
}
public String bytes2Hex(byte[] bts) {
String des = "";
String tmp = null;
for (int i = 0; i < bts.length; i++) {
tmp = (Integer.toHexString(bts[i] & 0xFF));
if (tmp.length() == 1) {
des += "0";
}
des += tmp;
}
return des;
}
public static void main(String[] args) {
TestEncrypt te = new TestEncrypt();
String strSrc = "可以加密汉字.Oh,and english";
System.out.println("Source String:" + strSrc);
System.out.println("Encrypted String:");
System.out.println("Use Def:" + te.Encrypt(strSrc, null));
System.out.println("Use MD5:" + te.Encrypt(strSrc, "MD5"));
System.out.println("Use SHA:" + te.Encrypt(strSrc, "SHA-1"));
System.out.println("Use SHA-256:" + te.Encrypt(strSrc, "SHA-256"));
File file = new File("C:\\Documents and Settings\\Administrator\\" +
".m2\\repository\\javax\\servlet\\com.springsource.javax.servlet.jsp.jstl\\1.1.2\\" +
"com.springsource.javax.servlet.jsp.jstl-1.1.2.jar");
System.out.println("加密文件:"+file.getName());
System.out.println("Use MD5:" + te.Encrypt(file, "MD5"));
System.out.println("Use SHA:" + te.Encrypt(file, "SHA-1"));
}
}

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)