Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ecb.c 1.8 KiB

9 år sedan
9 år sedan
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <openssl/evp.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. uint32_t ecb_decrypt( const unsigned char* i_stream,
  5. const uint32_t i_stream_size,
  6. const unsigned char* i_pass,
  7. const uint32_t i_pass_size,
  8. unsigned char* o_buff)
  9. {
  10. int ret, ret_fin;
  11. ret=ret_fin=0;
  12. EVP_CIPHER_CTX ctx;
  13. EVP_DecryptInit(&ctx, EVP_aes_128_ecb(), NULL, NULL);
  14. EVP_CIPHER_CTX_set_key_length(&ctx, i_pass_size);
  15. EVP_DecryptInit (&ctx, NULL, i_pass, NULL);
  16. EVP_DecryptUpdate(&ctx, o_buff, &ret, i_stream, i_stream_size);
  17. EVP_DecryptFinal (&ctx, o_buff+ret, &ret_fin);
  18. return ret+ret_fin;
  19. }
  20. uint32_t ecb_encrypt( const unsigned char* i_stream,
  21. const uint32_t i_stream_size,
  22. const unsigned char* i_pass,
  23. const uint32_t i_pass_size,
  24. unsigned char* o_buff)
  25. {
  26. int ret, ret_fin;
  27. ret=ret_fin=0;
  28. EVP_CIPHER_CTX ctx;
  29. EVP_DecryptInit(&ctx, EVP_aes_128_ecb(), NULL, NULL);
  30. EVP_CIPHER_CTX_set_key_length(&ctx, i_pass_size);
  31. EVP_EncryptInit (&ctx, NULL, i_pass, NULL);
  32. EVP_EncryptUpdate(&ctx, o_buff, &ret, i_stream, i_stream_size);
  33. EVP_EncryptFinal (&ctx, o_buff+ret, &ret_fin);
  34. return ret+ret_fin;
  35. }
  36. // checks if there are identical blocks in the i_textline
  37. int same_blocks(const char* i_textline, const int i_bs, int* first_block, int* second_block)
  38. {
  39. int chunk_nb = strlen(i_textline)/i_bs;
  40. for(int i=0; i<chunk_nb-1; i++)
  41. {
  42. for(int j=i+1; j<chunk_nb; j++)
  43. {
  44. if(memcmp(&i_textline[i*i_bs], &i_textline[j*i_bs],i_bs)==0)
  45. {
  46. *first_block = i+1;
  47. *second_block = j+1;
  48. return 1;
  49. }
  50. }
  51. }
  52. return 0;
  53. }