当前位置:网站首页>Information Security Experiment 1: implementation of DES encryption algorithm
Information Security Experiment 1: implementation of DES encryption algorithm
2022-07-07 09:22:00 【Not enough to eat】
One 、 Purpose and requirements of the experiment
1. Familiar with encryption 、 Decryption algorithm ; Understand the important role of encryption in communication ;
2. Encrypt the entered hexadecimal number ( Convert the input characters into integers ), Compare input and output , When changing the input number by one bit , Compare changes in output , Explain why .
3. Realize the encryption and decryption of a file , Submit program code and execution results .
Two 、 Experimental content
This experiment uses DES The algorithm encrypts and decrypts the actual data to gain a deep understanding DES Operating principle . Analyze according to the program provided DES The realization process of the algorithm . In analyzing the key generation function 、 Encryption function (8 byte )、 Decryption function 、 Test function and key length test function , use C/VC++ or Java Language programming to encrypt and decrypt text files .
3、 ... and 、 Experimental environment
function windows or Linux Operating system PC machine , have VC(windows)、gcc(Linux) etc. C Language compilation environment or Java Environmental Science .
Four 、 Experimental steps and result analysis
- Running results
1.1、 The original document
Encrypted file
Decrypted file
- Code
public class TestDES {
Key key;
public TestDES(String str) {
getKey(str);// Generate key
}
// Generate... Based on parameters KEY
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
}
catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
}
}
public void encrypt(String file, String destFile) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
// cipher.init(Cipher.ENCRYPT_MODE, getKey());
cipher.init(Cipher.ENCRYPT_MODE, this.key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(destFile);
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();
}
public void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, this.key);
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
public static void main(String[] args) throws Exception {
TestDES td = new TestDES("24234");
td.encrypt("D:/Desktop/test.txt", "D:/Desktop/test1.txt"); // encryption
td.decrypt("D:/Desktop/test1.txt", "D:/Desktop/test2.txt"); // Decrypt
}
}
边栏推荐
- STM32 clock system
- 【云原生】DevOps(一):DevOps介绍及Code工具使用
- Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
- JWT certification used in DRF
- C语言指针(中篇)
- Systick滴答定时器
- Port multiplexing and re imaging
- [cloud native] Devops (I): introduction to Devops and use of code tool
- Systick tick timer
- Using JWT to realize login function
猜你喜欢
[istio introduction, architecture, components]
C语言指针(下篇)
Mysql database transaction learning notes
C语言指针(特别篇)
C语言指针(中篇)
Postman data driven
Reflections on the way of enterprise IT architecture transformation (Alibaba's China Taiwan strategic thought and architecture practice)
Confitest of fixture py
H3C vxlan configuration
Postman interface test (II. Set global variables \ sets)
随机推荐
JMeter JDBC batch references data as input parameters (the simplest method for the whole website)
C language pointer (Part 2)
【Istio Network CRD VirtualService、Envoyfilter】
Zen - batch import test cases
shake数据库中怎么使用Mongo-shake实现MongoDB的双向同步啊?
Regularly modify the system time of the computer
[SVN] what is SVN? How do you use it?
Data association between two interfaces of postman
Jenkins+ant+jmeter use
5A summary: seven stages of PMP learning
JWT certification used in DRF
Pytest installation (command line installation)
C language pointer (special article)
Confitest of fixture py
NVIC interrupt priority management
Self awakening from a 30-year-old female programmer
How can I apply for a PMP certificate?
Interview question: general layout and wiring principles of high-speed PCB
What are the suggestions for PMP candidates?
SAP MM STO单据的外向交货单创建后新加ITEM?