EG Information

Main Index
EG Manual
Disclaimer
Legal Information
Hall of Fame
Hall of Shame
Member Rankings
Members List
Meet the Staff

Training Missions

Read Me First New
Basic Skills
Realistic Scenarios
Cryptography
Software Cracking
Linux ELF Binary Cracking
Logical Thinking
Programming
Patching
Steganography
Deface This Wall
/dev/null
/dev/urandom

Knowledge Bank

Discussion Forums
Enigma Chat New
RSS Feeds RSS
Articles / Tutorials
Videos
Online EG MP3 Player Radio
Enigma Zine
Downloads
Tools New

Code Resources

Submit Code
Ajax
ASM
Bash
C
CPP
Csharp
Delphi
Haskell
Java
Javascript
Jython
Lisp
mIRC
MySQL
Perl
PHP
Python
QBASIC
VisualBasic

Hakipedia: An open collaborative for all your information security needs.

The Urinal

Click Here To Vote For EG!

Has Enigma Group Helped You? Then Help Us By Advertising For Us. Place One Of The Following Images On Your Site.

enigma group

enigma group

enigma group

enigma group

Enigma Group's Code Bank


Example of using OpenSSL CryptoFunctions

By: BinaryShinigami  -  Date Submitted: 2009-05-31 18:38:56

  1. //MUST LINK WITH LibCrypto (-lcrypto) Tested under Linux with G++ make sure you have openssl installed
  2.  
  3. /* This program is an example of using openssl's encryption functions to encrypt a file using blowfish and decrypt it
  4. This was created by BinaryShinigami as an example. Hope you enjoy it :)
  5. P.S. I know I'm not an expert at ssl and encryption so some things may not be perfect but FUCK YOU! This is simply to help those who have never used
  6. openssl's crypto functions b/c The documentation is poor at explaining shit and I couldn't find all the much, I did find one tutorial and I followed it through
  7. so the code may look similar but I have changed some stuff as the code that was posted was giving me problems.
  8. */
  9. #include <iostream>
  10. #include <fstream>
  11. #include <openssl/ssl.h> //This is the include under linux for openssl, haven't tested it on windows so you may have to look this up for your system :p
  12. #include <string>
  13. #include <cstring>
  14.  
  15. using namespace std;
  16.  
  17. //These functions will be used to encrypt some data or decrypt some data using openssl's blowfish algorithm.
  18. int myBlowfishEncrypt(char *inBuff, char *outBuff,char *pass,char *iv,int *outLen, int inLen);
  19. int myBlowfishDecrypt(char *inBuff, char *outBuff, char *pass, char *iv, int *outLen, int inLen, int maxSize);
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23. if (argc < 6)
  24. {
  25. cout <<"Usage: " <<argv[0] <<" -e|-d (encrypt or decrypt respectively) <inputFile> <outputFile> <iv_file> <passwordFile>" <<endl;
  26. cout <<"t -e : Encrypt a file in inputFile and put encrypted contents in outputFile" <<endl;
  27. cout <<"t -d : Decrypt a file in inputFile and put decrypted contents in outputFile" <<endl;
  28. cout <<"t <inputFile> : The filename of the file to be encrypted/decrypted" <<endl;
  29. cout <<"t <outputFile> : The filename to store the encrypted/decrypted contents" <<endl;
  30. cout <<"t <ivFile> : The filename of the file that contains the data for the initialization vector, should be 56 bytes" <<endl;
  31. cout <<"t <passwordFile> : The filename of the file that contains the encryption key must be no bigger than 512 bytes, if not a multiple of 8 then it will be padded to be one" <<endl;
  32. return 0;
  33. }
  34.  
  35.  
  36. char blowfish_iv[57]; //This will be the IV or initialization vector, its 56 + 1 since We will use a iv of size 56 just cuz i can :p
  37. char plaintext[40]; //This is the plaintext or data that has been read, the program will be reading in chunks of 40 until it hits the end
  38. char password[512]; //This will be the password we will use, I have decided to make it a max of 512 chars for mere simplicity but so far I found that as long ass the pass is a power of 8 it works
  39. char cipherText[48]; //This will hold the outputed data, it is 48 bytes b/c blowfish outputted 48 chars for every chunk of 40 i tried :) it's supposed to be inputlength+1 block size so I guess it makes the block size 8 bytes
  40. int outputLength = 0; //This will be used to hold the output length for the ciphertext or plaintext from our encryption/decryption functions so we know how many bytes to write to the new file
  41.  
  42. int BytesRead = 0; //This will tell us how many bytes was read from the last read operation
  43. int bytesToRead = 40; //We use this to keep track of how many bytes we need to read, e.g. for encryption we read 40 byte chunks, for decryption we read 48 byte chunks
  44.  
  45.  
  46. ofstream outFile; //Self explanatory :p
  47. ifstream inFile; //Same as above
  48.  
  49. //Here we open the ivFile and load the contents and only read 56 bytes if not 56 bytes then we error out b/c I say so
  50. //NOTE I believe we only need a IV of 40 but I choose a IV of 56 just to be safe :p and The IV does matter in encryption/decryption, if they are not the same then decryption will not work I've tried it
  51. inFile.open(argv[4]);
  52. if (!inFile)
  53. {
  54. cerr <<"Unable to open IV File! Try again" <<endl;
  55. return 0;
  56. }
  57. inFile.read(blowfish_iv,56);
  58. if (inFile.gcount() < 56)
  59. {
  60. cerr <<"IV < 56 bytes ! Problem Encountered!" <<endl;
  61. return 0;
  62. }
  63. inFile.close();
  64.  
  65.  
  66. //strcpy(blowfish_iv,"12345678901234567890123456789012345678901234567890123456"); //Fill the
  67.  
  68. //Now its time to load the password key If the key is not a multiple of 8 it will pad it with 0's which is bad so you should make sure it is
  69. inFile.open(argv[5]);
  70. if (!inFile)
  71. {
  72. cerr <<"Can't Open Password File!" <<endl;
  73. return 0;
  74. }
  75.  
  76. inFile.read(password,512);
  77. BytesRead = inFile.gcount();
  78. if ((BytesRead%8) > 0)
  79. {
  80. int BytesToPad = 8-(BytesRead%8);
  81. for (int i = 0; i< BytesToPad; i++)
  82. sprintf(password,"%s%c",password,'0');
  83. }
  84. inFile.close();
  85.  
  86. //strcpy(password,"12345678901234567890123456789012345678901234567890123456");
  87.  
  88. //END OF loading iv and password
  89.  
  90.  
  91. //Here we check if the file mode is encryption and if so we encrypt the file
  92. if (strcmp(argv[1],"-e") == 0)
  93. {
  94.  
  95. //BEGIN FILE ENCRYPTION CODE
  96. inFile.open(argv[2]);
  97. outFile.open(argv[3]);
  98.  
  99. while(! inFile.eof())
  100. {
  101. inFile.read(plaintext,bytesToRead);
  102. BytesRead = inFile.gcount();
  103.  
  104. myBlowfishEncrypt(plaintext,cipherText,password,blowfish_iv,&outputLength,BytesRead);
  105.  
  106. outFile.write(cipherText,outputLength);
  107. outFile.flush();
  108.  
  109.  
  110. }
  111.  
  112. inFile.close();
  113. outFile.close();
  114.  
  115. cout <<"File Encrypted!" <<endl;
  116. //END ENCRYPTION CODE
  117. }
  118. //Now we check if mode is decryption instead
  119. else if (strcmp(argv[1],"-d") == 0)
  120. {
  121. //BEGIN FILE DECRYPTION CODE
  122.  
  123. inFile.open(argv[2]);
  124. outFile.open(argv[3]);
  125.  
  126. while (!inFile.eof())
  127. {
  128. bytesToRead = 48; //We do this b/c for some reason 48 kept getting changed to 0 :(
  129.  
  130. inFile.read(cipherText,bytesToRead);
  131. BytesRead = inFile.gcount();
  132. cout <<"Read " <<BytesRead <<" bytes" <<endl;
  133.  
  134. myBlowfishDecrypt(cipherText,plaintext,password,blowfish_iv,&outputLength,BytesRead,bytesToRead);
  135. outFile.write(plaintext,outputLength);
  136. outFile.flush();
  137. cout <<"Wrote " <<outputLength <<" bytes" <<endl;
  138.  
  139. }
  140.  
  141. cout <<"File Decrypted!" <<endl;
  142. inFile.close();
  143. outFile.close();
  144. cout <<"Files Closed" <<endl;
  145.  
  146. //END DECRYPTION CODE
  147. }
  148.  
  149.  
  150. return 0;
  151. }
  152. //For a full explanation of what each parameter to the EVP functions is for check out the decrypt function as it has the same variables and same meanings to both functions.
  153. int myBlowfishEncrypt(char *inBuff,char *outBuff,char *pass,char *iv, int *outLen, int inLen)
  154. {
  155. int finalOut = 0;
  156. EVP_CIPHER_CTX cipherHandle; //The handle to the current setup kinda like a cURL handle
  157.  
  158. EVP_CIPHER_CTX_init(&cipherHandle); //Like cURL_Init in cURL, it has to be the first function called
  159. EVP_EncryptInit(&cipherHandle,EVP_bf_cbc(),(const unsigned char*)pass, (const unsigned char*) iv); //Setup our handle to use blowfish
  160.  
  161. memset(outBuff,0,inLen); //Wipe any garbage memory in the output buffer
  162.  
  163. //Actually encrypt the data
  164. if ( EVP_EncryptUpdate(&cipherHandle, (unsigned char*)outBuff, outLen, (const unsigned char*)inBuff, inLen) != 1)
  165. {
  166. cout <<"error Encrypting data" <<endl;
  167. cout <<"Update" <<endl;
  168. return -1;
  169. }
  170. //If needed pad the data and encrypt the left over data
  171. if ( EVP_EncryptFinal(&cipherHandle,(unsigned char*) (outBuff + *outLen), &finalOut) != 1) {
  172. cout <<"Error Encrypting Data" <<endl;
  173. return -1;
  174. }
  175. *outLen += finalOut;
  176.  
  177.  
  178. EVP_CIPHER_CTX_cleanup(&cipherHandle);
  179. return 0;
  180.  
  181. }
  182.  
  183. //Maxsize is the size of the chunk of data read(48) if it wasn't padded with data ;)
  184.  
  185. int myBlowfishDecrypt(char *inBuff, char *outBuff, char *pass, char *iv, int *outLen, int inLen, int maxSize)
  186. {
  187. int finalOut = 0;
  188.  
  189. EVP_CIPHER_CTX cipherHandle;
  190. int charLen = strlen(inBuff);
  191.  
  192. EVP_CIPHER_CTX_init(&cipherHandle);
  193. EVP_DecryptInit(&cipherHandle,EVP_bf_cbc(),(const unsigned char*)pass, (const unsigned char*) iv);
  194.  
  195. memset(outBuff,0,charLen);
  196.  
  197.  
  198. //ALL THE ABOVE IS THE SAME AS BEFORE IN ENCRYPT
  199.  
  200. //Here we decrypt the data blocks
  201. //The first arg is the ctx handle, the 2nd is the output buffer, the 3rd is the address of a int to store the size of the outputed data, the 3rd is the input buffer, the 4th is teh size of the input buffer, success == 1
  202. if ( EVP_DecryptUpdate(&cipherHandle, (unsigned char*)outBuff, outLen, (const unsigned char*)inBuff, inLen) != 1)
  203. {
  204. cout <<"error decrypting data" <<endl;
  205. cout <<"Udate" <<endl;
  206. return -1;
  207. }
  208.  
  209. //This code here handles files that were padded
  210. //it would be padded if the file length was not a multiple of 8 so the block count would be different,
  211. //we read in 48 as max so if it is less then 48 we have some short blocks may not always work
  212. //but worst case scenario we could run this function everytime and not check for errors
  213. if (inLen < maxSize)
  214. {
  215. //This function acutally deals with the padding
  216. //1st arg is handle, 2nd is location to store the data (we used the regular outputBuffer just used some pointer arithmetic to move to the end of the last decrypt so we don't need to concatenate the values
  217. //3rd is the address of the place to store the new outputlenght
  218. if ( EVP_DecryptFinal(&cipherHandle,(unsigned char*)(outBuff + *outLen),&finalOut) != 1) {
  219. cout <<"Unable to decryptFinal data" <<endl;
  220. return -1;
  221. }
  222. //Add the new outputlength from Final to that from Update since they both form the same varible we need the actual size of the variable
  223. *outLen += finalOut;
  224. }
  225.  
  226. //Cleanup
  227.  
  228. EVP_CIPHER_CTX_cleanup(&cipherHandle);
  229. return 0;
  230.  
  231.  
  232. }
  233.  
