文件加解密完整性校验

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

上海电力学院

网络安全程序设计

实验报告

题目:基于CryptoAPI 实现文件加解密、完整性校验院系:计算机科学与技术学院

专业年级:信息安全三年级

学生姓名:吴xx

学号:xxxxxxxx

一、实验目的和要求:

掌握基于CryptoAPI文件加密、解密、完整性校验的流程以及实现方法。

二、实验内容:

1)基于CryptoAPI的文件加密;

2)基于CryptoAPI的文件解密;

三、主要实验设备:

PC机,Windows XP,VC++6.0.

四、实验步骤

1)在VC下配置CryptoAPI编译环境

2) 在VC++6.0下实现基于CryptoAPI的文件加密。

3) 在VC++6.0下实现基于CryptoAPI的文件解密。

4)实现基于CryptAPI实现基于MD5的文件完整性校验。

要求:

1)给出算法的详细实现过程。

a)获得csp句柄

b)构造密钥

c)数据加密

d)数据解密

e)释放相关资源

2)给出算法实现的流程图

1.加密时:(1)创建会话密钥

(2)加密数据

(3)安全保存或交换会话密钥

2.解密时:(1)获取会话密钥

(2)解密数据

基于CryptoAPI的文件加密

#ifndef _WIN32_WINNT

#define _WIN32_WINNT 0x0400

#endif

#include

#include

#include

#include

#include

#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING|X509_ASN_ENCODING)

#define KEYLENGTH 0x00800000

void HandleError(char*s);

HCRYPTPROV GetCryptProv();

#define ENCRYPT_ALGORITHM CALG_RC4

#define ENCRYPT_BLOCK_SIZE 8

BOOL EncryptFile(

PCHAR szSource,

PCHAR szDestination,

PCHAR szPassword);

HCRYPTKEY GenKeyByPassword(HCRYPTPROV hCryptProv,PCHAR szPassword);

HCRYPTKEY GenKeyByRandom(HCRYPTPROV hCryptProv,FILE*hDestination);

void main(void)

{

PCHAR szSource;

PCHAR szDestination;

CHAR szPassword[100]="";

char response;

if(!(szSource=(char*)malloc(100)))

HandleError("Memory allocation failed.");

if(!(szDestination=(char*)malloc(100)))

HandleError("Memory allocation failed.");

printf("加密一个文件.\n\n");

printf("请输入需要被加密文件的名称:");

fgets(szSource,100,stdin);

if(szSource[strlen(szSource)-1]=='\n')

szSource[strlen(szSource)-1]='\0';

printf("请输入需要输出文件文件的名称:");

fgets(szDestination,100,stdin);

if(szDestination[strlen(szDestination)-1]=='\n')

szDestination[strlen(szDestination)-1]='\0';

printf("要使用密码对这个文件加密吗?(y/n)");

response=getchar();

if(response=='y')

{

printf("请输入密码:");

getchar();

gets(szPassword);

else

{

printf("密钥将生成但没有使用密码.\n");

}

if(EncryptFile(szSource,szDestination,szPassword))

{

printf("对文件%s的加密已经成功1\n",

szSource);

printf("加密后的数据存储在文件%s中.\n",szDestination);

}

else

{

HandleError("解密文件出错!");

}

if(szSource)

free(szSource);

if(szDestination)

free(szDestination);

}

static BOOL EncryptFile(

PCHAR szSource,

PCHAR szDestination,

PCHAR szPassword)

{

FILE *hSource;

FILE *hDestination;

HCRYPTPROV hCryptProv;

HCRYPTKEY hKey;

PBYTE pbBuffer;

DWORD dwBlockLen;

DWORD dwBufferLen;

DWORD dwCount;

if(hSource=fopen(szSource,"rb"))

{

printf("原文件%s已经打开.\n",szSource);

}

else

{

HandleError("打开原文件出错!");

}

if(hDestination=fopen(szDestination,"wb"))

{

printf("目标文件%s已经打开.\n",szDestination);

相关文档
最新文档