Discussion:
[cryptopp-users] Stream cipher Encrypt file having problem
h***@gmail.com
2018-09-14 09:41:44 UTC
Permalink
Hi,everyone. I need your help.
I had problem when used stream cipher to encrypt and decrypt file.
I encrypted a file that is 2K, but it would become 3K when I decrypted the
cipher .
I think the problem is that I maybe incorrectly use ifstream or ofstream?
The following is my code.
#include<chacha.h> //包含ChaCha20算法的倎文件
#include<iostream>//䜿甚cin、cout
#include<osrng.h> //䜿甚AutoSeededRandomPool算法
#include<secblock.h> //䜿甚SecByteBlock
#include<string>//䜿甚string
#include<fstream>//䜿甚ifstream、ofstream
using namespace std;//std是C++的呜名空闎
using namespace CryptoPP;//CryptoPP是Cryptopp库的呜名空闎
void EncOrDecFile(const string& srcfile,StreamTransformation& encdec,const
string& desfile);
int main()
{
ChaCha20::Encryption enc;//定义加密对象
ChaCha20::Decryption dec;//定义解密对象
//定义䞀䞪随机数发生噚甚于产生随机的密钥Key和初始向量IV
AutoSeededRandomPool prng;
//劚态申请空闎以存傚接䞋来生成的密钥key和初始向量iv
SecByteBlock key(enc.DefaultKeyLength()),iv(enc.DefaultIVLength());
//产生随机的key和iv
prng.GenerateBlock(key,key.size());
prng.GenerateBlock(iv,iv.size());
//加密和解密准倇
enc.SetKeyWithIV(key,key.size(),iv,iv.size());//讟眮加密key和iv
dec.SetKeyWithIV(key,key.size(),iv,iv.size());//讟眮解密key和iv
EncOrDecFile("stream_02.cpp",enc,"cipher.txt");
EncOrDecFile("cipher.txt",dec,"recover.txt");
return 0;
}
//参数srcfile埅加密或解密的文件
//参数encdec䜿甚的流密码算法
//参数desfile存攟加密或解密结果的文件
void EncOrDecFile(const string& srcfile,StreamTransformation& encdec,const
string& desfile)
{//甚流密码对象encdec实现对文件srcfile的加密或解密操䜜并将操䜜的结果存攟于文件desfile
ifstream infile(srcfile,ios_base::binary);
ofstream outfile(desfile,ios_base::binary);
if(!infile || !outfile)
{
cout << "文件打匀倱莥" << endl;
return ;
}
int iread;
SecByteBlock readstr(1024);
while(infile)
{//文件内容读取没有结束
infile.read((char*)&readstr[0],readstr.size());
iread=infile.gcount();
iread = iread < readstr.size() ? iread : readstr.size();
encdec.ProcessString(&readstr[0],iread);
outfile.write((char*)&readstr[0],readstr.size());
}
infile.close();//关闭文件
outfile.close();//关闭文件
}
--
You received this message because you are subscribed to "Crypto++ Users". More information about Crypto++ and this group is available at http://www.cryptopp.com and http://groups.google.com/forum/#!forum/cryptopp-users.
---
You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Weikeng Chen
2018-09-14 15:38:30 UTC
Permalink
This is clearly a misuse of the mailing list.

Consider that the majority of users in this list are English speakers, you
should also at least translate your comments into English so everyone can
understand you.

Otherwise, such question should go somewhere else rather than this mailing
list.

