Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <openssl/blowfish.h>
- #include <stdlib.h>
- #include <cstdio>
- #include <string.h>
- #include <iostream>
- #include <unistd.h>
- using namespace std;
- const char *enckey = "0000000000000000";
- unsigned char *vin = (unsigned char *)"00000000";
- //
- // encrypt a string
- // NOTE: you must free this string afterwards
- //
- unsigned char *encrypt(const unsigned char *inStr, int len)
- {
- // blowfish key
- BF_KEY bfKey;
- BF_set_key(&bfKey, strlen(enckey), (const unsigned char*)enckey);
- // encrypt
- unsigned char *outStr = (unsigned char *)malloc(sizeof(unsigned char) * len);
- BF_cbc_encrypt((const unsigned char *)inStr, outStr, len, &bfKey, vin, BF_ENCRYPT);
- return outStr;
- }
- //
- // decrypt a string
- // NOTE: you must free this string afterwards
- //
- unsigned char *decrypt(const unsigned char *inStr, int len)
- {
- // blowfish key
- BF_KEY bfKey;
- BF_set_key(&bfKey, strlen(enckey), (const unsigned char*)enckey);
- // decrypt
- unsigned char *buf = (unsigned char *)malloc(sizeof(unsigned char) * len);
- BF_cbc_encrypt((const unsigned char*)inStr, buf, len, &bfKey, vin, BF_DECRYPT);
- return buf;
- }
- //
- // MAIN
- //
- int main(int argc, char **argv)
- {
- int c;
- char *encrypt_str = NULL;
- while ((c = getopt(argc, argv, "e:")) != -1)
- {
- switch (c)
- {
- case 'e':
- encrypt_str = optarg;
- break;
- default:
- cerr << "unknown argument: -" << c << " supplied\n";
- exit(1);
- }
- }
- if (encrypt_str != NULL) {
- int len = strlen(encrypt_str);
- unsigned char *encstr = encrypt((const unsigned char *)encrypt_str, len);
- unsigned char *decstr = decrypt((const unsigned char *)encstr, len);
- std::cout << "decrypted: " << decstr << "\n";
- free(encstr);
- free(decstr);
- } else {
- std::cout << "nothing to do\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement