加密算法之对称加密--DES

一、DES简介

现在相当多的分组密码都采用了Feistel网络密码结构,而DES则是Feistel网络的典型代表。Feistel网络包括平衡的Feistel网络和非平衡Feistel网络,DES加密算法则采用了较为简单的平衡网络。

二、DES加密算法过程

1. DES加密消息流程图

这里写图片描述

2. 加密具体过程

x = x 1 x 2 x 3 . . . . . . x 64 x = x_1 x_2 x_3 ...... x_{64} x=x1x2x3......x64 是待加密的64比特明文,其中 x i x_i xi 是 0或1的二进制比特 ( 1 < = i < = 64 ) (1<=i<=64) (1<=i<=64)。DES首先利用初始置换 ( I P ) (IP) (IP) x x x进行换位处理; I P IP IP置换如下表

12345678910111213141516
585012342618102605244362820124
625446383022146645648403224168
57494133251791595143352719113
615345372921135635547393123157

然后再进行与密钥相关的16次迭代(如流程图 K 1 , . . . , K 16 K_1,...,K_{16} K1,...,K16),最后经过逆初始置换 ( I P − 1 ) (IP^{-1}) (IP1) , I P − 1 IP^{-1} IP1 置换如下表

12345678910111213141516
408481656246432397471555236331
386461454226230375451353216129
364441252206028353431151195927
34242105018582633141949175725

经过上述处理得到密文 y = y 1 y 2 y 3 . . . . . . y 64 y =y_1 y_2 y_3 ...... y_{64} y=y1y2y3......y64

PS :置换方式(如初始 I P IP IP置换),参照 I P IP IP置换表,第一次将 x = x 1 x 2 x 3 . . . . . . x 64 x = x_1 x_2 x_3 ...... x_{64} x=x1x2x3......x64 置换后得到 x ′ = x 58 x 50 x 42 . . . . . . x 7 x' = x_{58} x_{50} x_{42} ...... x_7 x=x58x50x42......x7,然后再分为L和R各32位的左右两块

三、JAVA实现

package com.encrypted.cipher;

import org.apache.commons.codec.binary.Hex;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class En_DES {
	//JDK 提供的DES加密算法
	public void jdkDES(String msg){
		try{
			//生成Key
			KeyGenerator keygenerate = KeyGenerator.getInstance("DES");
			keygenerate.init(56);
			SecretKey secretkey = keygenerate.generateKey();
			byte [] bytesecret= secretkey.getEncoded();
			
			//加密
			SecretKey secretKey = new SecretKeySpec(bytesecret,"DES");
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			byte[] result = cipher.doFinal(msg.getBytes());
			
			System.out.println("JDK_DES加密结果:" +Hex.encodeHexString(result));
			
			//解密
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			result = cipher.doFinal(result);
			
			System.out.println("JDK_DES解密结果:" + new String(result));
			System.out.println();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
}

package com.encrypted.cipher;

public class Encrypt_Demo {

	public static String message = "这是一个明文";
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		En_DES des = new En_DES();
		des.jdkDES(message);
	}

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值