`
yuancihang
  • 浏览: 142202 次
  • 性别: Icon_minigender_1
  • 来自: 洛阳
社区版块
存档分类
最新评论
阅读更多

 本示例包含了DES, DESede和AES算法。 其中AES算法的密钥长度可以为128, 192 或者 256, 但是要想正常使用192和256位的密钥长度, 需要替换JRE默认的两个策略文件local_policy.jar和US_export_policy.jar。该策略文件下载地址:

https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=jce_policy-6-oth-JPR@CDS-CDS_Developer

下载完后替换%JRE_HOME%\lib\security目录下的策略文件即可。

 

1. 环境

JDK1.6

无限制的策略文件local_policy.jar

无限制的策略文件US_export_policy.jar

commons-codec-1.4.jar

 

2. 代码

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import org.apache.commons.codec.binary.Base64;

/**
 * 初等数据加密, 对称加密
 * @author yuan
 *
 */
public class SymmetricalCipher {
    /**
     * ALGORITHM 算法 <br>
     * 可替换为以下任意一种算法,同时key值的size相应改变。
     * <pre>
     * DES                  key size must be equal to 56
     * DESede(TripleDES)    key size must be equal to 112 or 168
     * AES                  key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
     * Blowfish             key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
     * RC2                  key size must be between 40 and 1024 bits
     * RC4(ARCFOUR)         key size must be between 40 and 1024 bits
     * </pre>
     */ 
    protected String cipherAlgorithm = "DES/EBC/PKCS5Padding"; // 算法/工作模式/填充方式
    protected Key key;
 
    private SymmetricalCipher(String keyAlgorithm, String cipherAlgorithm, int keySize)throws Exception{
        this.cipherAlgorithm = cipherAlgorithm;
        key = initKey(keyAlgorithm, keySize);
    }
   
    public static SymmetricalCipher getDESInstace()throws Exception{
       
        return getDESInstace("ECB", "PKCS5Padding", 56);
    }
    public static SymmetricalCipher getDESInstace(String mode, String fill, int keySize)throws Exception{
        return new SymmetricalCipher("DES", "DES/"+mode+"/"+fill, keySize);
    }
   
    public static SymmetricalCipher getDESedeInstance()throws Exception{
        return getDESedeInstance("ECB", "PKCS5Padding", 168);
    }
    public static SymmetricalCipher getDESedeInstance(String mode, String fill, int keySize)throws Exception{
        return new SymmetricalCipher("DESede", "DESede/"+mode+"/"+fill, keySize);
    }
   
    public static SymmetricalCipher getAESInstace()throws Exception{
       
        return getAESInstace("ECB", "PKCS5Padding", 256);
    }
    public static SymmetricalCipher getAESInstace(String mode, String fill, int keySize)throws Exception{
        return new SymmetricalCipher("AES", "AES/"+mode+"/"+fill, keySize);
    }
   
    public Key getKey(){
        return key;
    }
   
    public String getKeyString(){
        return Base64.encodeBase64String(key.getEncoded());
    }
 
    /**
     * 解密
     * 
     * @param data
     * @param key
     * @return
     * @throws Exception
     */ 
    public byte[] decrypt(byte[] data) throws Exception { 
        Cipher cipher = Cipher.getInstance(cipherAlgorithm); 
        cipher.init(Cipher.DECRYPT_MODE, key); 
 
        return cipher.doFinal(data); 
    } 
 
    /**
     * 加密
     * 
     * @param data
     * @param key
     * @return
     * @throws Exception
     */ 
    public byte[] encrypt(byte[] data) throws Exception { 
        Cipher cipher = Cipher.getInstance(cipherAlgorithm); 
        cipher.init(Cipher.ENCRYPT_MODE, key); 
 
        return cipher.doFinal(data); 
    } 
 
    /**
     * 生成密钥
     * 
     * @param seed
     * @return
     * @throws Exception
     */ 
    private Key initKey(String keyAlgorithm, int keySize) throws Exception { 
        KeyGenerator kg = KeyGenerator.getInstance(keyAlgorithm); 
        kg.init(keySize); 
 
        SecretKey secretKey = kg.generateKey(); 
        return secretKey;
    } 
}

 

3. 测试代码

 

SymmetricalCipher cipher = null;
       
        cipher = SymmetricalCipher.getDESInstace();
        System.out.println("DES密钥: " + cipher.getKeyString());
        byte[] desData = cipher.encrypt("DES加密".getBytes());
        System.out.println("密文: " + Base64.encodeBase64String(desData));
       
        byte[] desr = cipher.decrypt(desData);
        System.out.println("明文: " + new String(desr));
       
        cipher = SymmetricalCipher.getDESedeInstance();
        System.out.println("DESede密钥: " + cipher.getKeyString());
        byte[] desedeData = cipher.encrypt("DESede加密".getBytes());
        System.out.println("密文: " + Base64.encodeBase64String(desedeData));
        byte[] desedeText = cipher.decrypt(desedeData);
        System.out.println("明文: " + new String(desedeText));
       
        cipher = SymmetricalCipher.getAESInstace();
        System.out.println("AES密钥: " + cipher.getKeyString());
       
        byte[] data = cipher.encrypt("AES加密".getBytes());
        System.out.println(Base64.encodeBase64String(data));
       
        byte[] r = cipher.decrypt(data);
        System.out.println(new String(r));

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics