zoukankan      html  css  js  c++  java
  • java Encryption&Decryption

    The encryption class:

    package cn.com.smartcost.qy.util;
    
    import java.security.Key;  
    import java.security.Security;  
      
    import javax.crypto.Cipher;  
      
    /** 
     * encrypt and decryption
     * @author wangfeng 
     * @since 2013-4-27 15:50:26 
     * @version 1.0 
     * 
     */  
    public class EncryptionDecryption {  
        private static String strDefaultKey = "wfkey";  
          
      //encrypt 
        private Cipher encryptCipher = null;  
          
      // decryption  
        private Cipher decryptCipher = null;  
          
        /** 
         * byte array to hexadecimal
         * @param arrB 
         * @return 16 
         * @throws Exception 
         */  
        public static String byteArr2HexStr(byte[] arrB) throws Exception{  
            int bLen = arrB.length;  
            //
            StringBuffer strBuffer = new StringBuffer(bLen*2);  
            for(int i=0; i != bLen; ++i){  
                int intTmp = arrB[i];  
                //
                while(intTmp < 0){  
                    intTmp = intTmp + 256;//
                }  
                //
                if(intTmp < 16){  
                    strBuffer.append("0");  
                }  
                strBuffer.append(Integer.toString(intTmp,16));  
            }  
            return strBuffer.toString();  
        }  
          
          
        /** 
         * hex to byte array
         * @param hexStr 
         * @return 
         * @throws Exception 
         */  
        public static byte[] hexStr2ByteArr(String hexStr) throws Exception{  
            byte[] arrB = hexStr.getBytes();  
            int bLen = arrB.length;  
            byte[] arrOut = new byte[bLen/2];  
            for(int i=0; i<bLen; i = i+2){  
                String strTmp = new String(arrB,i,2);  
                arrOut[i/2] = (byte)Integer.parseInt(strTmp,16);  
            }  
            return arrOut;  
        }  
          
        /** 
         * encrypt
         * @throws Exception 
         */  
        public EncryptionDecryption() throws Exception {  
            this(strDefaultKey);  
        }  
      
          
          
        /** 
         * encryption
         * @param strKey
         * @throws Exception 
         */  
           @SuppressWarnings("restriction")  
        public EncryptionDecryption(String strKey) throws Exception {  
                Security.addProvider(new com.sun.crypto.provider.SunJCE());  
                Key key = getKey(strKey.getBytes());  
      
                encryptCipher = Cipher.getInstance("DES");  
                encryptCipher.init(Cipher.ENCRYPT_MODE, key);  
      
                decryptCipher = Cipher.getInstance("DES");  
                decryptCipher.init(Cipher.DECRYPT_MODE, key);  
            }  
      
        /** 
         * encrypt
         * @param arrB 
         * @return 
         * @throws Exception 
         */  
        public byte[] encrypt(byte[] arrB) throws Exception{  
            return encryptCipher.doFinal(arrB);  
        }  
          
        /** 
         * encrypt
         * @param strIn 
         * @return
         * @throws Exception 
         */  
        public String encrypt(String strIn) throws Exception{  
            return byteArr2HexStr(encrypt(strIn.getBytes()));  
        }  
          
        /** 
         * decrypt
         * @param arrB 
         * @return  
         * @throws Exception 
         */  
        public byte[] decrypt(byte[] arrB) throws Exception{  
            return decryptCipher.doFinal(arrB);  
        }  
          
        /** 
         * decrypt 
         * @param strIn 
         * @return 
         * @throws Exception 
         */  
        public String decrypt(String strIn) throws Exception{  
            try{  
                return new String(decrypt(hexStr2ByteArr(strIn)));  
            }catch (Exception e) {  
                return "";  
            }  
        }  
          
          
        /** 
         * get the key
         * @param arrBTmp 
         * @return 
         * @throws Exception 
         */  
        private Key getKey(byte[] arrBTmp) throws Exception{  
            byte[] arrB = new byte[8]; //  
            for(int i=0; i<arrBTmp.length && i < arrB.length; ++i){  
                arrB[i] = arrBTmp[i];  
            }  
              
            //
            Key key = new javax.crypto.spec.SecretKeySpec(arrB,"DES");  
            return key;  
        }  
          
    } 
    

      Attention:

    This class nees a jar:sunjce_provider.jar. u can download it from here: http://pan.baidu.com/s/1ntqkU4h

    To use:

    private EncryptionDecryption des = new  EncryptionDecryption("wang");
    //encrypt
    String password = "123456";
    password = des.encrypt(password);
    //decrypt
    password =des.decrypt(password);
    

      

  • 相关阅读:
    在python3中如何加载静态文件详版步骤
    django 过滤器总结
    通过虚拟环境创建并开始一个django
    关于django中模板的理解
    python 初学 正则表达式入门
    python 初学 错误类型以及编码规范
    获取地址的经纬度,根据经纬度反查地址
    mybatisz中一个可以替代between..and 的技巧
    linux指令和文件系统
    auto.js入门笔记
  • 原文地址:https://www.cnblogs.com/howlaa/p/4140126.html
Copyright © 2011-2022 走看看