Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <openssl/conf.h>
  2. #include <openssl/evp.h>
  3. #include <openssl/err.h>
  4.  
  5.  
  6. #include <string.h>
  7.  
  8. int main(int arc, char *argv[])
  9. {
  10. /* Set up the key and iv. Do I need to say to not hard code these in a
  11. * real application? :-)
  12. */
  13.  
  14. /* A 256 bit key */
  15. unsigned char *key = "01234567890123456789012345678901";
  16.  
  17. /* A 128 bit IV */
  18. unsigned char *iv = "01234567890123456";
  19.  
  20. /* Message to be encrypted */
  21. unsigned char *plaintext =
  22. "The quick brown fox jumps over the lazy dog";
  23.  
  24. /* Buffer for ciphertext. Ensure the buffer is long enough for the
  25. * ciphertext which may be longer than the plaintext, dependant on the
  26. * algorithm and mode
  27. */
  28. unsigned char ciphertext[128];
  29.  
  30. /* Buffer for the decrypted text */
  31. unsigned char decryptedtext[128];
  32.  
  33. int decryptedtext_len, ciphertext_len;
  34.  
  35. /* Initialise the library */
  36. ERR_load_crypto_strings();
  37. OpenSSL_add_all_algorithms();
  38. OPENSSL_config(NULL);
  39.  
  40. /* Encrypt the plaintext */
  41. ciphertext_len = encrypt(plaintext, strlen(plaintext), key, iv,ciphertext);
  42.  
  43. /* Do something useful with the ciphertext here */
  44. printf("Ciphertext is:n");
  45. BIO_dump_fp(stdout, ciphertext, ciphertext_len);
  46.  
  47. /* Decrypt the ciphertext */
  48. decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,decryptedtext);
  49.  
  50. /* Add a NULL terminator. We are expecting printable text */
  51. decryptedtext[decryptedtext_len] = '';
  52.  
  53. /* Show the decrypted text */
  54. printf("Decrypted text is:n");
  55. printf("%sn", decryptedtext);
  56.  
  57. /* Clean up */
  58. EVP_cleanup();
  59. ERR_free_strings();
  60.  
  61. return 0;
  62. }
  63.  
  64. ||=== Build: Release in CryptoProject (compiler: GNU GCC Compiler) ===|
  65. obj/Release/main.o||In function `main':|
  66. main.c:(.text.startup+0x46)||undefined reference to `encrypt'|
  67. main.c:(.text.startup+0x81)||undefined reference to `decrypt'|
  68. ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement