des对称加密算法java_JAVA-8 DES对称加密算法的实现 2020-07-24

本文介绍了DES对称加密算法,属于一种分组密码,遵循Feistel结构。内容包括加密原理、密钥生成、加密与解密的Java实现,并提供了测试用例及运行结果展示。
摘要由CSDN通过智能技术生成

1、Des symmetric encryption

Data Encryption Standard is a symmetric-key algorithm for the encrypting the data. It comes under block cipher algorithm which follows Feistel structure. Here is the block diagram of Data Encryption Standard.

449bdcfa8199

image.png

加密原理

编辑 DES 使用一个 56 位的密钥以及附加的 8 位奇偶校验位,产生最大 64 位的分组大小。这是一个迭代的分组密码,使用称为 Feistel 的技术,其中将加密的文本块分成两半。使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去,但最后一个循环不交换。DES 使用 16 个循环,使用异或,置换,代换,移位操作四种基本运算。

2、具体实现与解释如下:

package com.company;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class DESUtil {

/*

* 生成密钥

*/

public static byte[] initKey() throws Exception{

KeyGenerator keyGen = KeyGenerator.getInstance("DES");

keyGen.init(56);

SecretKey secretKey = keyGen.generateKey();

return secretKey.getEncoded();

}

/*

* DES 加密

*/

public static byte[] encrypt(byte[] data, byte[] key) throws Exception{

SecretKey secretKey = new SecretKeySpec(key, "DES");

Cipher cipher = Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

byte[] cipherBytes = cipher.doFinal(data);

return cipherBytes;

}

/*

* DES 解密

*/

public static byte[] decrypt(byte[] data, byte[] key) throws Exception{

SecretKey secretKey = new SecretKeySpec(key, "DES");

Cipher cipher = Cipher.getInstance("DES");

cipher.init(Cipher.DECRYPT_MODE, secretKey);

byte[] plainBytes = cipher.doFinal(data);

return plainBytes;

}

//Test

public static void main(String[] args) throws Exception {

byte[] desKey = DESUtil.initKey();

System.out.println("DES KEY : "+new String(desKey,"UTF-8"));

String DATA = "Rashidin🦅";

byte[] desResult = encrypt(DATA.getBytes() , desKey);

System.out.println(DATA + ">>>DES 加密结果>>>" + new String(desResult,"UTF-8"));

byte[] desPlain = DESUtil.decrypt(desResult, desKey);

System.out.println(DATA + ">>>DES 解密结果>>>" + new String(desPlain));

}

}

运行结果为:

449bdcfa8199

image.png

DES对称分组密码系统 import java.security.spec.*; import javax.crypto.*; import javax.crypto.spec.*; class DES01 { private String strkey; private SecretKey skey=null; private String[] algo= {"DES/ECB/PKCS5Padding","DES/ECB/NoPadding","DES"}; public DES01(String key) { strkey=key; } public void keyGenerating() throws Exception { byte[] bkey=strkey.getBytes(); KeySpec ks = new DESKeySpec(bkey); SecretKeyFactory kf = SecretKeyFactory.getInstance("DES"); skey = kf.generateSecret(ks); } public static void main(String[] a) { DES01 des = new DES01("IAMASTUDENT"); des.test02("STUDENTWANGFENGLIMING"); } public byte[] Encripting(String plaintext,int i) throws Exception { byte[] bpt=plaintext.getBytes(); Cipher cf = Cipher.getInstance(algo[i]); if(skey==null)this.keyGenerating(); cf.init(Cipher.ENCRYPT_MODE,skey); byte[] bct = cf.doFinal(bpt); return bct; } public byte[] decripting(byte[] bct,int i) throws Exception { Cipher cf = Cipher.getInstance(algo[i]); if(skey==null)this.keyGenerating(); cf.init(Cipher.DECRYPT_MODE,skey); byte[] bpt = cf.doFinal(bct); return bpt; } public void test01(String mess) { try{ byte[] ct=this.Encripting(mess,0); byte[] pt=this.Decripting(ct,0); String ptt=new String(pt); System.out.println(ptt); }catch(Exception ex) { return; } } public void test02(String mess) { try{ //Encripting print("Plaintext to be encripted:"); print(mess); byte[] ct=this.Encripting(mess,0); //Exploiting the results print("Byte array of cipher:"); for(int i=0;i<ct.length;i++) System.out.print(ct[i]+" "); print(""); for(int i=0;i<ct.length;i++) ct[i]=(byte)(ct[i]&0xFF); String ciphertxt=new String(ct); print("Cipher Text :"+ciphertxt); byte[] ctcode = ciphertxt.getBytes(); //Decripting byte[] pt=this.decripting(ctcode,0); String ptt=new String(pt); print("Plaintext after decripting:"); print(ptt); }catch(Exception ex) { return; } } public static byte[] hex2Bytes(String str) { if (str==null) { return null; } else if (str.length() < 2) { return null; } else { int len = str.length() / 2; byte[] buffer = new byte[len]; for (int i=0; i<len; i++) { buffer[i] = (byte) Integer.parseInt( str.substring(i*2,i*2+2),16); } return buffer; } } public static String bytes2Hex(byte[] data) { if (data==null) { return null; } else { int len = data.length; String str = ""; for (int i=0; i<len; i++) { if ((data[i]&0xFF)<16) str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF); else str = str + java.lang.Integer.toHexString(data[i]&0xFF); } return str.toUpperCase(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值