DES 加解密应用示例 发表于 2013-07-23 | 阅读次数: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.security.InvalidKeyException;import java.security.Key;import java.security.NoSuchAlgorithmException;import java.util.Arrays;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.CipherInputStream;import javax.crypto.IllegalBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import junit.framework.TestCase;import org.apache.commons.codec.binary.Base64;import org.apache.commons.codec.binary.Hex;public class DesCoder extends TestCase { /** 密钥 */ private static final String DES_KEY = "12345678"; /** * 测试 DES 加密解密字符串 * @author 张振伟 (2013-7-24) * @throws Exception */ public static void desString() throws Exception { String data = "这是需要加密的数据"; Key key = getKey(DES_KEY.getBytes()); byte[] cryptograph = encryptByteArr(data.getBytes("GBK"), key); System.out.println(Arrays.toString(cryptograph)); byte[] decryptData = decryptByteArr(cryptograph, key); System.out.println(new String(decryptData, "GBK")); } /** * 测试 DES 加密解密文件 * @author 张振伟 (2013-7-24) * @throws Exception */ public static void desFile() throws Exception { String dataFilePath = "D:/data.txt"; String cryptographFilePath = "D:/cryptographFilePath.txt"; String decryptData = "D:/decryptData.txt"; Key key = randomKey(); encryptFile(new File(dataFilePath), new File(cryptographFilePath), key); decryptFile(new File(cryptographFilePath), new File(decryptData), key); } /** * 生成一个随机的 DES 密钥 * @author 张振伟 (2013-7-24) * @throws Exception */ public static Key randomKey() throws Exception{ KeyGenerator generator = KeyGenerator.getInstance("DES"); SecretKey key = generator.generateKey(); byte[] arrKey = key.getEncoded(); System.out.println(Arrays.toString(arrKey)); System.out.println("密钥 Base64 转码:" + Base64.encodeBase64String(arrKey)); System.out.println("密钥 16 进制显示:" + Hex.encodeHexString(arrKey)); return key; } /** * 根据 byte 数组获得密钥对象 * @author 张振伟 (2013-7-24) * @param arrKey * @return * @throws Exception */ public static Key getKey(byte[] arrKey) throws Exception { // 实例化 DES 密钥材料 DESKeySpec spec = new DESKeySpec(arrKey); // 实例化 DES 密钥工厂 SecretKeyFactory factory = SecretKeyFactory.getInstance("DES"); return factory.generateSecret(spec); } /** * 加密字节数组形式的数据 * @author 张振伟 (2013-7-24) * @param data * @param key * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public static byte[] encryptByteArr(byte[] data, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // using DES in ECB mode Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // 用密匙初始化 Cipher 对象 cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(data); } /** * 解密字节数组形式的密文 * @author 张振伟 (2013-7-24) * @param data * @param key * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public static byte[] decryptByteArr(byte[] data, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // using DES in ECB mode Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // 用密匙初始化 Cipher 对象 cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(data); } /** * DES 加密文件 * @author 张振伟 (2013-7-24) * @param file 明文文件 * @param dest 加密后的密文文件 * @param key 密钥 * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IOException */ public static void encryptFile(File file, File dest, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); InputStream is = new FileInputStream(file); OutputStream out = new FileOutputStream(dest); CipherInputStream cis = new CipherInputStream(is, cipher); byte[] buffer = new byte[1024]; int r; while ((r = cis.read(buffer)) > 0) { out.write(buffer, 0, r); } cis.close(); is.close(); out.close(); } /** * DES 解密文件 * @author 张振伟 (2013-7-24) * @param file 需要解密的密文文件 * @param dest 解密出的明文文件 * @param key 密钥 * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IOException */ public static void decryptFile(File file, File dest, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); InputStream is = new FileInputStream(file); OutputStream out = new FileOutputStream(dest); CipherInputStream cis = new CipherInputStream(is, cipher); byte[] buffer = new byte[1024]; int r; while ((r = cis.read(buffer)) > 0) { out.write(buffer, 0, r); } cis.close(); is.close(); out.close(); }} 相关文章 数字摘要、数字信封、数字签名的基本原理 Java 安全编程:RSA 加密解密 Java 安全编程:基于口令加密(PBE) Java 安全编程:DES 加密解密 本文链接: https://zhangzw.com/posts/20130723.html 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-ND 许可协议。转载请注明出处!