Anyway, I think the number of bytes you write to is already wrong. Yet,
please note that mailing list doesn't debug, though.
Post by h***@gmail.com
Hi,everyone. I need your help.
I had problem when used stream cipher to encrypt and decrypt file.
I encrypted a file that is 2K, but it would become 3K when I decrypted the
cipher .
I think the problem is that I maybe incorrectly use ifstream or ofstream?
The following is my code.
#include<chacha.h> //包含ChaCha20算法的倎文件
#include<iostream>//䜿甚cin、cout
#include<osrng.h> //䜿甚AutoSeededRandomPool算法
#include<secblock.h> //䜿甚SecByteBlock
#include<string>//䜿甚string
#include<fstream>//䜿甚ifstream、ofstream
using namespace std;//std是C++的呜名空闎
using namespace CryptoPP;//CryptoPP是Cryptopp库的呜名空闎
void EncOrDecFile(const string& srcfile,StreamTransformation& encdec,const
string& desfile);
int main()
{
ChaCha20::Encryption enc;//定义加密对象
ChaCha20::Decryption dec;//定义解密对象
//定义䞀䞪随机数发生噚甚于产生随机的密钥Key和初始向量IV
AutoSeededRandomPool prng;
//劚态申请空闎以存傚接䞋来生成的密钥key和初始向量iv
SecByteBlock key(enc.DefaultKeyLength()),iv(enc.DefaultIVLength());
//产生随机的key和iv
prng.GenerateBlock(key,key.size());
prng.GenerateBlock(iv,iv.size());
//加密和解密准倇
enc.SetKeyWithIV(key,key.size(),iv,iv.size());//讟眮加密key和iv
dec.SetKeyWithIV(key,key.size(),iv,iv.size());//讟眮解密key和iv
EncOrDecFile("stream_02.cpp",enc,"cipher.txt");
EncOrDecFile("cipher.txt",dec,"recover.txt");
return 0;
}
//参数srcfile埅加密或解密的文件
//参数encdec䜿甚的流密码算法
//参数desfile存攟加密或解密结果的文件
void EncOrDecFile(const string& srcfile,StreamTransformation& encdec,const
string& desfile)
{//甚流密码对象encdec实现对文件srcfile的加密或解密操䜜并将操䜜的结果存攟于文件desfile
ifstream infile(srcfile,ios_base::binary);
ofstream outfile(desfile,ios_base::binary);
if(!infile || !outfile)
{
cout << "文件打匀倱莥" << endl;
return ;
}
int iread;
SecByteBlock readstr(1024);
while(infile)
{//文件内容读取没有结束
infile.read((char*)&readstr[0],readstr.size());
iread=infile.gcount();
iread = iread < readstr.size() ? iread : readstr.size();
encdec.ProcessString(&readstr[0],iread);
outfile.write((char*)&readstr[0],readstr.size());
}
infile.close();//关闭文件
outfile.close();//关闭文件
}
--
You received this message because you are subscribed to "Crypto++ Users".
More information about Crypto++ and this group is available at
http://www.cryptopp.com and
http://groups.google.com/forum/#!forum/cryptopp-users.
---
You received this message because you are subscribed to the Google Groups
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to "Crypto++ Users". More information about Crypto++ and this group is available at http://www.cryptopp.com and http://groups.google.com/forum/#!forum/cryptopp-users.
---
You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jeffrey Walton
2018-09-14 17:00:37 UTC
Permalink
Post by h***@gmail.com
Hi,everyone. I need your help.
I had problem when used stream cipher to encrypt and decrypt file.
I encrypted a file that is 2K, but it would become 3K when I decrypted the
cipher .
I think the problem is that I maybe incorrectly use ifstream or ofstream?
The following is my code.
#include<chacha.h> //包含ChaCha20算法的倎文件
#include<iostream>//䜿甚cin、cout
#include<osrng.h> //䜿甚AutoSeededRandomPool算法
#include<secblock.h> //䜿甚SecByteBlock
#include<string>//䜿甚string
#include<fstream>//䜿甚ifstream、ofstream
using namespace std;//std是C++的呜名空闎
using namespace CryptoPP;//CryptoPP是Cryptopp库的呜名空闎
void EncOrDecFile(const string& srcfile,StreamTransformation& encdec,const
string& desfile);
int main()
{
ChaCha20::Encryption enc;//定义加密对象
ChaCha20::Decryption dec;//定义解密对象
//定义䞀䞪随机数发生噚甚于产生随机的密钥Key和初始向量IV
AutoSeededRandomPool prng;
//劚态申请空闎以存傚接䞋来生成的密钥key和初始向量iv
SecByteBlock key(enc.DefaultKeyLength()),iv(enc.DefaultIVLength());
//产生随机的key和iv
prng.GenerateBlock(key,key.size());
prng.GenerateBlock(iv,iv.size());
//加密和解密准倇
enc.SetKeyWithIV(key,key.size(),iv,iv.size());//讟眮加密key和iv
dec.SetKeyWithIV(key,key.size(),iv,iv.size());//讟眮解密key和iv
EncOrDecFile("stream_02.cpp",enc,"cipher.txt");
EncOrDecFile("cipher.txt",dec,"recover.txt");
return 0;
}
//参数srcfile埅加密或解密的文件
//参数encdec䜿甚的流密码算法
//参数desfile存攟加密或解密结果的文件
void EncOrDecFile(const string& srcfile,StreamTransformation& encdec,const
string& desfile)
{//甚流密码对象encdec实现对文件srcfile的加密或解密操䜜并将操䜜的结果存攟于文件desfile
ifstream infile(srcfile,ios_base::binary);
ofstream outfile(desfile,ios_base::binary);
if(!infile || !outfile)
{
cout << "文件打匀倱莥" << endl;
return ;
}
int iread;
SecByteBlock readstr(1024);
while(infile)
{//文件内容读取没有结束
infile.read((char*)&readstr[0],readstr.size());
iread=infile.gcount();
iread = iread < readstr.size() ? iread : readstr.size();
encdec.ProcessString(&readstr[0],iread);
outfile.write((char*)&readstr[0],readstr.size());
}
infile.close();//关闭文件
outfile.close();//关闭文件
}
From a quick reading of the code above it looks like the issue with the way
the streams are being used.

You might want to checkout https://www.cryptopp.com/wiki/Chacha20 and
https://www.cryptopp.com/wiki/Pumping_Data .

Jeff
--
You received this message because you are subscribed to "Crypto++ Users". More information about Crypto++ and this group is available at http://www.cryptopp.com and http://groups.google.com/forum/#!forum/cryptopp-users.
---
You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cryptopp-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...