Des 對稱式加密演算法 加密\解密執行個體

來源:互聯網
上載者:User

標籤:

Des 加密相關類介紹:
SecureRandom  這個類是繼承自java.util.Random 這個類
SecureRandom 這個類的構造器有三種,下面例舉兩種:
SecureRandom()構造一個實現預設隨機數演算法的安全隨機數產生器 (RNG)。
SecureRandom(byte[] seed)構造一個實現預設隨機數演算法的安全隨機數產生器 (RNG)。

DESKeySpec 這個類是用來使用原始秘鑰來產生秘鑰的秘鑰內容
DESKeySpec 有兩個建構函式:
DESKeySpec(byte[] key) 建立一個 DESKeySpec 對象,使用 key 中的前 8 個位元組作為 DES 密鑰的金鑰產製原料。
DESKeySpec(byte[] key, int offset) 建立一個 DESKeySpec 對象,使用 key 中始於且包含 offset 的前 8 個位元組作為 DES-EDE 密鑰的金鑰產製原料。
SecretKeyFactory , 密鑰工廠用來將密鑰(類型 Key 的不透明加密金鑰)轉換為密鑰規範(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。

SecretKey對象,秘鑰對象,通過調用秘鑰工廠的generateSecret(DESKeySpec deskeyspace) 方法來產生秘鑰
Cipher 類為加密和解密提供密碼功能,通過調用Cipher的getInstance("des") 來擷取執行個體
Cipher 對象調用init() 方法進行對象的初始化,init() 方法的具體參數按照具體情況而定,有加密的也有解密的常量

最後調用Cipher的doFinal() 方法進行加密解密。


在這裡請教大家一個問題,不管是第一種使用BASE64Encoder編碼還是第二種org.apache.commons.codec.binary.Base64 

編碼,在將String 轉化為byte以及將byte轉化為String 時需要 UTF-8/GBK 等編碼來編碼,解碼嗎?

       

一、使用了  sun.misc.BASE64Decoder 和BASE64Encoder 進行解碼,編碼

package com.soufun.com;import java.io.IOException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.util.Date;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;// 匯入sun的64位編碼import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/** *@author WHD *data 2015年8月16日 *即使匯入sun.misc這個架包也會報錯,這時首先把你的JRE架包移除再匯入一次就可以了 */public class DesUtil {// 定義加密方式 private final static String DES = "DES"; private final static String UTF8="GBK"; static SecretKeyFactory keyFactory = null;static {try {keyFactory=SecretKeyFactory.getInstance("DES");} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}}    public static void main(String[] args) throws Exception {    long begin=new Date().getTime();    String data = "aaades加密測試";    // 注意:DES加密和解密過程中,密鑰長度都必須是8的倍數        String key = "qazwsxed";        System.err.println(encrypt(data, key));        System.err.println(decrypt(encrypt(data, key), key));        long end =new Date().getTime();        System.out.println(end-begin);    }         /**     * Description 根據索引值進行加密     * @param data      * @param key  加密鍵byte數組     * @return     * @throws Exception     */    public static String encrypt(String data, String key) throws Exception {    // 使用指定的編碼擷取要加密的內容,一般秘鑰都是字母或數字不用指定編碼,但指定也可以        byte[] bt = encrypt(data.getBytes(UTF8), key.getBytes(UTF8));        //注意:在加密和解密的時候使用sun的BASE64Encoder()進行編碼和解碼不然會有亂碼        //網上查看了很多執行個體,都沒有編碼和解碼,也說沒有亂碼問題,而我這裡出現了亂碼,所以使用BASE64Encoder()進行了編碼解碼        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;        //注意:在加密和解密的時候使用sun的BASE64Encoder()進行編碼和解碼不然會有亂碼        BASE64Decoder decoder = new BASE64Decoder();        byte[] buf = decoder.decodeBuffer(data);        byte[] bt = decrypt(buf,key.getBytes());        return new String(bt,UTF8);    }     /**     * 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);        // 密鑰工廠用來將密鑰(類型 Key 的不透明加密金鑰)轉換為密鑰規範(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。        // 這裡改為使用單例模式        //SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);        //根據提供的密鑰規範(密鑰材料)產生 SecretKey(秘鑰) 對象。        SecretKey securekey = keyFactory.generateSecret(dks);        // Cipher對象實際完成加密操作,此類為加密和解密提供密碼功能        Cipher cipher = Cipher.getInstance(DES);        // 用密鑰和隨機源初始化此 Cipher。ENCRYPT_MODE用於將 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);        // 密鑰工廠用來將密鑰(類型 Key 的不透明加密金鑰)轉換為密鑰規範(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。        // 這裡改為使用單例模式        //SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);        //根據提供的密鑰規範(密鑰材料)產生 SecretKey(秘鑰)對象。        SecretKey securekey = keyFactory.generateSecret(dks);        // Cipher類為加密和解密提供密碼功能        Cipher cipher = Cipher.getInstance(DES);        // DECRYPT_MODE用於將 Cipher 初始化為解密模式的常量。        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);        // 正式進行解密操作         return cipher.doFinal(data);    }}

二、使用org.apache.commons.codec.binary.Base64 進行解碼,編碼

package com.soufun.com;import java.io.IOException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.util.Date;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import org.apache.commons.codec.binary.Base64;/** *@author WHD *data 2015年8月16日 */public class DesUtil {// 定義加密方式 private final static String DES = "DES"; private final static String UTF8="GBK"; static SecretKeyFactory keyFactory = null;static {try {keyFactory=SecretKeyFactory.getInstance("DES");} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}}    public static void main(String[] args) throws Exception {    long begin=new Date().getTime();    String data = "aaades加密測試";    // 注意:DES加密和解密過程中,密鑰長度都必須是8的倍數        String key = "qazwsxed";        System.err.println(encrypt(data, key));        System.err.println(decrypt(encrypt(data, key), key));        long end =new Date().getTime();        System.out.println(end-begin);    }         /**     * Description 根據索引值進行加密     * @param data      * @param key  加密鍵byte數組     * @return     * @throws Exception     */    public static String encrypt(String data, String key) throws Exception {    // 使用指定的編碼擷取要加密的內容,一般秘鑰都是字母或數字不用指定編碼,但指定也可以        byte[] bt = encrypt(data.getBytes(UTF8), key.getBytes());        // 第一個使用了sun.misc.BASE64Encoder;進行了編碼,但網上說使用org.apache.commons.codec.binary.Base64比較好所以拿來試試        String strs = Base64.encodeBase64String(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;       // 使用org.apache.commons.codec.binary.Base64解碼        byte [] buf=Base64.decodeBase64(data);        byte[] bt = decrypt(buf,key.getBytes());        return new String(bt,UTF8);    }     /**     * 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);        // 密鑰工廠用來將密鑰(類型 Key 的不透明加密金鑰)轉換為密鑰規範(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。        // 這裡改為使用單例模式        //SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);        //根據提供的密鑰規範(密鑰材料)產生 SecretKey(秘鑰) 對象。        SecretKey securekey = keyFactory.generateSecret(dks);        // Cipher對象實際完成加密操作,此類為加密和解密提供密碼功能        Cipher cipher = Cipher.getInstance(DES);        // 用密鑰和隨機源初始化此 Cipher。ENCRYPT_MODE用於將 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);        // 密鑰工廠用來將密鑰(類型 Key 的不透明加密金鑰)轉換為密鑰規範(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對秘密(對稱)密鑰進行操作。        // 這裡改為使用單例模式        //SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);        //根據提供的密鑰規範(密鑰材料)產生 SecretKey(秘鑰)對象。        SecretKey securekey = keyFactory.generateSecret(dks);        // Cipher類為加密和解密提供密碼功能        Cipher cipher = Cipher.getInstance(DES);        // DECRYPT_MODE用於將 Cipher 初始化為解密模式的常量。        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);        // 正式進行解密操作         return cipher.doFinal(data);    }}

一、二中使用到的架包:

下載: sun.misc.BASE64Decoder

下載;apache的Base64編碼、解碼器


三、未使用任何編碼,解碼架包

package com.soufun.com;import java.io.IOException;import java.security.NoSuchAlgorithmException;import java.util.Date;import java.util.HashMap;import java.util.Map;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvParameterSpec;/** *@author WHD *data 2015年8月17日 */public class DESCrypt {static SecretKeyFactory secretKeyFactory = null;//Cipher 的“演算法/模式/填充”static final String CIPHER = "DES/CBC/PKCS5Padding";static {try {// 在靜態代碼塊中擷取秘鑰工程secretKeyFactory = SecretKeyFactory.getInstance("DES");} catch (NoSuchAlgorithmException e) {e.printStackTrace();}}// 定義常量 ,編碼格式private static final String UTF8 = "GBK"; /* * 對象緩衝的容器 */static abstract class Cache {private final Map innerCache = new HashMap();protected abstract Object createValue(Object key) throws Exception;public Object get(Object key) throws Exception {Object value;synchronized (innerCache) {value = innerCache.get(key);if (value == null) {value = new CreationPlaceholder();innerCache.put(key, value);}}if (value instanceof CreationPlaceholder) {synchronized (value) {CreationPlaceholder progress = (CreationPlaceholder) value;if (progress.value == null) {progress.value = createValue(key);synchronized (innerCache) {innerCache.put(key, progress.value);}}return progress.value;}}return value;}static final class CreationPlaceholder {Object value;}}/* * hex->str & str->hex */public static byte[] stringToHex(String ss) {// 字串轉化webyte digest[] = new byte[ss.length() / 2];for (int i = 0; i < digest.length; i++) {String byteString = ss.substring(2 * i, 2 * i + 2);int byteValue = Integer.parseInt(byteString, 16);digest[i] = (byte) byteValue;}return digest;}public static String hexToString(byte b[]) {StringBuffer hexString = new StringBuffer();for (int i = 0; i < b.length; i++) {String plainText = Integer.toHexString(0xff & b[i]);if (plainText.length() < 2) {hexString.append("0");}hexString.append(plainText);}return hexString.toString();}private static byte[] _convertKeyIv(String text) throws IOException {if (text.length() == 8) {return text.getBytes(UTF8);}if (text.startsWith("0x") && text.length() == 32) {byte[] result = new byte[8];for (int i = 0; i < text.length(); i += 2) {if (text.charAt(i++) == '0' && text.charAt(i++) == 'x') {try {result[i / 4] = (byte) Integer.parseInt(text.substring(i, i + 2), 16);} catch (Exception e) {throw new IOException("TXT '" + text + "' is invalid!");}}}return result;}throw new IOException("TXT '" + text + "' is invalid!");}/* * SecretKey & IvParameterSpec的緩衝 */private static Cache SecretKeySpecs = new Cache() {protected Object createValue(Object key) throws Exception {SecretKey secretKeyObj = null;try {secretKeyObj = secretKeyFactory.generateSecret(new DESKeySpec(_convertKeyIv((String) key)));} catch (Exception e) {e.printStackTrace();}return secretKeyObj;}};private static Cache IvParamSpecs = new Cache() {protected Object createValue(Object key) throws Exception {IvParameterSpec ivObj = null;ivObj = new IvParameterSpec(_convertKeyIv((String) key));return ivObj;}};/* * 加密&解密 */public static String encrypt(String text, String authKey, String authIv) {SecretKey secretKeyObj = null;IvParameterSpec ivObj = null;try {secretKeyObj = (SecretKey) SecretKeySpecs.get(authKey);ivObj = (IvParameterSpec) IvParamSpecs.get(authIv);} catch (Exception e) {e.printStackTrace();}byte[] data = null;try {data = text.getBytes(UTF8);} catch (Exception e) {e.printStackTrace();}byte[] authToken = null;try {authToken = encrypt(data, secretKeyObj, ivObj);} catch (Exception e) {e.printStackTrace();}return hexToString(authToken);}public static byte[] encrypt(byte[] data, SecretKey secretKey,IvParameterSpec iv) throws Exception {Cipher cipher = Cipher.getInstance(CIPHER);cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);return cipher.doFinal(data);}public static String decrypt(String hexString, String authKey, String authIv)throws Exception {SecretKey secretKeyObj = null;IvParameterSpec ivObj = null;try {secretKeyObj = (SecretKey) SecretKeySpecs.get(authKey);ivObj = (IvParameterSpec) IvParamSpecs.get(authIv);} catch (Exception e) {e.printStackTrace();}String text = decrypt(hexString, secretKeyObj, ivObj);return text;}public static String decrypt(String message, SecretKey secretKey,IvParameterSpec iv) throws Exception {byte[] data = stringToHex(message);return decrypt(data, secretKey, iv);}public static String decrypt(byte[] data, SecretKey secretKey,IvParameterSpec iv) throws Exception {Cipher cipher = Cipher.getInstance(CIPHER);cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);byte[] retByte = cipher.doFinal(data);return new String(retByte);}public static void main(String[] args) throws Exception {long begin= new Date().getTime();String authKey = "w8f3k9c2";String authIv = "w8f3k9c2";String text = "aaades加密測試";// 140CB412BA03869F// 140cb412ba03869f// 對原文進行加密String encryptedText = encrypt(text, authKey, authIv);System.out.println("encryptedText:" + encryptedText);// 對密文進行還原String plainText = decrypt(encryptedText, authKey, authIv);System.out.println("plainText:" + plainText);//2a329740ce15f549be64190b183a5be2long end =new Date().getTime();System.out.println(end-begin);}}




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

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.