Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include <openssl/ssl.h>
  2. #include <openssl/err.h>
  3. #include <string.h>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. void handleOpenSSLErrors(void)
  8. {
  9. ERR_print_errors_fp(stderr);
  10. abort();
  11. }
  12.  
  13. string decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
  14. unsigned char *iv ) {
  15.  
  16. EVP_CIPHER_CTX *ctx;
  17. unsigned char *plaintexts;
  18. int len;
  19. int plaintext_len;
  20. unsigned char* plaintext = new unsigned char[ciphertext_len];
  21. bzero(plaintext,ciphertext_len);
  22.  
  23. /* Create and initialise the context */
  24. if(!(ctx = EVP_CIPHER_CTX_new())) handleOpenSSLErrors();
  25.  
  26. /* Initialise the decryption operation. IMPORTANT - ensure you use a key
  27. * and IV size appropriate for your cipher
  28. * In this example we are using 256 bit AES (i.e. a 256 bit key). The
  29. * IV size for *most* modes is the same as the block size. For AES this
  30. * is 128 bits */
  31. if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
  32. handleOpenSSLErrors();
  33.  
  34. EVP_CIPHER_CTX_set_key_length(ctx, EVP_MAX_KEY_LENGTH);
  35.  
  36. /* Provide the message to be decrypted, and obtain the plaintext output.
  37. * EVP_DecryptUpdate can be called multiple times if necessary
  38. */
  39. if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
  40. handleOpenSSLErrors();
  41.  
  42. plaintext_len = len;
  43.  
  44. /* Finalise the decryption. Further plaintext bytes may be written at
  45. * this stage.
  46. */
  47. if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleOpenSSLErrors();
  48. plaintext_len += len;
  49.  
  50.  
  51. /* Add the null terminator */
  52. plaintext[plaintext_len] = 0;
  53.  
  54. /* Clean up */
  55. EVP_CIPHER_CTX_free(ctx);
  56. string ret = (char*)plaintext;
  57. delete [] plaintext;
  58. return ret;
  59. }
  60.  
  61. void initAES(const string& pass, unsigned char* salt, unsigned char* key, unsigned char* iv )
  62. {
  63. bzero(key,sizeof(key));
  64. bzero(iv,sizeof(iv));
  65.  
  66. EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt, (unsigned char*)pass.c_str(), pass.length(), 1, key, iv);
  67. }
  68.  
  69. size_t calcDecodeLength(char* b64input) {
  70. size_t len = strlen(b64input), padding = 0;
  71.  
  72. if (b64input[len-1] == '=' && b64input[len-2] == '=') //last two chars are =
  73. padding = 2;
  74. else if (b64input[len-1] == '=') //last char is =
  75. padding = 1;
  76. return (len*3)/4 - padding;
  77. }
  78.  
  79. void Base64Decode( char* b64message, unsigned char** buffer, size_t* length) {
  80. BIO *bio, *b64;
  81.  
  82. int decodeLen = calcDecodeLength(b64message);
  83. *buffer = (unsigned char*)malloc(decodeLen + 1);
  84. (*buffer)[decodeLen] = '\0';
  85.  
  86. bio = BIO_new_mem_buf(b64message, -1);
  87. b64 = BIO_new(BIO_f_base64());
  88. bio = BIO_push(b64, bio);
  89.  
  90. //BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer
  91. *length = BIO_read(bio, *buffer, strlen(b64message));
  92. BIO_free_all(bio);
  93. }
  94.  
  95. int main (void)
  96. {
  97. // This is the string Hello, World! encrypted using aes-256-cbc with the
  98. // pasword 12345
  99. char* ciphertext_base64 = (char*) "U2FsdGVkX1/E/yWBwY9nW96pYIv2nouyJIFF9BtVaKA=\n";
  100. int decryptedtext_len, ciphertext_len;
  101. size_t cipher_len;
  102. unsigned char* ciphertext;
  103. unsigned char salt[8];
  104. ERR_load_crypto_strings();
  105. Base64Decode(ciphertext_base64, &ciphertext, &cipher_len);
  106.  
  107. unsigned char key[32];
  108. unsigned char iv[32];
  109.  
  110. if (strncmp((const char*)ciphertext,"Salted__",8) == 0) {
  111. memcpy(salt,&ciphertext[8],8);
  112. ciphertext += 16;
  113. cipher_len -= 16;
  114. }
  115. initAES("12345", salt, key, iv);
  116.  
  117. string result = decrypt(ciphertext, cipher_len, key, iv);
  118. cout << result << endl;
  119.  
  120. // Clean up
  121. EVP_cleanup();
  122. ERR_free_strings();
  123.  
  124. return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement