Encrypt 和 Decrypt 的问题

zyc_lc 2008-10-23 10:33:50
int RSA_Public_Encrypt (unsigned char *this_output , /* this_output */
unsigned int *this_output_size, /* length of this_output */
unsigned char *this_input , /* this_input */
unsigned int this_input_size ) /* length of this_input */
//加密函数 是将一堆数据封装在一个字符串中(CString)然后写入一个.cri文件中
CFile MyResultFile;
MyResultFile.Open(this->m_FilePath, CFile::modeWrite, &MyException))后直接写入MyResultFile.Write()...
本来一位工程师写了个工具来读取那个加密后的.cri文件的.只是最近发现它有不少问题,想自己写一个,但是又没有原工具的文档和代码参考...

int RSA_Private_Decrypt (unsigned char *this_output , /* this_output */
unsigned int *this_output_size, /* length of this_output */
unsigned char *this_input , /* this_input */
unsigned int this_input_size ) /* length of this_input */
//解密函数
然后我不知道是不是也是直接
CFile MyResultFile;
void *ResultStr;
MyResultFile.Open(this->m_EncypytionFilePathStr, CFile::modeRead, &MyException)
{
MyResultFile.Read(ResultStr, sizeof(unsigned int)/sizeof(char));
...
这样能将里面的数据保存到ResultStr中吗?
然后我是不是就可以调用RSA_Private_Decrypt来解密,然后再转化为CString型,然后将其写入一个EXECL表中.
其中的内容是一些字段和字段的数值...
望高人指点,谢谢!
...全文
381 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyc_lc 2008-10-27
  • 打赏
  • 举报
回复
string来显示---我是测试着看看大概正确与否,谢谢你,我试一下.
tailzhou 2008-10-27
  • 打赏
  • 举报
回复
if(MyResultFile.Open(this->m_EncypytionFilePathStr, CFile::modeRead, &MyException))

文件需要用二进制打开的;

CFile::modeRead | CFile::typeBinary

并且加密后的内容当作string来显示也是没有任何意义的;


zyc_lc 2008-10-27
  • 打赏
  • 举报
回复
发现;dwLenght = MyResultFile.GetLength();
resultStr = (char *)malloc(dwLenght);
MyResultFile.Read(resultStr,(int)dwLenght);
MyResultFile.Close();
后并不能正确得到加密文件的内容,因为那个加密文件本来就有很多内容,但是resultStr在DEBUG时只现实了两个字符的乱码.
是不是加密过的文件不能直接读取?
我测试了个文本的,可以读取,只是这个加密后的连整体数据的大小后无法正确读取.虽然dwLenght可以得到1924个字节.

错误信息无.
我想因为要输入的将要被解密的信息是随机给的乱码或则只有两个字节的乱码就本身不是原来的数据了,自然不能被解密.
该怎么办呢/....

tailzhou 2008-10-27
  • 打赏
  • 举报
回复
加密后的数据原本就是一堆乱码的吧;

解密函数还是执行错误?错误信息?
zyc_lc 2008-10-27
  • 打赏
  • 举报
回复
我只是发现原本400多字节的内容如果不SeekToEnd(); 则只能在decryptInputLength = decryptInputStr.GetLength(); 中得到2个字节,但是如果SeekToEnd(); 了就可以得到400多字节的大小.

CFile MyResultFile;
CFileException MyException;

char *resultStr;
DWORD dwLenght;
if(MyResultFile.Open(this->m_EncypytionFilePathStr, CFile::modeRead, &MyException))
{
// dwLenght = MyResultFile.SeekToEnd();
dwLenght = MyResultFile.GetLength();
resultStr = (char *)malloc(dwLenght);
CString str;
str.Format("%d",(int)dwLenght);
AfxMessageBox(str);
MyResultFile.Read(resultStr,(int)dwLenght);
MyResultFile.Close();
}
我现在用GetLength();来代替SeekToEnd()发现得到的dwLenght是一样的,但是在
CString decryptInputStr;
decryptInputStr = resultStr;
char *pDecryptInput;
AfxMessageBox(decryptInputStr);
时decryptInputStr的内容从初始值变为了一个乱码....
tailzhou 2008-10-27
  • 打赏
  • 举报
回复
MyResultFile.SeekToEnd(); //???
MyResultFile.Read(resultStr, 1024);
MyResultFile.Close();

为什么要SeekToEnd再Read?
这样读到的是什么??
zyc_lc 2008-10-27
  • 打赏
  • 举报
回复
好像可以了 我再试...
tailzhou 2008-10-27
  • 打赏
  • 举报
回复
pDecryptInput = decryptInputStr.GetBuffer(decryptInputLength);

把decryptInputStr相关的语句的都去掉,直接用resultStr来调用RSA_Private_Decrypt看看;
zyc_lc 2008-10-27
  • 打赏
  • 举报
回复
谢谢tailzhou.

ecryptStatus = RSA_Private_Decrypt((unsigned char *)pDecryptOutput,(unsigned int *)\
&decryptOutputLength,(unsigned char *)pDecryptInput,(unsigned int)decryptInputLength);

if(decryptStatus == 0)
{
AfxMessageBox("decrypt is failed!");
return;
}
else
{
CString s;
s = pDecryptOutput;
AfxMessageBox(s);//测试了一下,显示为"吨吨吨吨吨吨..."的乱码了,...继续请教高人tailzhou麻烦了
}
tailzhou 2008-10-27
  • 打赏
  • 举报
回复
或者
(unsigned int *)(&decryptOutputLength)
也行;
tailzhou 2008-10-27
  • 打赏
  • 举报
回复
你用错函数的参数了;
传进去的decryptOutputLength参数应该是一个指针;
你用(unsigned int *)decryptOutputLength来调用都非法内存访问了;


unsigned int *decryptOutputLength=(int*)malloc(sizeof(unsigned int));
RSA_Private_Decrypt((unsigned char *)pDecryptOutput,decryptOutputLength,(unsigned char *)pDecryptInput,(unsigned int)decryptInputLength)
zyc_lc 2008-10-27
  • 打赏
  • 举报
回复
CFile MyResultFile;
CFileException MyException;
char *resultStr;
int decryptInputLength = 0;

if(MyResultFile.Open(this->m_EncypytionFilePathStr,CFile::modeRead | CFile::typeBinary,&MyException))
{
MyResultFile.Read(&decryptInputLength,sizeof(unsigned int)/sizeof(char));
resultStr = (char *)malloc(decryptInputLength);
MyResultFile.Read(resultStr,decryptInputLength);
MyResultFile.Close();
}
else
{
AfxMessageBox("Open file fail!");
return;
}

int decryptStatus = 0;
CString decryptInputStr;
decryptInputStr = resultStr;
char *pDecryptInput;
AfxMessageBox(decryptInputStr);

pDecryptInput = decryptInputStr.GetBuffer(decryptInputLength);

int decryptOutputLength = RSA_Get_Decrypt_Size(decryptInputLength);
char * pDecryptOutput;
pDecryptOutput = (char *)malloc(decryptOutputLength);

decryptStatus = RSA_Private_Decrypt((unsigned char *)pDecryptOutput,(unsigned int *)decryptOutputLength,(unsigned char *)pDecryptInput,(unsigned int)decryptInputLength);
改进后问题好像仍然在RSA_Private_Decrypt中.看来我的pDecryptInput的数据内容好像还是有问题或则输出的decryptOutputLength的问题...迷茫中
请个位高手指点 谢谢
zyc_lc 2008-10-26
  • 打赏
  • 举报
回复
在int RSA_Public_Encrypt (unsigned char *this_output , /* this_output */
unsigned int *this_output_size, /* length of this_output */
unsigned char *this_input , /* this_input */
unsigned int this_input_size )
之前 有一个RSA_Get_Ciphered_Size()函数,可以通过一个将要加密的字符串得到经过RSA_Public_Encrypt后的数据SIZE.
int RSA_Get_Ciphered_Size (int this_unciphered_data_size)
{
unsigned int key_size, bloc_size, nb_bloc, ciphered_size;
R_RSA_PUBLIC_KEY *this_public_key;

this_public_key = &Public_1024_Key_1;

key_size = (this_public_key->bits + 7) / 8;
bloc_size = key_size - PKCS_1_Header_Size;
nb_bloc = (this_unciphered_data_size + bloc_size - 1) / bloc_size;
ciphered_size = nb_bloc * (bloc_size + PKCS_1_Header_Size);
return (ciphered_size);
} /* RSA_Get_Ciphered_Size () */

于是我对照RSA_Public_Encrypt()写了一个RSA_Get_Decrypt_Size()本意是通过已知的加密数据的SIZE得到解密后数据的SIZE
int RSA_Get_Decrypt_Size (int this_unDecrypt_data_size)
{
unsigned int key_size, bloc_size, nb_bloc, decrypt_size;
R_RSA_PRIVATE_KEY *this_private_key;

this_private_key = &Private_1024_Key_1;

key_size = (this_private_key->bits + 7) / 8;
bloc_size = key_size - PKCS_1_Header_Size;
nb_bloc = this_unDecrypt_data_size/(bloc_size + PKCS_1_Header_Size);
decrypt_size = nb_bloc*bloc_size - (bloc_size - 1);

return (decrypt_size);
}
其实我就是将它反函数一样的倒过来写.

于是整体解密一个加密文件.CRI的程序就是:
CFile MyResultFile;
CFileException MyException;
char *resultStr;
resultStr = (char *)malloc(1024);

if(MyResultFile.Open(this->m_EncypytionFilePathStr, CFile::modeRead, &MyException))
{
MyResultFile.SeekToEnd();
MyResultFile.Read(resultStr, 1024);
MyResultFile.Close();
}
else
{
AfxMessageBox("Open file fail!");
return;
}
int decryptStatus = 0;

CString decryptInputStr;
decryptInputStr = resultStr;
char *pDecryptInput;
AfxMessageBox(decryptInputStr);//----测试一下显示为乱码.
int decryptInputLength = 0;
decryptInputLength = decryptInputStr.GetLength();
pDecryptInput = decryptInputStr.GetBuffer(decryptInputLength);

int decryptOutputLength = RSA_Get_Decrypt_Size(decryptInputLength);
char * pDecryptOutput;
pDecryptOutput = (char *)malloc(decryptOutputLength);

decryptStatus = RSA_Private_Decrypt((unsigned char *)pDecryptOutput,\
(unsigned int *)decryptOutputLength,(unsigned char *)pDecryptInput,(unsigned int)decryptInputLength);
但是DEBUG时显示到RSA_Private_Decrypt()时出错,不知道是什么原因,请教高手...
谢谢
meihuiyu 2008-10-24
  • 打赏
  • 举报
回复
up
yangkunjie 2008-10-24
  • 打赏
  • 举报
回复
感觉思路还比较对路
得不断测试,看是否达到自己的要求
currenttt 2008-10-24
  • 打赏
  • 举报
回复
有没有为ResultStr分配空间?如果不分配空间这样写的话,难道是内存映射文件?

先检查文件是否已经正确打开,在到Read函数中去看看是否能够正确地取出文件内容

如果Read函数你不需要管的话,那我想应该为指针ResultStr分配空间,再将其传入。另外,我觉得这里的ResultStr可以用char*来做
zyc_lc 2008-10-24
  • 打赏
  • 举报
回复
今天测试了几次都失败了,求救啊...
绿色夹克衫 2008-10-24
  • 打赏
  • 举报
回复
看起来是会把解密后的内容存在this_output中,我看你写的没什么问题呀!

就算帮你顶一下吧!

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