前言
為了幫助同學(xué)們完成痛苦的實驗課程設(shè)計,本作者將其作出的實驗結(jié)果及代碼貼至CSDN中,供同學(xué)們學(xué)習(xí)參考。如有不足或描述不完善之處,敬請各位指出,歡迎各位的斧正!
一、實驗?zāi)康?/h2>
1、掌握DES算法的工作原理。
2、熟悉分組加密算法的4種工作模式(OFB模式可不做)。
3、了解DES的雪崩效應(yīng)。
二、實驗環(huán)境
Microsoft Visual Studio 2019
三、實驗內(nèi)容及步驟
(1)編程實現(xiàn)DES算法。
(2)改變1位明文觀察輸出DES算法的16輪輸出,幾輪后密文變化達(dá)到32位以上。
(3)改變1位密鑰觀察輸出DES算法的16輪輸出,幾輪后密文變化達(dá)到32位以上。
(4)在電碼本模式和分組鏈接模式中,在最少64個分組的明文中,觀察當(dāng)一個密文分組錯誤時,還原的明文有幾個分組錯誤。
(5)在密碼反饋模式和輸出反饋模式中,在最少64個分組的明文中,觀察當(dāng)一個密文分組錯誤時,還原的明文有幾個分組錯誤。**
四、實驗要求
(1)編寫一個DES算法,*輸出其每一輪的加密結(jié)果并顯示在屏幕上。
(2)編程實現(xiàn)對文件的加密,加密模式:電碼本、分組鏈接模式;
(3)*額外要求:編程實現(xiàn)密碼反饋模式和輸出反饋模式。
說明:
(1)DES算法可以自編,也可以網(wǎng)上下載現(xiàn)成算法。
(2)四種工作模式的程序可以自編,也可以利用OpenSSL、VS的framework、cryptopp加密包編程。
(3)基本DES算法要想輸出每輪的加密結(jié)果,請參照C程序;使用C#編程可以實現(xiàn)電碼本、分組鏈接、密碼反饋模式。
五、實驗程序清單
一、編寫一個DES算法,輸出其每一輪的加密結(jié)果并顯示在屏幕上。
修改一位明文,第四輪即產(chǎn)生明顯不同的加密結(jié)果
修改一位密文,第四輪即產(chǎn)生明顯不同的加密結(jié)果
二、對文件的加密
明文文件:
密文文件:
解密文件:
通過修改文件后綴名得到原圖
六、實驗結(jié)果
(1)程序設(shè)計的思想,及程序關(guān)鍵原代碼。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace SecTest
{
public partial class DesForm : Form
{
public string str1;
public string MFileNamestr1;
public string CFileNamestr;
public string MFileNamestr2;
public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString = strB.ToString();
}
return hexString;
}
public DesForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyDES des1 = new MyDES();
if (radioButton1.Checked == true)
{
MyDES.f = 1;
}
if (radioButton2.Checked == true)
{
MyDES.f = 2;
}
if (radioButton3.Checked == true)
{
MyDES.f = 3;
}
if (radioButton5.Checked == true)
{
MyDES.ff = 1;
textBox2.Text = MyDES.Encrypt(textBox1.Text, textBox4.Text, textBox5.Text);
textBox6.Text = ToHexString(Encoding.UTF8.GetBytes(textBox2.Text));
}
if (radioButton4.Checked == true)
{
MyDES.ff = 2;
MyDES.FileEncryptor(textBox7.Text, textBox8.Text, textBox4.Text, textBox5.Text);
}
}
private void DesForm_Load(object sender, EventArgs e)
{
textBox1.Text = str1;
}
private void button2_Click(object sender, EventArgs e)
{
MyDES des1 = new MyDES();
if (radioButton1.Checked == true)
{
MyDES.f = 1;
}
if (radioButton2.Checked == true)
{
MyDES.f = 2;
}
if (radioButton3.Checked == true)
{
MyDES.f = 3;
}
if (radioButton5.Checked == true)
{
MyDES.ff = 1;
textBox3.Text = MyDES.Decrypt(textBox2.Text, textBox4.Text, textBox5.Text);
}
if (radioButton4.Checked == true)
{
MyDES.ff = 2;
MyDES.FileDecrypt(textBox8.Text, textBox9.Text, textBox4.Text, textBox5.Text);
}
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// openFileDialog.InitialDirectory = "E:\\";
// openFileDialog.Filter = "Md1 File(*.md1)|*.md1";
// openFileDialog.RestoreDirectory = true;
// openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox7.Text = openFileDialog.FileName;
MFileNamestr1 = textBox7.Text;
}
}
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox8.Text = openFileDialog.FileName;
CFileNamestr = textBox8.Text;
}
}
private void button5_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox9.Text = openFileDialog.FileName;
MFileNamestr2 = textBox9.Text;
}
}
}
public sealed class MyDES {
public static uint f;
public static uint ff;
public static bool FileDecrypt(string sInputFilename, string sOutputFilename, string key, string IV)
{
byte[] desKey = ASCIIEncoding.ASCII.GetBytes(key);
byte[] desIV = ASCIIEncoding.ASCII.GetBytes(IV);
FileStream fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
long rdlen = 0;
long totlen = fin.Length;
// byte[] bin = new byte[(int)totlen];
byte[] bin = new byte[1000];
int len;
DES provider1 = new DESCryptoServiceProvider();
if (f == 1)
provider1.Mode = CipherMode.ECB;
if (f == 2)
provider1.Mode = CipherMode.CBC;
if (f == 3)
provider1.Mode = CipherMode.CFB;
CryptoStream encStream = new CryptoStream(fout, provider1.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 1000);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
return true;
}
public static bool FileEncryptor(string sInputFilename, string sOutputFilename, string key, string IV)
{
byte[] desKey = ASCIIEncoding.ASCII.GetBytes(key);
byte[] desIV = ASCIIEncoding.ASCII.GetBytes(IV);
FileStream fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
long rdlen = 0;
long totlen = fin.Length;
byte[] bin = new byte[1000];
int len;
DES provider1 = new DESCryptoServiceProvider();
if (f == 1)
provider1.Mode = CipherMode.ECB;
if (f == 2)
provider1.Mode = CipherMode.CBC;
if (f == 3)
provider1.Mode = CipherMode.CFB;
CryptoStream encStream = new CryptoStream(fout, provider1.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 1000);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.Close();
fout.Close();
fin.Close();
return true;
}
public static string Decrypt(string val, string key, string IV)
{
try {
byte[] buffer1 = ASCIIEncoding.ASCII.GetBytes(key);
byte[] buffer2 = ASCIIEncoding.ASCII.GetBytes(IV);
DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
if(f==1)
provider1.Mode = CipherMode.ECB;
if (f == 2)
provider1.Mode = CipherMode.CBC ;
if (f == 3)
provider1.Mode = CipherMode.CFB;
provider1.Key = buffer1;
provider1.IV = buffer2;
provider1.Padding = PaddingMode.PKCS7;
ICryptoTransform transform1 = provider1.CreateDecryptor(provider1.Key, provider1.IV);
// byte[] buffer3 = Convert.FromBase64String(val);
byte[] buffer3 = Convert.FromBase64String(val);
MemoryStream stream1 = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
stream2.Write(buffer3, 0, buffer3.Length);
stream2.FlushFinalBlock();
stream2.Close();
return Encoding.Default.GetString(stream1.ToArray());
}
catch// (System.Exception ex)
{
return "";
}
}
public static string Encrypt(string val, string key, string IV) {
try {
byte[] buffer1 = ASCIIEncoding.ASCII.GetBytes(key);
byte[] buffer2 = ASCIIEncoding.ASCII.GetBytes(IV);
DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
if (f == 1)
provider1.Mode = CipherMode.ECB;
if (f == 2)
provider1.Mode = CipherMode.CBC;
if (f == 3)
provider1.Mode = CipherMode.CFB;
KeySizes[] a = provider1.LegalBlockSizes;
provider1.Key = buffer1;
provider1.IV = buffer2;
provider1.Padding = PaddingMode.PKCS7;
ICryptoTransform transform1 = provider1.CreateEncryptor(provider1.Key, provider1.IV);
byte[] buffer3 = ASCIIEncoding.ASCII.GetBytes(val);
MemoryStream stream1 = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
stream2.Write(buffer3, 0, buffer3.Length);
stream2.FlushFinalBlock();
stream2.Close();
return Convert.ToBase64String(stream1.ToArray());
// return Convert.ToString (stream1.ToArray());
}
catch// (Exception ex)
{
return "";
}
}
}
}
//
// main.cpp
// DES
#include <stdlib.h>
#include <stdio.h>
#include "tables.h"
#include <iostream>
using namespace std;
void BitsCopy(bool *DatOut,bool *DatIn,int Len); // 數(shù)組復(fù)制
void ByteToBit(bool *DatOut,char *DatIn,int Num); // 字節(jié)到位
void BitToByte(char *DatOut,bool *DatIn,int Num); // 位到字節(jié)
void BitToHex(char *DatOut,bool *DatIn,int Num); // 二進(jìn)制到十六進(jìn)制 64位 to 4*16字符
void HexToBit(bool *DatOut,char *DatIn,int Num); // 十六進(jìn)制到二進(jìn)制
void TablePermute(bool *DatOut,bool *DatIn,const char *Table,int Num); // 位表置換函數(shù)
void LoopMove(bool *DatIn,int Len,int Num); // 循環(huán)左移 Len長度 Num移動位數(shù)
void Xor(bool *DatA,bool *DatB,int Num); // 異或函數(shù)
void S_Change(bool DatOut[32],bool DatIn[48]); // S盒變換
void F_Change(bool DatIn[32],bool DatKi[48]); // F函數(shù)
void SetKey(char KeyIn[24]); // 設(shè)置密鑰
void PlayDes(char MesOut[8],char MesIn[8]); // 執(zhí)行DES加密
void KickDes(char MesOut[8],char MesIn[8]); // 執(zhí)行DES解密
int main(){
char MesHex[16]; // 16個字符數(shù)組用于存放 64位16進(jìn)制的密文
char MyKey[25]; // 初始密鑰 8字節(jié)*8
char MyMessage[8]; // 初始明文
cout<<"Input Message:64bit:"<<endl;
cin>>MyMessage; // 明文
cout<<"Input Secret Key:192bit"<<endl;
cin>>MyKey; // 密鑰
int len = 0;
while(MyKey[len]!='\0')
len++;
while(len!=24){
cout<<"Input Correct Secret Key!"<<endl;
cin>>MyKey;
len = 0;
while(MyKey[len]!='\0')
len++;
}
SetKey(MyKey); // 設(shè)置密鑰 得到子密鑰Ki
PlayDes(MesHex,MyMessage); // 執(zhí)行DES加密
cout<<"Encrypting:"<<endl; // 信息已加密
for(int i = 0; i < 16; i++)
cout<<MesHex[i];
cout<<endl;
KickDes(MyMessage,MesHex); // 解密輸出到MyMessage
cout<<"Deciphering:"<<endl;
for(int i = 0; i < 8; i++)
cout<<MyMessage[i];
cout<<endl;
}
//位移動
void BitsCopy(bool *DatOut,bool *DatIn,int Len){
for(int i = 0; i < Len; i++)
DatOut[i] = DatIn[i];
}
//字節(jié)轉(zhuǎn)換成位
void ByteToBit(bool *DatOut,char *DatIn,int Num){
for(int i = 0; i < Num; i++)
DatOut[i] = (DatIn[i / 8] >> (i % 8)) & 0x01;
}
//位轉(zhuǎn)換成字節(jié)
void BitToByte(char *DatOut,bool *DatIn,int Num){
for(int i = 0; i < (Num / 8); i++)
DatOut[i] = 0;
for(int i = 0; i < Num; i++)
DatOut[i / 8] |= DatIn[i] << (i % 8);
}
//二進(jìn)制密文轉(zhuǎn)換為十六進(jìn)制
void BitToHex(char *DatOut,bool *DatIn,int Num){
for(int i = 0; i < Num / 4; i++)
DatOut[i] = 0;
for(int i = 0; i < Num / 4; i++){
DatOut[i] = DatIn[i * 4] + (DatIn[i * 4 + 1] << 1) + (DatIn[i * 4 + 2] << 2) + (DatIn[i * 4 + 3] << 3);
if((DatOut[i] % 16) > 9)
DatOut[i] = DatOut[i] % 16 + '7'; // 余數(shù)大于9時處理 10-15 to A-F
else
DatOut[i] = DatOut[i] % 16 + '0';
}
}
//十六進(jìn)制字符轉(zhuǎn)二進(jìn)制
void HexToBit(bool *DatOut,char *DatIn,int Num){
for(int i = 0; i < Num; i++){
if((DatIn[i / 4]) > '9') // 大于9
DatOut[i] = ((DatIn[i / 4] - '7') >> (i % 4)) & 0x01;
else
DatOut[i] = ((DatIn[i / 4] - '0') >> (i % 4)) & 0x01;
}
}
//表置換函數(shù)
void TablePermute(bool *DatOut,bool *DatIn,const char *Table,int Num){
static bool Temp[256]={0};
for(int i = 0; i < Num; i++) // Num為置換的長度
Temp[i] = DatIn[Table[i] - 1]; // 原來的數(shù)據(jù)按對應(yīng)的表上的位置排列
BitsCopy(DatOut,Temp,Num); // 把緩存Temp的值輸出
}
// 子密鑰的移位
void LoopMove(bool *DatIn,int Len,int Num){
static bool Temp[256]={0};
BitsCopy(Temp,DatIn,Num); // 將數(shù)據(jù)最左邊的Num位(被移出去的)存入Temp
BitsCopy(DatIn,DatIn+Num,Len-Num); // 將數(shù)據(jù)左邊開始的第Num移入原來的空間
BitsCopy(DatIn+Len-Num,Temp,Num); // 將緩存中移出去的數(shù)據(jù)加到最右邊
}
// 按位異或
void Xor(bool *DatA,bool *DatB,int Num){
for(int i = 0; i < Num; i++)
DatA[i] = DatA[i] ^ DatB[i];
}
// S盒
void S_Change(bool DatOut[32],bool DatIn[48]){
for(int i = 0,Y = 0,X = 0; i < 8; i++,DatIn += 6,DatOut += 4){ // 每執(zhí)行一次,輸入數(shù)據(jù)偏移6位,輸出數(shù)據(jù)偏移4位
Y = (DatIn[0] << 1) + DatIn[5]; //af代表第幾行
X = (DatIn[1] << 3) + (DatIn[2] << 2) + (DatIn[3] << 1) + DatIn[4]; // bcde代表第幾列
ByteToBit(DatOut,&S_Box[i][Y][X],4); // 把找到的點數(shù)據(jù)換為二進(jìn)制
}
}
// F函數(shù)
void F_Change(bool DatIn[32],bool DatKi[48]){
bool MiR[48] = {0}; // 輸入32位通過E選位變?yōu)?8位
TablePermute(MiR,DatIn,E_Table,48);
Xor(MiR,DatKi,48); // 和子密鑰異或
S_Change(DatIn,MiR); // S盒變換
TablePermute(DatIn,DatIn,P_Table,32); // P置換后輸出
}
void SetKey(char KeyIn[8]){ // 設(shè)置密鑰 獲取子密鑰Ki
static bool KeyBit[192]={0}; // 密鑰二進(jìn)制存儲空間
static bool *KiL =&KeyBit[0],*KiR =&KeyBit[64],*KiB =&KeyBit[128];
ByteToBit(KeyBit,KeyIn,192); // 把密鑰轉(zhuǎn)為二進(jìn)制存入KeyBit
for(int i = 0; i < 16; i++){
LoopMove(KiL,64,4);
LoopMove(KiR,64,4);
LoopMove(KiB,64,4);
bool temp[64] = {0};
for(int j = 0; j < 64; j++){
temp[j] = KeyBit[i] ^ KeyBit[i + 64];
temp[j] = temp[j] ^ KeyBit[i + 128];
}
bool tep[48] = {0};
for(int j = 0; j < 16; j++){
tep[j] = (temp[j]^temp[j+16])^(temp[j+32]^temp[j+48]);
tep[j+16] = tep[j];
tep[j+32] = tep[j];
}
for(int j = 0; j < 16; j++)
SubKey[i][j] = tep[j];
}
}
// 執(zhí)行DES加密
void PlayDes(char MesOut[8],char MesIn[8]){
static bool MesBit[64]={0}; // 明文二進(jìn)制存儲空間 64位
static bool Temp[32]={0};
static bool *MiL=&MesBit[0],*MiR=&MesBit[32]; // 前32位 后32位
ByteToBit(MesBit,MesIn,64); // 把明文換成二進(jìn)制存入MesBit
TablePermute(MesBit,MesBit,IP_Table,64); // IP置換
for(int i = 0; i < 16; i++){
BitsCopy(Temp,MiR,32); // 臨時存儲
F_Change(MiR,SubKey[i]); // F函數(shù)變換
Xor(MiR,MiL,32); // 得到Ri
BitsCopy(MiL,Temp,32); // 得到Li
}
TablePermute(MesBit,MesBit,IPR_Table,64);
BitToHex(MesOut,MesBit,64);
}
// 執(zhí)行DES解密
void KickDes(char MesOut[8],char MesIn[8]){
static bool MesBit[64]={0}; // 密文二進(jìn)制存儲空間 64位
static bool Temp[32]={0};
static bool *MiL=&MesBit[0],*MiR=&MesBit[32]; // 前32位 后32位
HexToBit(MesBit,MesIn,64); // 把密文換成二進(jìn)制存入MesBit
TablePermute(MesBit,MesBit,IP_Table,64); // IP置換
for(int i = 15; i >= 0; i--){
BitsCopy(Temp,MiL,32);
F_Change(MiL,SubKey[i]);
Xor(MiL,MiR,32);
BitsCopy(MiR,Temp,32);
}
TablePermute(MesBit,MesBit,IPR_Table,64);
BitToByte(MesOut,MesBit,64);
}
(2)報告DES雪崩效應(yīng)的觀察結(jié)果。
密鑰固定,明文改變一個字節(jié)
假定密鑰為11111111(00000001 00000001 00000001 00000001 00000001 00000001 00000001 00000001);
明文為12345678(00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000)。
明文變動為10345678(00000001 00000000 00000011 00000100 00000101 00000110 00000111 00001000)僅變動一位。
分析:
將原明文12345678加密后的最終結(jié)果為:
9 9 F B 3 C A 8 1 4 2 E 8 3 2 1
將變動后的明文10345678加密后的最終結(jié)果為:
2 7 8 B F B E 1 9 F 8 D 7 2 3 6
對每一輪數(shù)出的加密結(jié)果統(tǒng)計分析,得到每一輪的加密結(jié)果差異bit位數(shù)如下表所示:
加密輪數(shù) | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
密文相差比特數(shù) | 1 | 4 | 15 | 31 | 33 | 33 |
加密輪數(shù) | 7 | 8 | 9 | 10 | 11 | 12 |
密文相差比特數(shù) | 37 | 34 | 32 | 37 | 37 | 33 |
加密輪數(shù) | 13 | 14 | 15 | 16 | ||
密文相差比特數(shù) | 28 | 30 | 36 | 34 |
通過統(tǒng)計分析,可以得出結(jié)論:DES加密具有雪崩效應(yīng)。
(3)對觀察結(jié)果的分析。
分析過程及結(jié)果同上:DES加密具有雪崩效應(yīng)
(4)報告對電碼本模式和分組鏈接模式中密文錯誤時的觀察結(jié)果;并對結(jié)果進(jìn)行分析。*
(5)報告對密碼反饋模式和輸出反饋模式中密文錯誤時的觀察結(jié)果;并對結(jié)果進(jìn)行分析。**文章來源:http://www.zghlxwxcb.cn/news/detail-734667.html
七、實驗總結(jié)
構(gòu)造一個具備良好雪崩效應(yīng)的密碼或散列是至關(guān)重要的設(shè)計目標(biāo)之一。若某種塊密碼或加密散列函數(shù)沒有顯示出一定程度的雪崩特性,那么它被認(rèn)為具有較差的隨機(jī)化特性,從而密碼分析者得以僅僅從輸出推測輸入。這可能導(dǎo)致該算法部分乃至全部被破解。因此,從加密算法或加密設(shè)備的設(shè)計者角度來說,滿足雪崩效應(yīng)乃是必不可缺的圭臬。
這正是絕大多數(shù)塊密碼采用了乘積密碼的原因,也是大多數(shù)散列函數(shù)使用大數(shù)據(jù)塊的原因。這些特性均使得微小的變化得以通過算法的迭代迅速增殖,造成輸出的每一個二進(jìn)制位在算法終止前均受到輸入的每一個二進(jìn)制位的影響。文章來源地址http://www.zghlxwxcb.cn/news/detail-734667.html
到了這里,關(guān)于現(xiàn)代密碼學(xué)第二次實驗:分組加密算法DES及其工作模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!