Return to cpp category list

Who's Online

487 Guests, 100 Users
ckryptix, TheRetech, Diznablo, Nicid1, Ios, viper0i0, rabbidmind, Nasrudin, CollapsingWalls, mehtaparag, bitstrike, jnony, C, Nusquam-Redono-Sapientia, bazcrown, saifulfaizan, The End, Ultraminor, psychomarine, st3alth, themastersinner, pgmrlink, login, lionaneesh, ishkur88, mahraja, Mac, chekifr, gandalf88, Vap0r, t0ast, tantrum6226, BnE, Distorted, Psiber_Syn, Ausome1, invas10n, oldgoat, freedaysbecumei, BinaryShinigami, Rex_Mundi, Red_beard, Strobeflux, s0m3nak3dguy, Descent, teehee, machupicchu, Genetix, Anandarl, NotMyOwn, thegamerdude, Godzila, popo12341234, RedEvolution, velocity_b, myne17, teto111, aVoid, Central-Gsm, 1101, JackalReborn, InjectioN, h4lted, c0re, DisPater, markt4death, splatta, Jackowacko, saint556, Pyron2312, Azerion, howsens, white.hat.gone.bad, vazzilly, pwunkz, Inverted, QuarterCask, Infernoe11, deskata, cr4ck3rj4ck, Blizer, jasonmax, j0sh, gwenwavor, N4g4c3N, spizeyboy, Network X, Uino59, Jae Cee, ianFDK, saykov, medhaavee, zofy, demonkiller410, Stumble, SaMTHG, kishore, Raze, helasraizam, Venom1019