• java加密解密


    1、Base64编码
    是网络上最常见的用于传输8Bit字节代码的编码方式之一,可用于在HTTP环境下传递较长的标识信息。(来自百度百科)
    运算原理:
    3*8=4*6
    大家都知道,计算机是8位的存数,所以计算机将字符转换成二进制后,base64则会按照6位进行抽取,这样就可以将24位字符分解成4组6位的字符,然后计算机会对每一组进行高位补0,补足8位,最后转成ascii码,再对照base64的RFC2045~RFC2049表进行转换。

    加密用BASE64Encoder

    解密用BASE64Decoder

    加密前要先将String类型的内容转换为Byte[],再进行加密

    解密后的类型为byte[]数组,再转化为String类型

    代码如下:

    
    
     
     
    public class Test {
     public static void main(String[] args) throws Exception {
      Test test=new Test();
      test.testEncrypt();
      test.testDecrypt();
      
     }
     /**
      * BASE64加密
      * @throws Exception
      */
     public void testEncrypt() throws Exception {
      String pwd="123456";
      System.out.println(pwd);
      byte[] i=pwd.getBytes();//加密前,将String类型转化为byte[]
      System.out.println(i);
      String input= (new BASE64Encoder()).encodeBuffer(i); //加密 
      //String input= BaseUtil.encryptBASE64(i);//加密 
      System.out.println(input);  
     }
     /**
      * BASE64解密
      * @throws Exception
      */
     public void testDecrypt() throws Exception {
      String pwd="MTIzNDU2";
      System.out.println(pwd);
      byte[] o=(new BASE64Decoder()).decodeBuffer(pwd);//解密后的类型为byte[]
      //byte[] o=BaseUtil.decryptBASE64(pwd);//解密后的类型为byte[]
      System.out.println(o);
      String output=new String(o);//byte[]类型转换为String
      System.out.println(output);
     }
    }

    输出:

    123456
    [B@10dea4e
    MTIzNDU2
    
    MTIzNDU2 [B@
    1909752 123456

     2、DES加密解密算法。

    对称加密算法。所谓对称加密算法即:加密和解密使用相同密钥的算法。

    import java.security.Key;
    import javax.crypto.Cipher;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    public class DESTest {
     public static void main(String[] args) {
      DESTest t=new DESTest();
      String data="123456";
      String encode_data=t.encode("123456");
      System.out.println(encode_data);
      String decode_data=t.decode(encode_data);
      System.out.println(decode_data);
      
     }
     public static final String ALGORITHM_DES="DES";//指定算法名称
     public static final String SIGN_KEY="20170313EHR";
     /**
      * DES加密
      * @param data 要加密的字符串
      * @return 加密后的字符创
      */
     public String encode(String data) {
      if(data==null)
       return null;
      try {
       String key=SIGN_KEY;
       DESKeySpec dks=new DESKeySpec(key.getBytes());
       SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
       Key secretKey=keyFactory.generateSecret(dks);//获取init需要的key
       Cipher cipher=Cipher.getInstance(ALGORITHM_DES);
       cipher.init(Cipher.ENCRYPT_MODE, secretKey);//加密模式
       byte[] bytes=cipher.doFinal(data.getBytes());
       return byte2str(bytes);
      }catch(Exception e) {
       e.printStackTrace();
       return data;
      }
     }
     /**
      * DES解密
      * @param data 要解密的字符串
      * @return 解密后的字符串
      */
     public String decode(String data) {
      if(data==null)
       return null;
      try {
       String key=SIGN_KEY;
       DESKeySpec dks=new DESKeySpec(key.getBytes());
       SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
       Key secretKey=keyFactory.generateSecret(dks);//获取init需要的key
       Cipher cipher=Cipher.getInstance(ALGORITHM_DES);
       cipher.init(Cipher.DECRYPT_MODE, secretKey);//解密模式
       byte[] bytes=cipher.doFinal(str2byte(data.getBytes()));
       return new String(bytes);
      }catch(Exception e) {
       e.printStackTrace();
       return data;
      }
     }
     /**
         * 字符串转字节数组
         * @param 字符串的字节数组
         * @return 字节数组
         */
       private static byte[] str2byte(byte[] b) {  
           if((b.length%2)!=0)     //判断是否为偶数
               throw new IllegalArgumentException();  
           byte[] b2 = new byte[b.length/2];  
           for (int n = 0; n < b.length; n+=2) {  
               String item = new String(b,n,2);  
               b2[n/2] = (byte)Integer.parseInt(item,16);  //将字符串转换成16进制的byte
           }  
           return b2;  
       }
       
       /** 
        * 字节数组转字符串 
        * @param 字节数组
        * @return 转换之后的字符串
        */  
       private static String byte2str(byte[] b) {  
           StringBuilder hs = new StringBuilder();  
           String stmp;  
           for (int n = 0; b!=null && n < b.length; n++) {  
               stmp = Integer.toHexString(b[n] & 0XFF);    //将int转换成base16的字符串,
                                                           //b[n] & 0XFF将会判断b[n]的值,如果为正,则不变化,如果为负,则转换成对应的byte 的1~255的值
               if (stmp.length() == 1)  
                   hs.append('0');  
               hs.append(stmp);  
           }  
           return hs.toString().toUpperCase();  
       }
    }

     输出结果:

    1730D210F056D4D8
    123456
  • 相关阅读:
    servlet上传图片 服务器路径(转)
    图片和提交servlet的相对和绝对路径
    Intel 的面试经历中国研究院
    CentOS-6.5-x86_64 最小化安装,已安装包的总数,这些包?
    西门子PLC学习笔记8-(计时器)
    这个周末我太累了
    windows7股票的,win8残疾人,安装Han澳大利亚sinoxn个时间,sinox它支持大多数windows软体
    net.sf.json 迄今 时刻 格式 办法
    ar命令提取.a时刻,一个错误 is a fat file (use libtool(1) or lipo(1) and ar(1) on it)
    POJ 2187: Beauty Contest(旋转卡)
  • 原文地址:https://www.cnblogs.com/BonnieWss/p/9151684.html
Copyright © 2020-2023  润新知