当前位置:网站首页>信息安全实验一:DES加密算法的实现
信息安全实验一:DES加密算法的实现
2022-07-07 06:38:00 【一顿吃不饱】
一、实验目的及要求
1.熟悉加密、解密算法;懂得加密在通信中的重要作用;
2.对输入的十六进制数加密(把输入的字符转化成整数),比较输入和输出,当把输入的数改变一个比特时,比较输出的变化,说明原因。
3.实现对一个文件进行加解密,提交程序代码和执行结果。
二、实验内容
本实验通过用DES算法对实际的数据进行加密和解密来深刻了解DES的运行原理。根据所提供的程序分析DES算法的实现过程。在分析密钥生成函数、加密函数(8字节)、解密函数、测试函数和密钥长度检验函数的基础上,用C/VC++或Java语言编写程序实现对文本文件进行加解密。
三、实验环境
运行windows或Linux操作系统的PC机,具有VC(windows)、gcc(Linux)等C语言编译环境或Java环境。
四、实验步骤及结果分析
- 运行结果
1.1、原文件
加密后的文件
解密后的文件
- 代码
public class TestDES {
Key key;
public TestDES(String str) {
getKey(str);// 生成密匙
}
// 根据参数生成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"); // 加密
td.decrypt("D:/Desktop/test1.txt", "D:/Desktop/test2.txt"); // 解密
}
}
边栏推荐
- Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points
- Systick tick timer
- stm32和电机开发(从单机版到网络化)
- Implementation of corner badge of Youmeng message push
- Skill review of test engineer before interview
- Why is access to the external network prohibited for internal services of the company?
- Three updates to build applications for different types of devices | 2022 i/o key review
- Huawei HCIP - datacom - Core 03 jours
- 华为HCIP-DATACOM-Core_03day
- C language pointer (Part 2)
猜你喜欢
Serial port experiment - simple data sending and receiving
2022-06-30 unity core 8 - model import
Locust performance test 2 (interface request)
Jemter operation
NVIC interrupt priority management
PMP Exam details after the release of the new exam outline
Mysql数据库-锁-学习笔记
Postman interface debugging method
Pycharm importing third-party libraries
Implementation of corner badge of Youmeng message push
随机推荐
Entity of cesium data visualization (Part 1)
PMP experience learning and sharing process
Serial port experiment - simple data sending and receiving
(3/8)枚举的不当用法 之 方法参数(二)
Simulation volume leetcode [general] 1706 Where does the ball meet
On December 8th, 2020, the memory of marketing MRC application suddenly increased, resulting in system oom
[istio introduction, architecture, components]
OpenGL frame buffer
【istio简介、架构、组件】
Screen automatically generates database documents
Mysql database transaction learning notes
Mysql数据库-锁-学习笔记
Skill review of test engineer before interview
LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
STM32 serial port register library function configuration method
C language pointer (Part 2)
Postman setting environment variables
Pytest+request+allure+excel interface automatic construction from 0 to 1 [five nails / flying Book notice]
浏览器中如何让视频倍速播放
Jemter operation