100分求字符串和文件加密解密源码或控件
的网上找不很久,也没找到合适的,希望有相关资源的给我一份,我希望的字符串加密后的密文是可显示字符,即可以的文件框中显示的。我的mail:
crossbeam@126.com
[解决办法]
我找到一个文件加密的
//---------------------------------------
//.h
#ifndef EncryptFileH
#define EncryptFileH
//---------------------------------------
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wincrypt.h>
#ifdef USE_BLOCK_CIPHER
#define ENCRYPT_ALGORITHM CALG_RC2
#define ENCRYPT_BLOCK_SIZE 8
#else
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 1
#endif
class Encrypt
{
private:
String psw;
public:
//CONSTRUCTOR
Encrypt(String password);
//DESTRUCTOR
virtual ~Encrypt();
void EncryptFile(PCHAR szSource,PCHAR szDestination,String szPassword);
void DecryptFile(PCHAR szSource,PCHAR szDestination,String szPassword);
};
#endif
//---------------------------------------
//.cpp
#pragma hdrstop
#include "EncryptFile.h "
//---------------------------------------
#pragma package(smart_init)
Encrypt::Encrypt()
{
}
//---------------------------------------
void Encrypt::EncryptFile(PCHAR szSource, PCHAR szDestination, String szPassword)
{
FILE *hSource=NULL;
FILE *hDestination=NULL;
int eof = 0;
HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
HCRYPTKEY hXchgKey = 0;
HCRYPTHASH hHash = 0;
PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;
PBYTE pbBuffer = NULL;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
hSource = fopen(szSource, "rb ");
hDestination = fopen(szDestination, "wb ") ;
if (hSource==NULL) {ShowMessage( "open Source File error ! "); return ;}
if (hDestination==NULL){ ShowMessage( "open Destination File error ! "); return;}
if(!CryptAcquireContext(&hProv, NULL, NULL,PROV_RSA_FULL, 0))
{
ShowMessage( "Error reading CSP name "+ GetLastError());
return;
}
if(szPassword == " ")
{
if(!CryptGenKey(hProv, ENCRYPT_ALGORITHM,CRYPT_EXPORTABLE, &hKey))
ShowMessage( "Error "+ GetLastError());
if(!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hXchgKey))
ShowMessage( "Error "+ GetLastError());
if(!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0,NULL, &dwKeyBlobLen))
ShowMessage( "error "+GetLastError());
pbKeyBlob=(PBYTE )malloc(dwKeyBlobLen) ;
if(!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB,0, pbKeyBlob, &dwKeyBlobLen))
ShowMessage( "error "+GetLastError());
CryptDestroyKey(hXchgKey);
hXchgKey = 0;
fwrite(&dwKeyBlobLen, sizeof(DWORD), 1, hDestination);
fwrite(pbKeyBlob, 1, dwKeyBlobLen, hDestination);
}
else {
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
ShowMessage( "error "+GetLastError());
if(!CryptHashData(hHash, szPassword.c_str() , szPassword.Length(), 0))
ShowMessage( "error "+GetLastError());
if(!CryptDeriveKey(hProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey))
ShowMessage( "error "+GetLastError());
CryptDestroyHash(hHash);
hHash = 0;
}
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
if( ENCRYPT_BLOCK_SIZE> 1 )
{
dwBufferLen=dwBlockLen+ENCRYPT_BLOCK_SIZE;
}
else
{
dwBufferLen=dwBlockLen;
}
pbBuffer=(PBYTE )malloc(dwBufferLen);
do {
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
eof = feof(hSource);
CryptEncrypt(hKey, 0, eof, 0, pbBuffer,&dwCount, dwBufferLen);
fwrite(pbBuffer, 1, dwCount, hDestination);
} while(!feof(hSource));
ShowMessage( "¼ÓÃÜ OK! ");
fclose(hSource);
fclose(hDestination);
}
//-------------------------------------------------------
void Encrypt::DecryptFile(PCHAR szSource, PCHAR szDestination, String szPassword)
{
FILE *hSource = NULL;
FILE *hDestination = NULL;
int eof = 0;
HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
HCRYPTKEY hXchgKey = 0;
HCRYPTHASH hHash = 0;
PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;
PBYTE pbBuffer = NULL;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
hSource = fopen(szSource, "rb ");
hDestination = fopen(szDestination, "wb ") ;
if (hSource==NULL) { ShowMessage( "open Source File error ! "); return;}
if (hDestination==NULL){ ShowMessage( "open Destination File error ! "); return;}
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))
{
ShowMessage( "Error reading CSP name "+ GetLastError());
return;
}
if(szPassword == " ") {
fread(&dwKeyBlobLen, sizeof(DWORD), 1, hSource);
pbKeyBlob =(PBYTE)malloc(dwKeyBlobLen);
fread(pbKeyBlob, 1, dwKeyBlobLen, hSource);
if(!CryptImportKey(hProv, pbKeyBlob,dwKeyBlobLen, 0, 0, &hKey))
ShowMessage( "Error "+ GetLastError());
}
else {
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
ShowMessage( "Error "+ GetLastError());
if(!CryptHashData(hHash, szPassword.c_str() , szPassword.Length() , 0))
ShowMessage( "Error "+ GetLastError());
if(!CryptDeriveKey(hProv, ENCRYPT_ALGORITHM,hHash, 0, &hKey))
ShowMessage( "Error "+ GetLastError());
CryptDestroyHash(hHash);
hHash = 0;
}
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
if(ENCRYPT_BLOCK_SIZE > 1)
{
dwBufferLen =dwBlockLen+ENCRYPT_BLOCK_SIZE;
}
else
{
dwBufferLen = dwBlockLen;
}
pbBuffer =(PBYTE )malloc(dwBufferLen);
do {
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
eof = feof(hSource);
if(!CryptDecrypt(hKey, 0, eof, 0, pbBuffer, &dwCount))
ShowMessage( "Error "+ GetLastError());
fwrite(pbBuffer, 1, dwCount, hDestination);
} while(!feof(hSource));
ShowMessage( "OK! ");
fclose(hSource);
fclose(hDestination);
}
[解决办法]
我那个是FOR CB5,CB6的,没有安2006,所以也就没做2006的控件。
你到该算法的原作者处下代码吧。
http://fp.gladman.plus.com/cryptography_technology/rijndael/
[解决办法]
哇~大哥,我路过
为了保证自己的数据不被别人非法地窃取,有必要对数据文件进行适当地加密。本程序采取一定的算法,对密码和数据进行加密,生成一个自解密的EXE文件。这个自解密文件是由两部分组成的。第一部分为可执行代码部分,其作用是对密码进行判断,对数据进行解密;后一部分是被加过密的密码和数据。
本程序分两个模块进行编写。第一个是main.c,它是用来进行加密,产生EXE文件的;第二个是main2.c,它是用来解密的。分别对它们进行编译生成main.exe和main2.exe,再对其进行合并即可。
合并方法如下:
COPY /B main.EXE+main2.EXE Key.EXE(注意main.EXET和main2.EXE的顺序)
使用时请按下列格式键入。
Key〈欲加密文件〉〈产生后的EXE文件名〉
按提示输入密码后将生成指定的可执行文件。解密时运行这个可执行文件。输入密码后(若输入三次都不正确,将退出解密程序),即把原文件内容还原到你指定的文件中。解密程序格式如下。
可执行文件名 (filename)
源程序如下:
第一个源代码文件 main.c
#include “stdio.h”
main (int argc,char *argv[])
{FILE *fp0, *fp1, *fp2;
char ch,*password,I,str[10];password=str;
if(argc!=3)
{printf(“usage:key ");
exit(0);
}
if((fp0=fopen(argv[0],“rb "))=NULL)
{printf(“can`t open %s ",argv[0]);
exit(0)
}
if((fp1=fopen(argv[1],“r "))=NULL)
{printf(“can`t open %s ")argv[1]);
// 本文转自 C++Builder 研究 - http://www.ccrun.com/article.asp?i=263&d=wwscw2
exit(0);
}
if((fp2=fopen(argv[2],“wb "))=NULL)
{printf(“can`t open %s ",argv[2]);
}
password=(char *)getpass(“please input password: ");
fseek(fp0,1394OL,SEEK_SET);/* 13940是main.exe文件的长度,不同的工作环境会有不同的值。请以实际长度加以修改 */
while(!feOf(fp0))
fputc(fgetc(fp0),fp2);/*把main2.exe输入fp2指定的文件中*/
for(i=0;password[i]!=`10`;i++)
{fputc((i+password[i])&I,fp2);/*将密码加密后写入文件*/
}
fputc(`10`,fp2), /*将“10”放入密码后 */
do{
{for(i=0;password[i]!=`10`,i++
{ch=fgetc(fp1) /*从欲加密文件读一个字符*/
if(feof(fp1))break;
ch=ch&password[i];
ch+=I;
ch=nch; /*将此字符同密码i值进行运算后写入fp2指定文件*/
fputc(ch,fp2);
}
}While(!feof(fp1));
fcloseal?;
}
第二个源代码文件main2.c
#include “stdio.h "
#include “string.h "
main(int argc,char *argv[])
{FILE *FP0,*FP1;
char *password,old password[10],str[10],ch,I;
if(argc!=2)
{printf(“Usage=%s ",argv[0]);
exit(o);
}
fseek(fp0,16888L,SEEK_SET); /*同理16888也视实际情况而定*/
fgetc(fp0); /*读掉一个无效字符*/
for(i=0;(oldpassword[i]=fget((fp0))!=`10`;i++)
Oldpassword[i]=(oldpassword[i]&i)-I; /*对密码进行还原*/
oldpassword[i]=`10`; /*把“10”藏于密码后 */
password=(char *)getpass(“please input password: ");
for(i=0;I {if(strcmp(password,oldpassword))
{password=(ch *)getpass(“
password is wrong.
please input agai: ");
if(i==2)
{printf(“
press any Key to exit……
");
getch();exit(o)
}
}
}/*对密码进行判断,有三次机会,都不正确则返回到操作系统*/
do{
for (i=0;password[i]!=`10`;i++)
{ch=fgetc(fp0);
if(feof(fp0))break;
ch=nch;
ch-=I;
ch&=password[i];
}
}while(!feof(fp0);
fcloseal?;
{
上述程序在COMPAQ 586.TC2.0集成环境里通过。