1:消息摘要:(数字指纹):既对一个任意长度的一个数据块进行计算,产生一个唯一指纹。MD5/SHA1
发送给其他人你的信息和摘要,其他人用相同的加密方法得到摘要,最后进行比较摘要是否相同。
MD5(Message Digest algorithm 5,信息摘要算法)
SHA(Secure Hash Algorithm,安全散列算法)
SHA(Secure Hash Algorithm,安全散列算法)
2:单匙密码体制:DES:比较简便高效,密钥简短,加解密速度快,破译极其困难,但其安全性依赖于密匙的安全性。
DES(Data Encryption Standard)是发明最早的最广泛使用的分组对称加密算法。DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密
3:数字签名:就是信息发送者用其私钥对从所传报文中提取出的特征数据(或称数字指纹)进行RSA算法操作,以保证发信人无法抵赖曾发过该信息(即不可抵赖性),同时也确保信息报文在经签名后末被篡改(即完整性)。当信息接收者收到报文后,就可以用发送者的公钥对数字签名进行验证。
代表:DSA
4:非对称密匙密码体制(公匙体系):加密密匙不同于解密密匙,加密密匙公之于众,谁都可以使用,解密密匙只有解密人自己知道。代表:RSA
二、使用Commons-codec加密
Apache Commons家族中的Commons Codec就提供了一些公共的编解码实现,比如Base64, Hex, MD5,Phonetic and URLs等等
加密算法也有很多,比较常用的有MD5和SHA-1。在Java中通过
java.security.MessageDigest类可以实现MD5和SHA-1加密算法。
import org.apache.commons.codec.digest.DigestUtils; //导入处理加密的类
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String passwod="123456";
System.out.println(DigestUtils.md5Hex(passwod)); //md5加密
System.out.println(DigestUtils.sha256Hex(passwod)); //
}
}
public class PasswordEncode { public final static String MD5 = "MD5"; public final static String NONE = "NONE"; public final static String SHA_256 = "SHA-256"; public final static String SHA_512 = "SHA-512"; public final static String SHA_384 = "SHA-384"; /** * 加密文件算法 * * @param filename * 需要加密的文件名 * @param algorithm * 加密算法名 */ public static void digestFile(String filename, String algorithm) { byte[] b = new byte[1024 * 4]; int len = 0; FileInputStream fis = null; FileOutputStream fos = null; try { MessageDigest md = MessageDigest.getInstance(algorithm); fis = new FileInputStream(filename); while ((len = fis.read(b)) != -1) { md.update(b, 0, len); } byte[] digest = md.digest(); StringBuffer fileNameBuffer = new StringBuffer(128).append(filename).append(".").append(algorithm); fos = new FileOutputStream(fileNameBuffer.toString()); OutputStream encodedStream = new Base64OutputStream(fos); encodedStream.write(digest); encodedStream.flush(); encodedStream.close(); } catch (Exception e) { System.out.println("Error computing Digest: " + e); } finally { try { if (fis != null) fis.close(); } catch (Exception ignored) { } try { if (fos != null) fos.close(); } catch (Exception ignored) { } } } /** * 加密密码算法 * * @param pass * 需要加密的原始密码 * @param algorithm * 加密算法名称 * @return 加密后的密码 * @throws NoSuchAlgorithmException * 当加密算法不可用时抛出此异常 */ public static String digestString(String password, String alg) throws NoSuchAlgorithmException { String newPass; if (alg == null || MD5.equals(alg)) { newPass = DigestUtils.md5Hex(password); } else if (NONE.equals(alg)) { newPass = password; } else if (SHA_256.equals(alg)) { newPass = DigestUtils.sha256Hex(password); } else if (SHA_384.equals(alg)) { newPass = DigestUtils.sha384Hex(password); } else if (SHA_512.equals(alg)) { newPass = DigestUtils.sha512Hex(password); } else { newPass = DigestUtils.shaHex(password); } return newPass; } /** * 加密密码算法,默认的加密算法是MD5 * * @param password * 未加密的密码 * @return String 加密后的密码 */ public static String digestPassword(String password) { try { if (password != null && !"".equals(password)) { return digestString(password, MD5); } else return null; } catch (NoSuchAlgorithmException nsae) { throw new RuntimeException("Security error: " + nsae); } } /** * 判断密码是不是相等,默认的加密算法是MD5 * * @param beforePwd * 要判断的密码 * @param afterPwd * 加密后的数据库密码 * @return Boolean true 密码相等 */ public static boolean isPasswordEnable(String beforePwd, String afterPwd) { if (beforePwd != null && !"".equals(beforePwd)) { String password = digestPassword(beforePwd); return afterPwd.equals(password); } else return false; } public static void main(String[] args) throws NoSuchAlgorithmException { System.out.println(PasswordEncode.digestPassword("123456")); System.out.println(PasswordEncode.digestString("123456", PasswordEncode.MD5)); PasswordEncode.digestFile("C:\\Users\\user\\Desktop\\PasswordEncode.java", PasswordEncode.SHA_512); System.out.println(PasswordEncode.isPasswordEnable("123456", PasswordEncode.digestPassword("123456"))); } }