当前位置:网站首页>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
}
}
边栏推荐
- How to pass the PMP Exam in a short time?
- Final keyword
- JVM 垃圾回收 详细学习笔记(二)
- Mysql:select ... for update
- Locust performance test 5 (analysis)
- What is the value of getting a PMP certificate?
- Zen - batch import test cases
- Where is the answer? action config/Interceptor/class/servlet
- PMP experience learning and sharing process
- Huawei hcip datacom core_ 03day
猜你喜欢
Where is the answer? action config/Interceptor/class/servlet
STM32 and motor development (from stand-alone version to Networking)
Jenkins automated email
端口复用和重映像
C language pointer (Part 2)
[cloud native] Devops (I): introduction to Devops and use of code tool
四、机器学习基础
Several stages of PMP preparation study
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
C language pointer (exercises)
随机推荐
Reflections on the way of enterprise IT architecture transformation (Alibaba's China Taiwan strategic thought and architecture practice)
Record of structured interview
Locust performance test 2 (interface request)
PMP certificate preparation experience sharing
What is the value of getting a PMP certificate?
寄存器地址名映射
C语言指针(上篇)
Difference between interface iterator and iteratable
嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
Network request process
C language pointer (exercises)
Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
Huawei hcip datacom core_ 03day
【云原生】DevOps(一):DevOps介绍及Code工具使用
Schema-validation: wrong column type encountered in column XXX in table XXX
Yapi test plug-in -- cross request
Leetcode question brushing record (array) combination sum, combination sum II
2020 year end summary
Connecting mobile phone with ADB