DES JAVA source code

來源:互聯網
上載者:User
* 著作權: 廣州點訊科技有限公司 2003

          * Created on 2004-2-24 By Liudong

          */

        

         import java.security.*;

         import javax.crypto.Cipher;

         import javax.crypto.SecretKey;

         import javax.crypto.SecretKeyFactory;

         import javax.crypto.spec.DESKeySpec;

        

         /**

          * 字串工具集合

          * @author Liudong

          */

         public class StringUtils {

        

           private static final String PASSWORD_CRYPT_KEY = "__jDlog_";

         private final static String DES = "DES";

        

         /**

          * 加密

          * @param src 資料來源

          * @param key 密鑰,長度必須是8的倍數

          * @return  返回加密後的資料

          * @throws Exception

          */

         public static byte[] encrypt(byte[] src, byte[] key)throws Exception {

                 //DES演算法要求有一個可信任的隨機數源

                 SecureRandom sr = new SecureRandom();

                 // 從原始密匙資料建立DESKeySpec對象

                 DESKeySpec dks = new DESKeySpec(key);

                 // 建立一個密匙工廠,然後用它把DESKeySpec轉換成

                 // 一個SecretKey對象

                 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

                 SecretKey securekey = keyFactory.generateSecret(dks);

                 // Cipher對象實際完成加密操作

                 Cipher cipher = Cipher.getInstance(DES);

                 // 用密匙初始化Cipher對象

                 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

                 // 現在,擷取資料並加密

                 // 正式執行加密操作

                 return cipher.doFinal(src);

              }

        

              /**

              * 解密

              * @param src 資料來源

              * @param key 密鑰,長度必須是8的倍數

              * @return   返回解密後的未經處理資料

              * @throws Exception

              */

              public static byte[] decrypt(byte[] src, byte[] key)throws Exception {

                 // DES演算法要求有一個可信任的隨機數源

                 SecureRandom sr = new SecureRandom();

                 // 從原始密匙資料建立一個DESKeySpec對象

                 DESKeySpec dks = new DESKeySpec(key);

                 // 建立一個密匙工廠,然後用它把DESKeySpec對象轉換成

                 // 一個SecretKey對象

                 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

                 SecretKey securekey = keyFactory.generateSecret(dks);

                 // Cipher對象實際完成解密操作

                 Cipher cipher = Cipher.getInstance(DES);

                 // 用密匙初始化Cipher對象

                 cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

                 // 現在,擷取資料並解密

                 // 正式執行解密操作

                 return cipher.doFinal(src);

              }

           /**

            * 密碼解密

            * @param data

            * @return

            * @throws Exception

            */

           public final static String decrypt(String data){

              try {

               return new String(decrypt(hex2byte(data.getBytes()),

                  PASSWORD_CRYPT_KEY.getBytes()));

             }catch(Exception e) {

             }

             return null;

           }

           /**

            * 密碼加密

            * @param password

            * @return

            * @throws Exception

            */

           public final static String encrypt(String password){

             try {

               return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes()));
             }catch(Exception e) {

             }

             return null;

           }

         /**

          * 二行制轉字串

          * @param b

          * @return

          */

           public static String byte2hex(byte[] b) {

                 String hs = "";

                 String stmp = "";

                 for (int n = 0; n < b.length; n++) {

                     stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));

                     if (stmp.length() == 1)

                         hs = hs + "0" + stmp;

                     else

                         hs = hs + stmp;

                 }

                 return hs.toUpperCase();

            }

           

           public static byte[] hex2byte(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);

                 }

             return b2; 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.