中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java加密算法DES

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
密鑰加密也稱為對稱加密,速度快,但加密和解密的鑰匙必須相同,只有通信雙方才能知道鑰匙。
import java.security.Key;  
import java.security.SecureRandom;  
  
import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  
import javax.crypto.SecretKey;  
import javax.crypto.SecretKeyFactory;  
import javax.crypto.spec.DESKeySpec;  
import javax.crypto.spec.IvParameterSpec;  
  
/** 
 * DES 算法       1972美國IBM研制,對稱加密算法 
 * @author stone 
 * @date 2014-03-10 04:47:05 
 */  
public class DES {    
    // 算法名稱  
    public static final String KEY_ALGORITHM = "DES";  
    // 算法名稱/加密模式/填充方式  
    public static final String CIPHER_ALGORITHM_ECB = "DES/ECB/PKCS5Padding";  
    public static final String CIPHER_ALGORITHM_CBC = "DES/CBC/PKCS5Padding";  
      
    public static void main(String[] args) throws Exception {  
        /* 
         * 使用 ECB mode 
         * 密鑰生成器 生成密鑰 
         * ECB mode cannot use IV 
         */  
        byte[] key = generateKey();   
        byte[] encrypt = encrypt("胃炎F#*(x)".getBytes(), key);  
        System.out.println(new String(decrypt(encrypt, key)));  
          
          
        /* 
         * 使用CBC mode 
         * 使用密鑰工廠生成密鑰,加密 解密 
         * iv: DES in CBC mode and RSA ciphers with OAEP encoding operation. 
         */  
        DESKeySpec dks = new DESKeySpec(generateKey());  
        SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM);  
        SecretKey secretKey = factory.generateSecret(dks);  
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);  
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(getIV()));  
        byte[] enc = cipher.doFinal("胃炎A%F#*(x)".getBytes()); //加密  
          
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV()));  
        byte[] dec = cipher.doFinal(enc); // 解密  
        System.out.println(new String(dec));  
    }  
      
    static byte[] getIV() {  
        String iv = "asdfivh7"; //IV length: must be 8 bytes long  
        return iv.getBytes();  
    }  
  
    /** 
     * 生成密鑰 
     *  
     * @return 
     * @throws Exception 
     */  
    private static byte[] generateKey() throws Exception {  
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);  
        keyGenerator.init(56); //des 必須是56, 此初始方法不必須調(diào)用  
        SecretKey secretKey = keyGenerator.generateKey();  
        return secretKey.getEncoded();  
    }  
  
    /** 
     * 還原密鑰 
     *  
     * @param key 
     * @return 
     * @throws Exception  
     */  
    private static Key toKey(byte[] key) throws Exception {  
        DESKeySpec des = new DESKeySpec(key);  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);  
        SecretKey secretKey = keyFactory.generateSecret(des);  
        return secretKey;  
    }  
      
    /** 
     * 加密  
     * @param data 原文 
     * @param key 
     * @return 密文 
     * @throws Exception 
     */  
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {  
        Key k = toKey(key);  
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);  
        cipher.init(Cipher.ENCRYPT_MODE, k, new SecureRandom());  
        return cipher.doFinal(data);  
    }  
    /** 
     * 解密 
     * @param data 密文 
     * @param key 
     * @return 明文、原文 
     * @throws Exception 
     */  
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {  
        Key k = toKey(key);  
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);  
        cipher.init(Cipher.DECRYPT_MODE, k, new SecureRandom());  
        return cipher.doFinal(data);  
    }  
}  

標(biāo)簽: 通信

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C#通過List類實(shí)現(xiàn)動態(tài)變長數(shù)組

下一篇:一個(gè)好用的驗(yàn)證碼PHP工具類