h***@gmail.com
2018-09-14 09:41:44 UTC
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();//å ³éæ件
}
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.
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.