常用的加密算法

来源:互联网 发布:芭碧琪面膜怎么样 知乎 编辑:程序博客网 时间:2024/06/16 19:12
一。摘要算法

    1》MD5算法(Message Digest Algorithm 5) 可以保证数据传输完整性和一致性 摘要后长度为16字节 摘要信息中不包含原文信息

所有加密结果不可逆(无法解密) 一般在传送文件时 对源文件进行md5 hash 传送到对方后 检测hash值是否相等 如果相等文件传输正确

如果不相等 说明文件被篡改(加入木马)或者未传送完成

    其他MD算法 MD2(16字节)

     

[html] view plain copy
  1. public static void main(String[] args) throws NoSuchAlgorithmException {  
  2.         MessageDigest md=MessageDigest.getInstance("MD5") ;  
  3.         String code="hello";  
  4.         byte[] bt=md.digest(code.getBytes());  
  5.         System.out.println(bt.length);  
  6.     }  
    2》SHA算法Secure Hash Algorithm(安全hash算法) 安全散列算法(hash函数 将原始信息压缩 返回散列值)可以是SHA-1,SHA1是目前最安全

的摘要算法 摘要的长度为 20字节 

     其他的SHA 包括 SHA-256(32字节)

    

[html] view plain copy
  1. public static void main(String[] args) throws NoSuchAlgorithmException {  
  2.         MessageDigest md=MessageDigest.getInstance("SHA") ;//或者SHA-1 SHA1  
  3.         String code="hello";  
  4.         byte[] bt=md.digest(code.getBytes());  
  5.         System.out.println(bt.length);  
  6.           
  7.     }  

二。编码和解码

   1》16进制 编码  计算机系统使用 2进制 为了编写存储方便一般将2进制 转换为16进制字符串 其中base64也是其中类似转换一种 16进制编码和base64都是

可逆的 一般用于存储

[html] view plain copy
  1. public static byte[] toByte(String src){  
  2.         ByteArrayOutputStream baos=new ByteArrayOutputStream();  
  3.         for(int i=0;i<src.length();i=i+2){  
  4.             char fchar=src.charAt(i);  
  5.             char nchar=src.charAt(i+1);  
  6.             byte srcb=0;  
  7.             if(fchar=='0'){  
  8.                 srcb=Byte.parseByte(nchar+"", 16);  
  9.             }else{  
  10.                 srcb=(byte)(Integer.parseInt(fchar+""+nchar, 16));  
  11.             }  
  12.             baos.write(srcb);  
  13.         }  
  14.         return baos.toByteArray();  
  15.     }  
  16.       
  17.       
  18.     public static String toHex(byte[] src){  
  19.         StringBuffer sb=new StringBuffer();  
  20.         for(byte s:src){  
  21.             //0XFF表示  8位的 11111111  和它&后 只剩下 8位 其他位都为0  
  22.             String result=Integer.toHexString(s&0xFF);  
  23.             if(result.length()==1){  
  24.                 result='0'+result;  
  25.             }  
  26.             sb.append(result);  
  27.         }  
  28.         return sb.toString();  
  29.     }  

  

 2》Base64编码  用于将字节数组和字符串互相转换

[html] view plain copy
  1. public static void main(String[] args) throws NoSuchAlgorithmException, IOException {  
  2.         byte[] src="hello".getBytes();  
  3.         //摘要出来的结果为字节数组  存储到数据库不方便  
  4.         MessageDigest md=MessageDigest.getInstance("SHA") ;  
  5.         byte[] bt=md.digest(src);  
  6.         //使用base64转换为字符串方便存储  
  7.         BASE64Encoder base=new BASE64Encoder();  
  8.         String str=base.encode(bt);  
  9.         System.out.println(str);  
  10.           
  11.         //还原成字节数组  
  12.         BASE64Decoder de=new BASE64Decoder();  
  13.         byte[] bts=de.decodeBuffer(str);  
  14.         System.out.println(bt.length==bts.length);  
  15.     }  

三。对称加密

   1》DES算法 (Data Encryptin Standard) 是对称加密算法的一种 使用秘钥加解密  秘钥必须是56字节  

      概念解释:

            秘钥 :用于加密和解密的钥匙  秘钥可以使用 getEncoded方法 获取byte[] 存储在文件系统中

            公钥和私钥:用于非对称加密的钥匙 公钥加密 私钥解密  私钥一般用于解密所以私钥一般存储在密钥库中

            口令:一般是自定义的字符串  可以通过口令和盐生成秘钥 

[html] view plain copy
  1. /**  
  2.      * 生成56字节的秘钥  
  3.      */  
  4.     public static SecretKey genKey(int len) throws NoSuchAlgorithmException{  
  5.         KeyGenerator kg=KeyGenerator.getInstance("DES");  
  6.         kg.init(len);  
  7.         return kg.generateKey();  
  8.     }  
  9.     public static void main(String[] args) throws Exception {  
  10.         //SecretKey sk=new SecretKeySpec(kl.getBytes(), "DES");  
  11.         SecretKey sk=genKey(57);  
  12.         //---------加密  
  13.         String password="tiger";  
  14.         Cipher cipher=Cipher.getInstance("DES");  
  15.         cipher.init(Cipher.ENCRYPT_MODE, sk);  
  16.         //被加密之后获取的字节数组  
  17.         byte[] mcontent=cipher.doFinal(password.getBytes());  
  18.         //---------解密  
  19.         Cipher cipher1=Cipher.getInstance("DES");  
  20.         cipher1.init(Cipher.DECRYPT_MODE, sk);  
  21.         System.out.println(new String(cipher1.doFinal(mcontent)));  
  22.           
  23.       
  24.     }  

   2》AES算法 (Advanced Encryptin Standard 高级加密标准) 是对称加密算法一种升级 因为 56位秘钥 在计算机系统性能越来越高的前提下 56位很容易被

  破解 所以 AES将秘钥的长度提高到128, 192 or 256  必须是这三个数 128默认可以使用  192和256由于美国限制 需要相关授权 否则抛出异常

[html] view plain copy
  1. public static final String AL="AES";  
  2.     /**  
  3.      * 生成56字节的秘钥  
  4.      */  
  5.     public static SecretKey genKey(int len) throws NoSuchAlgorithmException{  
  6.         KeyGenerator kg=KeyGenerator.getInstance(AL);  
  7.         kg.init(len);  
  8.         return kg.generateKey();  
  9.     }  
  10.     public static void main(String[] args) throws Exception {  
  11.         //SecretKey sk=new SecretKeySpec(kl.getBytes(), "DES");  
  12.         SecretKey sk=genKey(128);  
  13.         //---------加密  
  14.         String password="tiger";  
  15.         Cipher cipher=Cipher.getInstance(AL);  
  16.         cipher.init(Cipher.ENCRYPT_MODE, sk);  
  17.         //被加密之后获取的字节数组  
  18.         byte[] mcontent=cipher.doFinal(password.getBytes());  
  19.         //---------解密  
  20.         Cipher cipher1=Cipher<span style="margin: 0px; padding: 0px; borde
原创粉丝点击