Java工具类——DES加密和解密工具 II

import java.security.Key;  
  
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(key);  
        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, 此初始方法不必须调用  
        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);  
        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);  
        return cipher.doFinal(data);  
    }  
}  


参考另一篇:Java工具类——DES加密和解密工具 I    http://blog.csdn.net/softwave/article/details/12838045

转自【http://www.open-open.com/lib/view/open1394441809958.html】

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 DES 算法进行纯数字加密解密Java 工具类代码示例: ```java import javax.crypto.Cipher; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; import javax.crypto.SecretKeyFactory; import javax.crypto.SecretKey; import java.security.spec.KeySpec; import java.util.Base64; public class NumericEncryption { private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding"; private static final String KEY_ALGORITHM = "DES"; private static final String CHARSET = "UTF-8"; private static final String IV_PARAMETER = "12345678"; // 8位长度的向量参数 public static void main(String[] args) { String input = "12345678"; // 待加密的数字 String key = "abcdefgh"; // 密钥,必须是8位长度的字符串 String encrypted = encrypt(input, key); // 加密后的结果 System.out.println(encrypted); String decrypted = decrypt(encrypted, key); // 解密后的结果 System.out.println(decrypted); } public static String encrypt(String input, String key) { try { IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); KeySpec keySpec = new DESKeySpec(key.getBytes(CHARSET)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM); SecretKey secretKey = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); byte[] encryptedBytes = cipher.doFinal(input.getBytes(CHARSET)); return Base64.getEncoder().encodeToString(encryptedBytes); } catch (Exception e) { e.printStackTrace(); } return null; } public static String decrypt(String input, String key) { try { IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); KeySpec keySpec = new DESKeySpec(key.getBytes(CHARSET)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM); SecretKey secretKey = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(input)); return new String(decryptedBytes, CHARSET); } catch (Exception e) { e.printStackTrace(); } return null; } } ``` 这段代码使用了 DES 算法进行加密解密,需要注意以下几点: 1. 密钥必须是8位长度的字符串,因此在使用时需要对密钥进行处理。 2. 使用 CBC 模式需要指定一个8位长度的向量参数,可以自定义值。 3. 加密后的结果需要进行 Base64 编码,解密前需要进行 Base64 解码。 4. 在使用 Cipher 类时需要指定算法、加密模式和填充方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值