1》MD5算法(Message Digest Algorithm 5) 可以保证数据传输完整性和一致性 摘要后长度为16字节 摘要信息中不包含原文信息
所有加密结果不可逆(无法解密) 一般在传送文件时 对源文件进行md5 hash 传送到对方后 检测hash值是否相等 如果相等文件传输正确
如果不相等 说明文件被篡改(加入木马)或者未传送完成
其他MD算法 MD2(16字节)
- public static void main(String[] args) throws NoSuchAlgorithmException {
- MessageDigest md=MessageDigest.getInstance("MD5") ;
- String code="hello";
- byte[] bt=md.digest(code.getBytes());
- System.out.println(bt.length);
- }
2》SHA算法Secure Hash Algorithm(安全hash算法) 安全散列算法(hash函数 将原始信息压缩 返回散列值)可以是SHA-1,SHA1是目前最安全
的摘要算法 摘要的长度为 20字节
其他的SHA 包括 SHA-256(32字节)
- public static void main(String[] args) throws NoSuchAlgorithmException {
- MessageDigest md=MessageDigest.getInstance("SHA") ;//或者SHA-1 SHA1
- String code="hello";
- byte[] bt=md.digest(code.getBytes());
- System.out.println(bt.length);
-
- }
二。编码和解码
1》16进制 编码 计算机系统使用 2进制 为了编写存储方便一般将2进制 转换为16进制字符串 其中base64也是其中类似转换一种 16进制编码和base64都是
可逆的 一般用于存储
- public static byte[] toByte(String src){
- ByteArrayOutputStream baos=new ByteArrayOutputStream();
- for(int i=0;i<src.length();i=i+2){
- char fchar=src.charAt(i);
- char nchar=src.charAt(i+1);
- byte srcb=0;
- if(fchar=='0'){
- srcb=Byte.parseByte(nchar+"", 16);
- }else{
- srcb=(byte)(Integer.parseInt(fchar+""+nchar, 16));
- }
- baos.write(srcb);
- }
- return baos.toByteArray();
- }
-
-
- public static String toHex(byte[] src){
- StringBuffer sb=new StringBuffer();
- for(byte s:src){
- //0XFF表示 8位的 11111111 和它&后 只剩下 8位 其他位都为0
- String result=Integer.toHexString(s&0xFF);
- if(result.length()==1){
- result='0'+result;
- }
- sb.append(result);
- }
- return sb.toString();
- }
2》Base64编码 用于将字节数组和字符串互相转换
- public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
- byte[] src="hello".getBytes();
- //摘要出来的结果为字节数组 存储到数据库不方便
- MessageDigest md=MessageDigest.getInstance("SHA") ;
- byte[] bt=md.digest(src);
- //使用base64转换为字符串方便存储
- BASE64Encoder base=new BASE64Encoder();
- String str=base.encode(bt);
- System.out.println(str);
-
- //还原成字节数组
- BASE64Decoder de=new BASE64Decoder();
- byte[] bts=de.decodeBuffer(str);
- System.out.println(bt.length==bts.length);
- }
三。对称加密
1》DES算法 (Data Encryptin Standard) 是对称加密算法的一种 使用秘钥加解密 秘钥必须是56字节
概念解释:
秘钥 :用于加密和解密的钥匙 秘钥可以使用 getEncoded方法 获取byte[] 存储在文件系统中
公钥和私钥:用于非对称加密的钥匙 公钥加密 私钥解密 私钥一般用于解密所以私钥一般存储在密钥库中
口令:一般是自定义的字符串 可以通过口令和盐生成秘钥
- /**
- * 生成56字节的秘钥
- */
- public static SecretKey genKey(int len) throws NoSuchAlgorithmException{
- KeyGenerator kg=KeyGenerator.getInstance("DES");
- kg.init(len);
- return kg.generateKey();
- }
- public static void main(String[] args) throws Exception {
- //SecretKey sk=new SecretKeySpec(kl.getBytes(), "DES");
- SecretKey sk=genKey(57);
- //---------加密
- String password="tiger";
- Cipher cipher=Cipher.getInstance("DES");
- cipher.init(Cipher.ENCRYPT_MODE, sk);
- //被加密之后获取的字节数组
- byte[] mcontent=cipher.doFinal(password.getBytes());
- //---------解密
- Cipher cipher1=Cipher.getInstance("DES");
- cipher1.init(Cipher.DECRYPT_MODE, sk);
- System.out.println(new String(cipher1.doFinal(mcontent)));
-
-
- }
2》AES算法 (Advanced Encryptin Standard 高级加密标准) 是对称加密算法一种升级 因为 56位秘钥 在计算机系统性能越来越高的前提下 56位很容易被
破解 所以 AES将秘钥的长度提高到128, 192 or 256 必须是这三个数 128默认可以使用 192和256由于美国限制 需要相关授权 否则抛出异常
- public static final String AL="AES";
- /**
- * 生成56字节的秘钥
- */
- public static SecretKey genKey(int len) throws NoSuchAlgorithmException{
- KeyGenerator kg=KeyGenerator.getInstance(AL);
- kg.init(len);
- return kg.generateKey();
- }
- public static void main(String[] args) throws Exception {
- //SecretKey sk=new SecretKeySpec(kl.getBytes(), "DES");
- SecretKey sk=genKey(128);
- //---------加密
- String password="tiger";
- Cipher cipher=Cipher.getInstance(AL);
- cipher.init(Cipher.ENCRYPT_MODE, sk);
- //被加密之后获取的字节数组
- byte[] mcontent=cipher.doFinal(password.getBytes());
- //---------解密
- Cipher cipher1=Cipher<span style="margin: 0px; padding: 0px; borde