LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2006, 02:27 PM   #1
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Rep: Reputation: 31
c++ string encryption


anyone knows of a standard c++ function for encrypting strings???

I'm creating a program which changes filenames to encrypted ones because GPG doesn't have this option!!!

thanks
 
Old 02-28-2006, 02:57 PM   #2
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
I don't know such standard c++ function, but the free crypto++ library provides a huge collection of en/decrypting algorithms:
http://www.eskimo.com/~weidai/cryptlib.html
 
Old 02-28-2006, 04:47 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
When you write about encryption, you need to specify the cipher you'd like to use.

You're writing about GPG... What are you trying to do? It may clear thing up.
 
Old 03-02-2006, 05:52 AM   #4
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Original Poster
Rep: Reputation: 31
I posted on this other forum

http://www.linuxquestions.org/questi...d.php?t=419357

the program I'm writing and they helped me a lot...now I need only the last thing to finish it... to basically encrypt filenames...

Any encryption is ok.. only that it is real encryption (Blowfish, GPG, AES etc) not cyphering!!!

thanks
 
Old 03-02-2006, 07:21 AM   #5
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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!
 
Old 03-02-2006, 12:34 PM   #6
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by Flesym
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....
 
Old 03-02-2006, 04:36 PM   #7
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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;
}

Last edited by Flesym; 03-02-2006 at 04:57 PM.
 
Old 03-02-2006, 06:09 PM   #8
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
There's a lazy way to get the same effect:

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
 
Old 03-03-2006, 10:37 AM   #9
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Original Poster
Rep: Reputation: 31
Thanks Flesym however compiling your code is a complete mess...! it couldn't find any crypto library!!!!

I just did

urpmi libcryptopp5

then I checked and /usr/include/crypt.h and /usr/lib contained

libcryptopp.so.5 libcryptopp.so.5.0.2 libcrypto.so.0.9.7 libcrypt.so

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!!!!

hell how am I supposed to do it????

thanks.. :-)
 
Old 03-03-2006, 10:39 AM   #10
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by aluser
There's a lazy way to get the same effect:

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...
 
Old 03-03-2006, 03:42 PM   #11
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Quote:
Originally Posted by rinonapo
Thanks Flesym however compiling your code is a complete mess...! it couldn't find any crypto library!!!!

I just did

urpmi libcryptopp5

then I checked and /usr/include/crypt.h and /usr/lib contained

libcryptopp.so.5 libcryptopp.so.5.0.2 libcrypto.so.0.9.7 libcrypt.so

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:
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
Note: See the difference between 'l' (lower case 'L'), which links to a library an 'I' (upper case i) that adds include-paths.

Hope that helps....

Last edited by Flesym; 03-03-2006 at 05:14 PM.
 
Old 03-04-2006, 02:59 AM   #12
rino.caldelli
Member
 
Registered: Apr 2005
Location: perugia
Distribution: ubuntu
Posts: 181

Original Poster
Rep: Reputation: 31
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.
 
Old 08-07-2011, 08:47 AM   #13
monazEssam
LQ Newbie
 
Registered: Aug 2011
Posts: 22

Rep: Reputation: Disabled
Urgently

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)

Error 2 error LNK2001: unresolved external symbol "public: virtual void __thiscall CryptoPP::Blowfish::Base::UncheckedSetKey(unsigned char const *,unsigned int,class CryptoPP::NameValuePairs const &)" (?UncheckedSetKey@Base@Blowfish@CryptoPP@@UAEXPBEIABVNameValuePairs@3@@Z)

Error 3 error LNK2001: unresolved external symbol "public: virtual void __thiscall CryptoPP::Blowfish::Base::ProcessAndXorBlock(unsigned char const *,unsigned char const *,unsigned char *)const " (?ProcessAndXorBlock@Base@Blowfish@CryptoPP@@UBEXPBE0PAE@Z)

Error 4 fatal error LNK1120: 3 unresolved externals

I have included the crypto++ directory which include the header and sources files in the linker library directories and additional include directories

Please tell me what should i do to make your program work in my environment as it's exactly what i want ?
thanks in advance
 
Old 08-07-2011, 09:07 AM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
@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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
encryption dr_zayus69 Linux - Software 2 11-28-2004 11:03 PM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
Mandrake 9.0 Wireless Works without encryption.. does not with encryption topcat Linux - Wireless Networking 3 05-04-2003 08:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration