Java DES 加密和解密源碼

來源:互聯網
上載者:User

標籤:stat   generate   根據   process   lan   簡介   content   方式   演算法   

Java密碼學結構設計遵循兩個原則:

1) 演算法的獨立性和可靠性。

2) 實現的獨立性和相互作用性。

演算法的獨立性是通過定義密碼服務類來獲得。使用者只需瞭解密碼演算法的概念,而不用去關心如何?這些概念。實現的獨立性和相互作用性通過密碼服務提供器來實現。密碼服務提供器是實現一個或多個密碼服務的一個或多個程式包。軟體開發商根據一定介面,將各種演算法實現後,打包成一個提供器,使用者可以安裝不同的提供器。安裝和配置提供器,可將包含提供器的ZIP和JAR檔案放在CLASSPATH下,再編輯Java安全屬性檔案來設定定義一個提供器。

 

DES演算法及如何利用DES演算法加密和解密類檔案的步驟:

DES演算法簡介
DES(Data Encryption Standard)是發明最早的最廣泛使用的分組對稱式加密演算法。DES演算法的入口參數有三個:Key、Data、Mode。其中Key為8個位元組共64位,是DES演算法的工作密鑰;Data也為8個位元組64位,是要被加密或被解密的資料;Mode為DES的工作方式,有兩種:加密或解密。

  標籤: <無> 程式碼片段(1)
1. [代碼]java 加密解密源碼      
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 package com.afreon.util; import java.io.IOException;import java.security.SecureRandom; import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec; import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder; public class DesUtil {     private final static String DES = "DES";     public static void main(String[] args) throws Exception {        String data = "123 456";        String key = "[email protected]#$%";        System.err.println(encrypt(data, key));        System.err.println(decrypt(encrypt(data, key), key));     }         /**     * Description 根據索引值進行加密     * @param data      * @param key  加密鍵byte數組     * @return     * @throws Exception     */    public static String encrypt(String data, String key) throws Exception {        byte[] bt = encrypt(data.getBytes(), key.getBytes());        String strs = new BASE64Encoder().encode(bt);        return strs;    }     /**     * Description 根據索引值進行解密     * @param data     * @param key  加密鍵byte數組     * @return     * @throws IOException     * @throws Exception     */    public static String decrypt(String data, String key) throws IOException,            Exception {        if (data == null)            return null;        BASE64Decoder decoder = new BASE64Decoder();        byte[] buf = decoder.decodeBuffer(data);        byte[] bt = decrypt(buf,key.getBytes());        return new String(bt);    }     /**     * Description 根據索引值進行加密     * @param data     * @param key  加密鍵byte數組     * @return     * @throws Exception     */    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {        // 產生一個可信任的隨機數源        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(data);    }              /**     * Description 根據索引值進行解密     * @param data     * @param key  加密鍵byte數組     * @return     * @throws Exception     */    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {        // 產生一個可信任的隨機數源        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(data);    }} 來源地址:http://www.yoodb.com/article/display/266

Java DES 加密和解密源碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.