ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The library I posted above, will do what you want: It provides several AES versions as well as Blowfish and a lot of other algorithms..., and it is really easy to use! Although I don't know what you mean by "real encryption ... not ciphering." For example: Blowfish is a (block)cipher, so it does ciphering!
The library I posted above, will do what you want: It provides several AES versions as well as Blowfish and a lot of other algorithms..., and it is really easy to use! Although I don't know what you mean by "real encryption ... not ciphering." For example: Blowfish is a (block)cipher, so it does ciphering!
It doesn't seem to have so much documentation and they don't explain any class-method!!! I watched it with some other compter science students and we couldnt figure it out how to start....
The library is not as bad documented as you say (although it is not the best manual out there, that's true). But look into their FAQ, where you will find a lot of sample code. There you will also find a "chm file" that describes the general structure of the library. Note: this manual is for the previous version and the syntax changed at some places, but for learning the basic inner structure it will surely do: http://www.bitvise.com/downloads/CryptoPPGuide.chm
On Linux you can open this with "xchm".
And here a little bonus I wrote for you; this is a program that uses Blowfish to encrypt a plain string and decrypt it back to the original state. All the other blockciphers (like AES) work in the exact same way, so you only have to replace "Blowfish" with "AES":
Note: I assume, that the crypto++ headers are installed to a subdirectory (named crypto++) of one of your default-inlcude-paths. Also to compile the program you have to link it against the libcrypto++.a (g++ sourceFile -lcrypto++)
Code:
#include <iostream>
#include <crypto++/osrng.h> //needed for AutoSeededRandomPool
#include <crypto++/modes.h>
#include <crypto++/blowfish.h>
#include <crypto++/filters.h>
using namespace std;
using namespace CryptoPP; //the general crapto++ namespace
int main() {
AutoSeededRandomPool rng;
string key(Blowfish::DEFAULT_KEYLENGTH, 0);
string iv(Blowfish::BLOCKSIZE, 0);// this is the Initialization Vecktor
//Create a random key as well as a random IV with default sizes (16)
rng.GenerateBlock((unsigned char*)key.c_str(), Blowfish::DEFAULT_KEYLENGTH);
rng.GenerateBlock((unsigned char*)iv.c_str(), Blowfish::BLOCKSIZE);
string plain = "This string will be encrypted throug Blowfish!";
string encrypted; //will store the encrypted string
string decrypted; //will store the string after it is decrypted again
//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( encrypted ) );
stfEncryptor.Put( (unsigned char*)plain.c_str(), plain.size() + 1 );
stfEncryptor.MessageEnd();
// 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( decrypted ) );
stfDecryptor.Put((unsigned char*)encrypted.c_str(), encrypted.size() );
stfDecryptor.MessageEnd();
//Dump "plain"
cout << "Plain: "<< endl << plain << endl << endl;
//Dump "encrypted" (convert it to hexadecimal numbers for better reading)
cout << "Encrypted text: " << endl;
for( int i = 0; i < encrypted.size(); i++ ) {
cout << hex << (0xff & (int)encrypted[i]) << " ";
}
cout << endl << endl;
// Dump Decrypted Text
cout << "Decrypted Text: " << endl;
cout << decrypted;
cout << endl << endl << ":-)" << endl;
return 0;
}
Instead of encrypting your file with gpg, encrypt its filename, a newline, and then its contents with gpg; send the results to a file with a generic name like encryptedfile1.gpg. Then write an analogous script decrypt with gpg and strip the filename from the top.
Or, going one level of laziness farther, use tar to encode the filename:
Code:
19:06 aluser@alf:/tmp$ echo "super secret contents" > file.txt
19:06 aluser@alf:/tmp$ tar cf - file.txt | gpg -e -o encryptedfile1.gpg -r my@email.address && rm file.txt
19:06 aluser@alf:/tmp$ gpg -d encryptedfile1.gpg | tar xf -
[ type your passphrase ]
19:07 aluser@alf:/tmp$ cat file.txt
super secret contents
but then I get a bunch of errors compiling with the g++ -lcrypto...
I tried changing the <include> to "include" but nothing (untarring all files from the downloaded .zip in a crypto dir didn't do much better!!!!
Instead of encrypting your file with gpg, encrypt its filename, a newline, and then its contents with gpg; send the results to a file with a generic name like encryptedfile1.gpg. Then write an analogous script decrypt with gpg and strip the filename from the top.
Or, going one level of laziness farther, use tar to encode the filename:
Code:
19:06 aluser@alf:/tmp$ echo "super secret contents" > file.txt
19:06 aluser@alf:/tmp$ tar cf - file.txt | gpg -e -o encryptedfile1.gpg -r my@email.address && rm file.txt
19:06 aluser@alf:/tmp$ gpg -d encryptedfile1.gpg | tar xf -
[ type your passphrase ]
19:07 aluser@alf:/tmp$ cat file.txt
super secret contents
Not bad.. thanks however you have to remember name -> new name correspondence... and also you have to retype all new names from shell for each file.. anyway it could possibily work...
but then I get a bunch of errors compiling with the g++ -lcrypto...
I tried changing the <include> to "include" but nothing (untarring all files from the downloaded .zip in a crypto dir didn't do much better!!!!
First of all, you need the development package, which contains also the header files. I'm not so familiar with "urpmi", but 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:
thanks a lot Flesym....now my program is as follows.. it works for normal filenames like ahdjd.ke however if you input a complex filename like Video_File.[Divx ITA].avi it skips the password input and doesn't change the filename!! can you help me? it's the 3 option of the program which matters (choice==3)
A FILE RENAMER-ENCRYPTER-DECRYPTER FOR LINUX
Code:
#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()
{
int choice;
cout<<"//////////////////////////////"<<endl;
cout<<"1 copy file with new encrypted filename\n2 rename file\n3 rename with filename encryption/decryption"<<endl;
cout<<"CTRL+C to exit"<<endl;
cout<<"//////////////////////////////"<<endl;
cin>>choice;
//RENAME WITH A NEWLY CHOSEN FILENAME
if (choice==1)
{
string infilename;
string outfilename;
cout <<"Enter input filename: ";
cin >>infilename;
ifstream input(infilename.c_str());
cout <<"Enter output filename: ";
cin >>outfilename;
ofstream output(outfilename.c_str());
char c;
while (!input.eof())
{
input.get(c);
output << c;
}
}
//RENAME TO DEFAULT NAME
if (choice==2)
{
string infilename;
cout <<"Enter input filename: ";
cin >>infilename;
ifstream input(infilename.c_str());
rename(infilename.c_str(),"nuovofile");
}
//ENCRYPT FILENAME
if (choice==3)
{
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());
}
}
Last edited by rino.caldelli; 03-05-2006 at 10:46 AM.
Dear Flesym
I'm new member and i try to use the blowfish encryption algorithm from the cryptopp library, I'm working on windows 7 and use VS 8.0 and i take your program and tried to compiled it but i had the follwoing errors:
Error 1 error LNK2001: unresolved external symbol "class CryptoPP::NameValuePairs const & const CryptoPP::g_nullNameValuePairs" (?g_nullNameValuePairs@CryptoPP@@3ABVNameValuePairs@1@B)
@monazEssam: This thread is 5 years old. Do not resurrect old threads. Start your own thread instead and do not demand priority attention. This is only urgent for you. We are all volunteers here and will help when we feel like it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.