encrypt filenames debug

R

rino100

can anyone tell me why this c++ code works encrypting simple filenames
but instead if you try to encrypt a filename like "video - 833 12_
..avi" it doesn't rename the file??????

#include <fstream>
#include <iostream>
//cryptopp libraries
#include <cryptopp/osrng.h> //needed for AutoSeededRandomPool
#include <cryptopp/modes.h>
#include <cryptopp/blowfish.h>
#include <cryptopp/filters.h>

using namespace std;
using namespace CryptoPP;

int main()
{

cout<<"//////////////////////////////"<<endl;
cout<<"rename with filename encryption/decryption"<<endl;
cout<<"CTRL+C to exit"<<endl;
cout<<"//////////////////////////////"<<endl;

//ENCRYPT FILENAME

string infilename;
string outfilename;
cout <<"Enter filename of file to encrypt or decrypt: ";
cout<<"\n";
cin >>infilename;
cout<<"\n";
ifstream input(infilename.c_str());

AutoSeededRandomPool rng;
string key(Blowfish::DEFAULT_KEYLENGTH, 0);
string iv(Blowfish::BLOCKSIZE, 0);// this is the Initialization
Vecktor

cout<<"enter the password..(12 charachters max)"<<endl;
cout<<"\n";
cin>>key;
iv="àc€­Õ=Xòžy"; //default block

cout<<"Do you want to encrypt 1 \nor to decrypt 0 \n?";
int choice;
cin>>choice;
if (choice)
{
//Setup the Blowfish Cipher in CBC-Mode
Blowfish::Encryption blowEn((unsigned char*)key.c_str(),
key.size());
CBC_Mode_ExternalCipher::Encryption cbcEn( blowEn, (unsigned
char*)iv.c_str() );

//Put the "plain" string into the cipher and encrypt it to
"encrypted
StreamTransformationFilter stfEncryptor(cbcEn, new StringSink(
outfilename ) );
stfEncryptor.Put( (unsigned char*)infilename.c_str(),
infilename.size() + 1 );
stfEncryptor.MessageEnd();

}
else
{
// Decrypt (very analog to the encryption block
Blowfish::Decryption blowDe((unsigned char*)key.c_str(),
key.size());
CBC_Mode_ExternalCipher::Decryption cbcDe( blowDe, (unsigned
char*)iv.c_str() );

StreamTransformationFilter stfDecryptor(cbcDe, new StringSink(
outfilename ) );
stfDecryptor.Put((unsigned char*)infilename.c_str(),
infilename.size() );
stfDecryptor.MessageEnd();
}
cout<<"renaming to..."<<outfilename<<endl;
rename(infilename.c_str(),outfilename.c_str());

}
 
M

Marcus Kwok

can anyone tell me why this c++ code works encrypting simple filenames
but instead if you try to encrypt a filename like "video - 833 12_
.avi" it doesn't rename the file?????? [snip]
rename(infilename.c_str(),outfilename.c_str());

Maybe it has something to do with the fact that the filename has spaces
in it. Perhaps you need to surround the filename with quotes.
 
R

rino100

actually it seems working for:
a - 4.bzip
a-k.bzip
a.zip
aaaaaaaaaaa.bzip
ajehkwhferfuiwefhkwehfhweifhwfwkl.bz2

but not for:
a - l.zip

just like as if the combination of "space line space" confuused the
proram... and no.. putting " hypens " before and after the filename
didn't help...

if someone can compile it here's how to do it:

first install cryptolib from site
http://www.eskimo.com/~weidai/cryptlib.html or urpmi libcrypto
you need the development package, which contains also the header files.
In "urpmi" I guess this package is named "libcryptopp5-dev",
"...-devel" or something like this. Then your package manager will most
usually create a link to the actual library. In example if the library
is named "libcryptopp5.0.2.a" (I guess this will be true for you), it
will create a link named "libcryptopp.a" that points to this file.
Similiar is true for the dynamic libraries in your output above
(libcryptopp.so.5.0.2 is usually just link to libcryptopp.so.5,
although we don't use it here). But if you hava a file "libcryptopp.a",
then you link against it with:
Code:

g++ -lcryptopp sourceFile.cpp

About the includes:
The easiest way to find out, where the headers were installed to, is by
just searching for them. In my case I used (for example) the file
"blowfish.h", so:
Code:

cd /usr find -name blowfish.h

If I do this on my system, it shows me, that it is in
"/usr/include/crypto++/". Because "/usr/include" is one of the default
include paths, I can include the file with:
Code:

#include <crypto++/blowfish.h>

If, for example, on your machine the crypto-headers are located in
"/no/default/includes/cryptopp", then you have to include it this way:
Code:

#include "/no/default/includes/cryptopp/blowfish.h"

Or another way would be:
Code:

#include <cryptopp/blowfish.h>

....but then you have to invoke the compiler like so:
Code:

g++ -lcryptopp -I/no/default/includes sourceFile.cpp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top