当前位置:文档之家› 现代密码学-RC4校验 实验报告

现代密码学-RC4校验 实验报告

现代密码学实验报告院系:理学院班级:信安二班姓名:学号:前言密码学(Cryptology)是研究秘密通信的原理和破译秘密信息的方法的一门学科。

密码学的基本技术就是对数据进行一组可逆的数学变换,使未授权者不能理解它的真实含义。

密码学包括密码编码学(Cryptography)和密码分析学(Cryptanalyst)两个既对立又统一的主要分支学科。

研究密码变化的规律并用之于编制密码以保护信息安全的科学,称为密码编码学。

研究密码变化的规律并用之于密码以获取信息情报的科学,称为密码分析学,也叫密码破译学。

密码学在信息安全中占有非常重要的地位,能够为信息安全提供关键理论与技术。

密码学是一门古老而深奥的学问,按其发展进程,经历了古典密码和现代密码学两个阶段。

现代密码学(Modern Cryptology)通常被归类为理论数学的一个分支学科,主要以可靠的数学方法和理论为基础,为保证信息的机密性、完整性、可认证性、可控性、不可抵赖性等提供关键理论与技术。

RC4密码算法算法实现实验目的:理解流密码的概念及相关结构; 理解并能够编写基本的流密码体制; 熟练应用C/C++编程实现RC4密码算法体制。

实验内容:编程实现RC4加/解密算法。

实验原理:RC4算法是一种序列密码体制或称流密码体制,其加密密钥和解密密钥相同RC4的密钥长度可变,但为了确保哪去安全强度,目前RC4至少使用128位的密钥。

用1~256个字节(8~2048位)的可变长度密钥初始化一个256个字节的状态向量S,S的元素记为S[0],S[1],…,S[255],从始至终置换后的S包含从0到255的所有8位数。

对于加密和解密,字节K是从S的255个元素中按一种系统化的方式选出的一个元素生成的。

每生成一个K的值,S中的元素个体就被重新置换一次。

实验代码:Encrypt.h文件:#ifndef _ENCRYPT_RC4_#define _ENCRYPT_RC4_#include <string.h>#define BOX_LEN 256intGetKey(const unsigned char* pass, intpass_len, unsigned char *out);int RC4(const unsigned char* data, intdata_len, const unsigned char* key, intkey_len, unsigned char* out, int* out_len);static void swap_byte(unsigned char* a, unsigned char* b);char* Encrypt(const char* szSource, const char* szPassWord); // 加密,返回加密结果char* Decrypt(const char* szSource, const char* szPassWord); // 解密,返回解密结果char* ByteToHex(const unsigned char* vByte, constintvLen); // 把字节码pbBuffer转为十六进制字符串,方便传输unsigned char* HexToByte(const char* szHex); // 把十六进制字符串转为字节码pbBuffer,解码#endif // #ifndef _ENCRYPT_RC4_Encrypt.cpp文件:#include "Encrypt.h"char* Encrypt(const char* szSource, const char* szPassWord) // 加密,返回加密结果{if(szSource == NULL || szPassWord == NULL) return NULL;unsigned char* ret = new unsigned char[strlen(szSource)];intret_len = 0;if(RC4((unsigned char*)szSource,strlen(szSource),(unsigned char*)szPassWord,strlen(szPassWord),ret,&ret_len) == NULL)return NULL;char* ret2 = ByteToHex(ret, ret_len);delete[] ret;return ret2;}char* Decrypt(const char* szSource, const char* szPassWord) // 解密,返回解密结果{if(szSource == NULL || (strlen(szSource)%2 != 0) || szPassWord == NULL) return NULL;unsigned char* src = HexToByte(szSource);unsigned char* ret = new unsigned char[strlen(szSource) / 2 + 1];intret_len = 0;memset(ret, strlen(szSource) / 2 + 1,0);if(RC4(src, strlen(szSource) / 2, (unsigned char*)szPassWord, strlen(szPassWord), ret, &ret_len) == NULL)return NULL;ret[ret_len] = '/0';return (char*)ret;}int RC4(const unsigned char* data, intdata_len, const unsigned char* key, intkey_len, unsigned char* out, int* out_len){if (data == NULL || key == NULL || out == NULL)return NULL;unsigned char* mBox = new unsigned char[BOX_LEN];if(GetKey(key, key_len, mBox) == NULL)return NULL;inti=0;int x=0;int y=0;for(int k = 0; k <data_len; k++){x = (x + 1) % BOX_LEN;y = (mBox[x] + y) % BOX_LEN;swap_byte(&mBox[x], &mBox[y]);out[k] = data[k] ^ mBox[(mBox[x] + mBox[y]) % BOX_LEN];}*out_len = data_len;delete[] mBox;return -1;}intGetKey(const unsigned char* pass, intpass_len, unsigned char* out) {if(pass == NULL || out == NULL)return NULL;inti;for(i = 0; i< BOX_LEN; i++)out[i] = i;int j = 0;for(i = 0; i< BOX_LEN; i++){j = (pass[i % pass_len] + out[i] + j) % BOX_LEN;swap_byte(&out[i], &out[j]);}return -1;}static void swap_byte(unsigned char* a, unsigned char* b){unsigned char swapByte;swapByte = *a;*a = *b;*b = swapByte;}// 把字节码转为十六进制码,一个字节两个十六进制,内部为字符串分配空间char* ByteToHex(const unsigned char* vByte, constintvLen){if(!vByte)return NULL;char* tmp = new char[vLen * 2 + 1]; // 一个字节两个十六进制码,最后要多一个'/0'int tmp2;for (inti=0;i<vLen;i++){tmp2 = (int)(vByte[i])/16;tmp[i*2] = (char)(tmp2+((tmp2>9)?'A'-10:'0'));tmp2 = (int)(vByte[i])%16;tmp[i*2+1] = (char)(tmp2+((tmp2>9)?'A'-10:'0'));}tmp[vLen * 2] = '/0';return tmp;}// 把十六进制字符串,转为字节码,每两个十六进制字符作为一个字节unsigned char* HexToByte(const char* szHex){if(!szHex)return NULL;intiLen = strlen(szHex);if (iLen<=0 || 0!=iLen%2)return NULL;unsigned char* pbBuf = new unsigned char[iLen/2]; // 数据缓冲区int tmp1, tmp2;for (inti=0;i<iLen/2;i++){tmp1 = (int)szHex[i*2] - (((int)szHex[i*2]>='A')?'A'-10:'0');if(tmp1>=16)return NULL;tmp2 = (int)szHex[i*2+1] - (((int)szHex[i*2+1]>='A')?'A'-10:'0');if(tmp2>=16)return NULL;pbBuf[i] = (tmp1*16+tmp2);}return pbBuf;}main.cpp文件#include <iostream>#include <string>#include <stdio.h>#include "Encrypt.h"intmain(){inti;std::cout<< "请选择你要进行的操作:1 .加密 2.解密" <<std::endl;std::cin>>i;if (i == 1){char source[100];char pass[100];char *result = NULL;std::cout<< "请输入明文:";std::cin>> source; std::cout<<std::endl;std::cout<< "请输入密钥:";std::cin>> pass;result = Encrypt(source, pass);printf("密文为:%s/n", result);delete[]result;}else if (i == 2){char result[100];char pass[100];char *source = NULL;std::cout<< "请输入密文:";std::cin>> result; std::cout<<std::endl;std::cout<< "请输入密钥:";std::cin>> pass;source = Encrypt(result, pass);printf("明文为:%s/n", source);delete[]source;}elsestd::cout<< "您输入的不合法!" <<std::endl;return 0;}实验结果:。

相关主题