Guest

cromestant

By: a guest on Apr 2nd, 2008  |  syntax: C  |  size: 4.99 KB  |  hits: 240  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. //WORKING
  2.  
  3. #include <openssl/evp.h>
  4. #include <openssl/blowfish.h>
  5. #include <openssl/crypto.h>
  6. #include <openssl/bn.h>
  7. #include <openssl/pem.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <arpa/inet.h>
  11.  
  12. int main(){
  13.         /*char * plaintext = "This is the plaintext to encrypt";
  14.         EVP_CIPHER_CTX *ctx;
  15.         int err;
  16.         const EVP_CIPHER *c;
  17.         c=EVP_get_cipherbyname("RC4");
  18.         if (!c){printf("No Cipher");exit(0);}
  19.         EVP_CIPHER_CTX_init(ctx);
  20. //      err= EVP_EncryptInit_ex(ctx,
  21. */
  22. char *filein ="original.txt";
  23. char *fileout ="test.bin";
  24. do_crypt(fileout,filein);
  25. }
  26. int do_crypt(char *outfile,char *filein)
  27.        {
  28.         int outlen, tmplen;
  29.         long inlen;
  30.         FILE *out,*in;
  31.         unsigned char *intext,*cripted;
  32.         //bad key, get better one
  33.         unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  34.        //bad IV , get better one
  35.         unsigned char iv[] = {1,2,3,4,5,6,7,8};
  36.         in = fopen(filein,"rb");
  37.         fseek(in,0,SEEK_END);
  38.         inlen=ftell(in);
  39.         rewind(in);
  40.         //get whole size, then return to begining.
  41.         intext = (unsigned char*)malloc(sizeof(char *)*inlen);
  42.         cripted = (unsigned char *)malloc(sizeof(char*)*inlen+8);
  43.         //size of file + block size (Blowfish buffer is 64bits->8bytes
  44.         fread(intext,1,inlen,in);
  45.         //read the whole file to buffer.
  46.         EVP_CIPHER_CTX ctx;
  47.         EVP_CIPHER_CTX_init(&ctx);
  48.         EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv);
  49.         if(!EVP_EncryptUpdate(&ctx, cripted, &outlen, intext, inlen))
  50.                 {
  51.                 /* Error */
  52.                 return 0;
  53.                 }
  54.         /* Buffer passed to EVP_EncryptFinal() must be after data just
  55.  *          * encrypted to avoid overwriting it.
  56.  *                   */
  57.         if(!EVP_EncryptFinal_ex(&ctx, cripted + outlen, &tmplen))
  58.                 {
  59.                 /* Error */
  60.                 return 0;
  61.                 }
  62.         outlen += tmplen;
  63.         EVP_CIPHER_CTX_cleanup(&ctx);
  64.         /* Need binary mode for fopen because encrypted data is
  65.  *          * binary data. Also cannot use strlen() on it because
  66.  *                   * it wont be null terminated and may contain embedded
  67.  *                            * nulls.
  68.  *                                     */
  69.         out = fopen(outfile, "wb");
  70.         fwrite(cripted, 1, outlen, out);
  71.         //char outp[outlen+1];
  72.         //&outp=outbuf;
  73.         //outp[outlen+1='\0';
  74.         //printf(outp);
  75.         fclose(out);
  76.         fclose(in);
  77.         return 1;
  78.         }
  79.  
  80.  
  81. //DECRYPT
  82.  
  83. #include <openssl/evp.h>
  84. #include <openssl/blowfish.h>
  85. #include <openssl/crypto.h>
  86. #include <openssl/bn.h>
  87. #include <openssl/pem.h>
  88. #include <stdio.h>
  89. #include <stdlib.h>
  90. #include <arpa/inet.h>
  91.  
  92. int main(){
  93.         /*char * plaintext = "This is the plaintext to encrypt";
  94.         EVP_CIPHER_CTX *ctx;
  95.         int err;
  96.         const EVP_CIPHER *c;
  97.         c=EVP_get_cipherbyname("RC4");
  98.         if (!c){printf("No Cipher");exit(0);}
  99.         EVP_CIPHER_CTX_init(ctx);
  100. //      err= EVP_EncryptInit_ex(ctx,
  101. */
  102. char *filein ="test.bin";
  103. do_decrypt(filein);
  104. }
  105. int do_decrypt(char *filein)
  106.         {
  107.         unsigned char *cripted;
  108.         unsigned char * plain;
  109.         int criptedlen, tmplen,plainlen,errcode;
  110.        /* Bogus key and IV: we'd normally set these from
  111.  *          * another source.
  112.  *                   */
  113.         long fileSize;
  114.         unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  115.         unsigned char iv[] = {1,2,3,4,5,6,7,8};
  116.  
  117.         FILE *in,*out;
  118.         //open file for read
  119.         in = fopen(filein,"rb");
  120.         //see how long it is
  121.         fseek (in , 0 , SEEK_END);
  122.         fileSize =ftell(in);
  123.         rewind(in);
  124.         //allocate memory for file
  125.         cripted = (char *) malloc(sizeof(char *)*fileSize);
  126.         //decript buffer should be filesize + block size length
  127.         //blowfish block size is 8bytes (64bits)
  128.         plain = (char*) malloc(sizeof(char *)*fileSize + 8);
  129.         fread(cripted,fileSize,1,in);
  130.         //all file is loaded into cripted*)
  131.        
  132.         EVP_CIPHER_CTX ctx;
  133.         //FILE *out;
  134.         EVP_CIPHER_CTX_init(&ctx);
  135.         EVP_DecryptInit_ex(&ctx, EVP_bf_cbc(), NULL, key, iv);
  136.         if(!EVP_DecryptUpdate(&ctx, plain , &plainlen,cripted,fileSize))
  137.               {
  138.                 /* Error */
  139.                        printf("error 1");
  140.               return 0;
  141.                }
  142.         /* Buffer passed to EVP_EncryptFinal() must be after data just
  143.  *          * encrypted to avoid overwriting it.
  144.  *                   */
  145.  
  146.       if(!(errcode=EVP_DecryptFinal_ex(&ctx, plain + plainlen, &tmplen)))
  147.             {
  148.                 /* Error */
  149.                     printf("error 2, code %x %s",errcode,plain);
  150.              return 0;
  151.                }
  152.         plainlen += tmplen;
  153.         EVP_CIPHER_CTX_cleanup(&ctx);
  154.         /* Need binary mode for fopen because encrypted data is
  155.  *          * binary data. Also cannot use strlen() on it because
  156.  *                   * it wont be null terminated and may contain embedded
  157.  *                            * nulls.
  158.  *                                     */
  159.        out = fopen("decripted.bin", "wb");
  160.       // printf("decrypted :%x",plain);
  161.        fwrite(plain, 1, plainlen, out);
  162.         fclose(in);
  163.         fclose(out);
  164.        
  165.         return 1;
  166.         }