DES加密解密

自己留着,解释如下

package com.jicheng.android.project.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
*
* @Package: com.jicheng.android.project.utils
* @ClassName: DESEncrypter
* @Description: TODO(这里用一句话描述这个类的作用)
* @author
* @date 2016年4月8日 上午11:23:26
*
*/
public class DESEncrypter
{
private final static String DES = “DES”;

private final static int DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE = 1024;

/**
 * 秘钥
 */
private static byte[] iv = "1234567812345678".getBytes();

/**
 * Description 根据键值进行加密
 * 
 * @param data
 * @param key 加密键byte数组
 * @return
 * @throws Exception
 */
public static void encrypt(byte[] key, InputStream in, OutputStream out)
    throws Exception
{
    // 生成一个可信任的随机数源
    SecureRandom sr = new SecureRandom();
    // 从原始密钥数据创建DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(key);
    // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher对象实际完成加密操作
    Cipher cipher = Cipher.getInstance(DES);
    // 用密钥初始化Cipher对象
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    out = new CipherOutputStream(out, cipher);
    int count = 0;
    byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
    while ((count = in.read(buffer)) >= 0)
    {
        out.write(buffer, 0, count);
    }
    out.close();

}

/**
 * Description 根据键值进行解密
 * 
 * @param data
 * @param key 加密键byte数组
 * @return
 * @throws Exception
 */
public static void decrypt(byte[] key, InputStream in, OutputStream out)
    throws Exception
{
    // 生成一个可信任的随机数源
    SecureRandom sr = new SecureRandom();

    // 从原始密钥数据创建DESKeySpec对象
    DESKeySpec dks = new DESKeySpec(key);

    // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);

    // Cipher对象实际完成解密操作
    Cipher cipher = Cipher.getInstance(DES);

    // 用密钥初始化Cipher对象
    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

    out = new CipherOutputStream(out, cipher);
    int count = 0;
    byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
    while ((count = in.read(buffer)) >= 0)
    {
        out.write(buffer, 0, count);
    }

    out.close();

}

public static void main(String[] args)
{
    File inFile = new File("TheTest.mp4");
    File outFile = new File("encrypt_file.mp4");
    File outFile_dec = new File("decrypt_file.mp4");

    try
    {
        // 加密
        // encrypt(iv, new FileInputStream(inFile), new FileOutputStream(outFile));
        // 解密
        decrypt(iv, new FileInputStream(outFile), new FileOutputStream(outFile_dec));
    }
    catch (FileNotFoundException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();

        System.out.println("i=" + "=========printStackTrace===============t");
    }

}

}

### DES加密解密方法概述 数据加密标准(Data Encryption Standard, DES)是一种对称加密算法,它通过一个固定的56位密钥来加密解密数据。DES将明文分为64位的数据块,并对其进行一系列复杂的置换和替换操作以生成密文[^1]。 以下是关于DES加密解密的具体实现方式: #### 密钥长度与分组大小 DES使用的是固定长度的密钥——即56位有效密钥加上8位奇偶校验位形成64位输入密钥。每次处理的数据块也是64位长。这种设计使得DES能够快速执行加密解密过程[^2]。 #### 加密流程描述 在加密过程中,原始消息被分割成多个64比特的小段,每一段都经过初始排列IP (Initial Permutation),随后进入由16轮迭代组成的Feistel结构,在最后一轮结束后再做一次逆向初始排列(IP^-1)[^3]。 #### 解密流程描述 由于DES采用对称密码体制,因此其解密过程实际上就是按照相反顺序重复相同的运算步骤,只是使用的子密钥次序颠倒过来而已[^4]。 下面给出Python语言中的简单示例代码展示如何利用pycryptodome库完成基本的DES解密功能: ```python from Crypto.Cipher import DES from Crypto.Util.Padding import pad, unpad import base64 def des_encrypt(plaintext, key): cipher = DES.new(key.encode(), DES.MODE_ECB) padded_text = pad(plaintext.encode(), DES.block_size) encrypted_data = cipher.encrypt(padded_text) return base64.b64encode(encrypted_data).decode() def des_decrypt(ciphertext, key): cipher = DES.new(key.encode(), DES.MODE_ECB) decoded_ciphertext = base64.b64decode(ciphertext) decrypted_padded_data = cipher.decrypt(decoded_ciphertext) return unpad(decrypted_padded_data, DES.block_size).decode() # Example Usage key = '12345678' # Must be exactly 8 bytes long. message = "HelloWorld" ciphered_message = des_encrypt(message, key) print(f"Cipher Text: {ciphered_message}") original_message = des_decrypt(ciphered_message, key) print(f"Original Message: {original_message}") ``` 上述程序展示了基于ECB模式下的基础版DES加密/解密逻辑[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值