//Initializes the encryption and decryption modules while performing error checks void encryption_decryption_init(char *key, int key_len) { crypt_fd = mcrypt_module_open("blowfish", NULL, "ofb", NULL); if(crypt_fd == MCRYPT_FAILED) { perror("Error opening module"); exit(EXIT_FAILURE); } if(mcrypt_generic_init(crypt_fd, key, key_len, NULL) < 0) { perror("Error while initializing encrypt"); exit(EXIT_FAILURE); } decrypt_fd = mcrypt_module_open("blowfish", NULL, "ofb", NULL); if(decrypt_fd == MCRYPT_FAILED) { perror("Error opening module"); exit(EXIT_FAILURE); } if(mcrypt_generic_init(decrypt_fd, key, key_len, NULL) < 0) { perror("Error while initializing decrypt"); exit(EXIT_FAILURE); } }
void setup_encryption() { td = mcrypt_module_open("twofish", NULL, "cfb", NULL); if (td == MCRYPT_FAILED) error_exit("mcrypt failed", errno); key_fd = open(enckeyf, O_RDONLY); if (key_fd <= -1) error_exit("unable to open key file", errno); enckey = malloc(key_len); if (enckey == NULL) error_exit("failed to allocate memory for key", errno); int iv_size = mcrypt_enc_get_iv_size(td); IV = malloc(iv_size); if (IV == NULL) error_exit("failed to allocate memory for IV", errno); memset(enckey, 0, key_len); int numread = read(key_fd, enckey, key_len); if (numread != key_len) error_exit("failed to read key", errno); int c = close(key_fd); if (c <= -1) error_exit("failed to close key file", errno); int i = 0; while (i < iv_size) { IV[i] = 0; i++; } if (mcrypt_generic_init(td, enckey, key_len, IV) <= -1) error_exit("failed to start encryption process", errno); free(enckey); free(IV); }
void dump_testcase(char *algo, char *mode) { MCRYPT td; int *key_sizes, *cur_size; /* globalize this for easier access below */ strcpy(testing_algo, algo); strcpy(testing_mode, mode); td = mcrypt_module_open(algo, NULL, mode, NULL); if (td == MCRYPT_FAILED) { /* assume that this algorithm just doesn't support this mode */ return; } cur_size = key_sizes = list_key_sizes(td); if (key_sizes == NULL) { return; } while (*cur_size != 0) { dump_testcase_keysize(td, *cur_size); cur_size++; } free(key_sizes); mcrypt_module_close(td); }
MCryptHandle::MCryptHandle(const std::string& algo, const std::string& mode): algo_( copyStr(algo) ), mode_( copyStr(mode) ), td_( mcrypt_module_open(algo_.get(), nullptr, mode_.get(), nullptr) ) { if(td_==MCRYPT_FAILED) throw std::runtime_error("mcrypt_module_open(): failed to open cypher"); }
int encrypt(char *password, char *plaintext, char *ciphertext) { MCRYPT td; int i; char *key; char block_buffer; char *IV; char *salt; td = mcrypt_module_open(algorithm, NULL, mode, NULL); if (td == MCRYPT_FAILED) { printf("failed to open module\n"); return 1; } salt = crypt_malloc(saltsize); crypt_random(salt, saltsize); // printf("salt:%s\n",salt); IV = crypt_malloc(ivsize); crypt_random(IV, ivsize); // printf("IV:%s\n",IV); putin_meg(ciphertext, salt, IV); key = crypt_malloc(keysize); data.hash_algorithm[0] = hash_algo; data.count = 0; data.salt = salt; data.salt_size = saltsize; mhash_keygen_ext(KEYGEN_MCRYPT, data, key, keysize, password, strlen(password)); printf("key:%s\n",key); i = mcrypt_generic_init(td, key, keysize, IV); if (i<0) { mcrypt_perror(i); return 1; } // printf("%d",strlen(plaintext)); // Here to encrypt in CFB performed in bytes for(i=36; i<strlen(plaintext); i++) { // printf("%c",plaintext[i]); block_buffer = plaintext[i]; mcrypt_generic (td, &block_buffer, 1); ciphertext[i] = block_buffer; // printf("%c",ciphertext[i]); } // Deinit the encryption thread, and unload the module mcrypt_generic_end(td); return 0; }
GRG_TMPFILE grg_tmpfile_gen (const GRG_CTX gctx) { GRG_TMPFILE tf; char tmpname[] = "/tmp/___-XXXXXX"; grg_crypt_algo ca; if (!gctx) return NULL; tf = (GRG_TMPFILE) malloc (sizeof (struct _grg_tmpfile)); if (!tf) return NULL; ca = grg_ctx_get_crypt_algo (gctx); memcpy (tmpname + 5, gctx->header, HEADER_LEN); tf->tmpfd = mkstemp (tmpname); unlink (tmpname); memcpy (tmpname, "/tmp/___-XXXXXX", 15); if (tf->tmpfd < 0) { free (tf); return NULL; } tf->crypt = mcrypt_module_open (grg2mcrypt (ca), NULL, MCRYPT_CFB, NULL); if (tf->crypt == MCRYPT_FAILED) { close (tf->tmpfd); free (tf); return NULL; } tf->dKey = grg_get_key_size_static (ca); tf->key = grg_rnd_seq (gctx, tf->dKey); if(!tf->key) { close (tf->tmpfd); free (tf); return NULL; } tf->dIV = grg_get_block_size_static (ca); tf->IV = grg_rnd_seq (gctx, tf->dIV); if(!tf->IV) { close (tf->tmpfd); free (tf); return NULL; } tf->rwmode = WRITEABLE; return tf; }
static int cached_cipher_init(struct cached_cipher * cipher, char * destype) { cipher->cipher = mcrypt_module_open("des", NULL, destype, NULL); if(!cipher->cipher) return -1; cipher->valid = 0; return 0; }
main() { MCRYPT td; int i; char *key; char password[20]; char block_buffer; char *IV; int keysize=19; /* 128 bits */ key=calloc(1, keysize); strcpy(password, "A_large_key"); /* Generate the key using the password */ /* mhash_keygen( KEYGEN_MCRYPT, MHASH_MD5, key, keysize, NULL, 0, password, strlen(password)); */ memmove( key, password, strlen(password)); td = mcrypt_module_open("twofish", NULL, "cfb", NULL); if (td==MCRYPT_FAILED) { return 1; } IV = malloc(mcrypt_enc_get_iv_size(td)); /* Put random data in IV. Note these are not real random data, * consider using /dev/random or /dev/urandom. */ /* srand(time(0)); */ for (i=0; i< mcrypt_enc_get_iv_size( td); i++) { IV[i]=rand(); } i=mcrypt_generic_init( td, key, keysize, IV); if (i<0) { mcrypt_perror(i); return 1; } /* Encryption in CFB is performed in bytes */ while ( fread (&block_buffer, 1, 1, stdin) == 1 ) { mcrypt_generic (td, &block_buffer, 1); /* Comment above and uncomment this to decrypt */ /* mdecrypt_generic (td, &block_buffer, 1); */ fwrite ( &block_buffer, 1, 1, stdout); } mcrypt_generic_deinit(td); mcrypt_module_close(td); return 0; }
EMOKIT_DECLSPEC int emokit_init_crypto(emokit_device* s) { emokit_get_crypto_key(s, 1); //libmcrypt initialization s->td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, NULL, MCRYPT_ECB, NULL); s->blocksize = mcrypt_enc_get_block_size(s->td); //should return a 16bits blocksize s->block_buffer = malloc(s->blocksize); mcrypt_generic_init(s->td, s->key, EMOKIT_KEYSIZE, NULL); return 0; }
int px_find_cipher(const char *name, PX_Cipher ** res) { char nbuf[PX_MAX_NAMELEN + 1]; const char *mode = NULL; char *p; MCRYPT ctx; PX_Cipher *c; strcpy(nbuf, name); if ((p = strrchr(nbuf, '-')) != NULL) { if (is_mode(p + 1)) { mode = p + 1; *p = 0; } } name = px_resolve_alias(aliases, nbuf); if (!mode) { mode = "cbc"; /* * if (mcrypt_module_is_block_algorithm(name, NULL)) mode = "cbc"; * else mode = "stream"; */ } mode = px_resolve_alias(mode_aliases, mode); ctx = mcrypt_module_open((char *) name, NULL, (char *) mode, NULL); if (ctx == (void *) MCRYPT_FAILED) return -1; c = palloc(sizeof *c); c->iv_size = cipher_iv_size; c->key_size = cipher_key_size; c->block_size = cipher_block_size; c->init = cipher_init; c->encrypt = cipher_encrypt; c->decrypt = cipher_decrypt; c->free = cipher_free; c->ptr = ctx; c->pstat = 0; *res = c; return 0; }
int Encrypt(char *plain) { int len = strlen(plain) + 8 - (strlen(plain) % 8); MCRYPT mc = mcrypt_module_open("blowfish", NULL, "ecb", NULL); if(mc == MCRYPT_FAILED) { printf("Failed\n"); } char *key = "6#26FRL$ZWD"; mcrypt_generic_init(mc, key, 11, ""); mcrypt_generic(mc, plain, len); return len; }
int encrypt_buffer(void* buf, int buf_len, char* key, int key_len) { MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL); int blocksize = mcrypt_enc_get_block_size(td); if(buf_len % blocksize != 0) { return -1; } mcrypt_generic_init(td, key, key_len, IV); mcrypt_generic(td, buf, buf_len); mcrypt_generic_deinit (td); mcrypt_module_close(td); return 0; }
Variant HHVM_FUNCTION(mcrypt_get_cipher_name, const String& cipher) { MCRYPT td = mcrypt_module_open((char*)cipher.data(), (char*)MCG(algorithms_dir).data(), (char*)"ecb", (char*)MCG(modes_dir).data()); if (td == MCRYPT_FAILED) { td = mcrypt_module_open((char*)cipher.data(), (char*)MCG(algorithms_dir).data(), (char*)"stream", (char*)MCG(modes_dir).data()); if (td == MCRYPT_FAILED) { MCRYPT_OPEN_MODULE_FAILED("mcrypt_get_cipher_name"); return false; } } char *cipher_name = mcrypt_enc_get_algorithms_name(td); mcrypt_module_close(td); String ret(cipher_name, CopyString); mcrypt_free(cipher_name); return ret; }
Variant f_mcrypt_get_cipher_name(CStrRef cipher) { MCRYPT td = mcrypt_module_open((char*)cipher.data(), (char*)MCG(algorithms_dir).data(), (char*)"ecb", (char*)MCG(modes_dir).data()); if (td == MCRYPT_FAILED) { td = mcrypt_module_open((char*)cipher.data(), (char*)MCG(algorithms_dir).data(), (char*)"stream", (char*)MCG(modes_dir).data()); if (td == MCRYPT_FAILED) { raise_warning(MCRYPT_OPEN_MODULE_FAILED); return false; } } char *cipher_name = mcrypt_enc_get_algorithms_name(td); mcrypt_module_close(td); String ret(cipher_name, CopyString); mcrypt_free(cipher_name); return ret; }
int epoc_init_crypto(epoc_device* s) { epoc_get_crypto_key(s->serial, ""); //libmcrypt initialization td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, NULL, MCRYPT_ECB, NULL); blocksize = mcrypt_enc_get_block_size(td); //should return a 16bits blocksize block_buffer = malloc(blocksize); mcrypt_generic_init( td, key, KEYSIZE, NULL); return 0; }
void Decrypt(const char *encrypted, unsigned char *decrypted) { int encLength = strlen(encrypted); HexToBinary(encrypted, decrypted); MCRYPT mc = mcrypt_module_open("blowfish", NULL, "ecb", NULL); if(mc == MCRYPT_FAILED) { printf("Failed\n"); } char *key = "R=U!LH$O2B#"; mcrypt_generic_init(mc, key, 11, ""); mdecrypt_generic(mc, decrypted, encLength/2); }
int64_t f_mcrypt_get_key_size(CStrRef cipher, CStrRef module) { MCRYPT td = mcrypt_module_open((char*)cipher.data(), (char*)MCG(algorithms_dir).data(), (char*)module.data(), (char*)MCG(modes_dir).data()); if (td == MCRYPT_FAILED) { raise_warning(MCRYPT_OPEN_MODULE_FAILED); return false; } int64_t ret = mcrypt_enc_get_key_size(td); mcrypt_module_close(td); return ret; }
Variant f_mcrypt_get_iv_size(const String& cipher, const String& mode) { MCRYPT td = mcrypt_module_open((char*)cipher.data(), (char*)MCG(algorithms_dir).data(), (char*)mode.data(), (char*)MCG(modes_dir).data()); if (td == MCRYPT_FAILED) { raise_warning(MCRYPT_OPEN_MODULE_FAILED); return false; } int64_t ret = mcrypt_enc_get_iv_size(td); mcrypt_module_close(td); return ret; }
static int htc_aes_crypt(FILE *in, FILE *out, char *key, char *iv, unsigned char chunks_in, htc_aes_progress_t callback, htc_aes_crypt_t crypt_func) { char buf[HTC_AES_READBUF], orig_iv[HTC_AES_KEYSIZE]; unsigned int pos, size, chunks, bytes, chunksdone = 0; unsigned int count = HTC_AES_READBUF_ROUNDS + 1; unsigned int chunk_size = (((int)chunks_in)<<HTC_AES_CHUNK_SIZE); MCRYPT td; /* Get size of zip data. */ pos = ftell(in); fseek(in, 0, SEEK_END); size = ftell(in) - pos; fseek(in, pos, SEEK_SET); chunks = get_num_chunks(size, chunk_size); td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, NULL, MCRYPT_CBC, NULL); if(td == MCRYPT_FAILED) { perror("failed to open mcrypt module"); return 0; } memcpy(orig_iv, iv, HTC_AES_KEYSIZE); while((bytes = fread(buf, sizeof(char), sizeof(buf), in)) > 0) { if(callback) callback(ftell(in), size); if(chunksdone < chunks) { if((ftell(in) - bytes - pos) % chunk_size == 0) { count = 0; memcpy(iv, orig_iv, HTC_AES_KEYSIZE); } if(count < HTC_AES_READBUF_ROUNDS) { crypt_func(td, buf, sizeof(buf), key, iv); count++; } else if(count == HTC_AES_READBUF_ROUNDS) { chunksdone++; count++; } } fwrite(buf, sizeof(char), sizeof(buf), out); } mcrypt_module_close(td); return 1; }
int encrypt(void* buffer, int buffer_len, /* Because the plaintext could include null bytes*/ char* IV, char* key, int key_len) { MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL); int blocksize = mcrypt_enc_get_block_size(td); if (buffer_len % blocksize != 0) { return 1; } mcrypt_generic_init(td, key, key_len, IV); mcrypt_generic(td, buffer, buffer_len); mcrypt_generic_deinit(td); mcrypt_module_close(td); return 0; }
Variant HHVM_FUNCTION(mcrypt_get_key_size, const String& cipher, const String& module) { MCRYPT td = mcrypt_module_open((char*)cipher.data(), (char*)MCG(algorithms_dir).data(), (char*)module.data(), (char*)MCG(modes_dir).data()); if (td == MCRYPT_FAILED) { MCRYPT_OPEN_MODULE_FAILED("mcrypt_get_key_size"); return false; } int64_t ret = mcrypt_enc_get_key_size(td); mcrypt_module_close(td); return ret; }
main() { MCRYPT td; int i; char *key; /* created using mcrypt_gen_key */ char *block_buffer; char *IV; int blocksize; int keysize = 24; /* 192 bits == 24 bytes */ key = calloc(1, keysize); strcpy(key, "A_large_and_random_key"); td = mcrypt_module_open("saferplus", NULL, "cbc", NULL); blocksize = mcrypt_enc_get_block_size(td); block_buffer = malloc(blocksize); /* but unfortunately this does not fill all the key so the rest bytes are * padded with zeros. Try to use large keys or convert them with mcrypt_gen_key(). */ IV=malloc(mcrypt_enc_get_iv_size(td)); /* Put random data in IV. Note these are not real random data, * consider using /dev/random or /dev/urandom. */ /* srand(time(0)); */ for (i=0; i < mcrypt_enc_get_iv_size(td); i++) { IV[i]=rand(); } mcrypt_generic_init ( td key, keysize, IV); /* Encryption in CBC is performed in blocks */ while ( fread (block_buffer, 1, blocksize, stdin) == blocksize ) { mcrypt_generic (td, block_buffer, blocksize); /* mdecrypt_generic (td, block_buffer, blocksize); */ fwrite ( block_buffer, 1, blocksize, stdout); } mcrypt_generic_end (td); return 0; }
void mcrypt_randomize( void* _buf, int buf_size, int type) { #ifdef HAVE_DEV_RANDOM unsigned char *buf = _buf; int _fd; if (type==0) _fd = fd0; else _fd = fd1; if (read( _fd, buf, buf_size)==-1) { err_quit(_("Error while reading random data\n")); } #else /* no /dev/random */ int level; static int pool_inited; static MCRYPT ed; if ( !pool_inited) { if (real_random_flag!=0) level = 2; else level = 1; gather_random( level); pool_inited = 1; /* Expansion step. * Pool data are expanded as: * pool is set as an arcfour key. The arcfour algorithm * is then used to encrypt the given data, * to generate a pseudorandom sequence. */ ed = mcrypt_module_open( "arcfour", algorithms_directory, "stream", modes_directory); if (ed==MCRYPT_FAILED) err_quit(_("Mcrypt failed to open module.\n")); if (mcrypt_generic_init( ed, rnd_pool, 20, NULL) < 0) { err_quit(_("Mcrypt failed to initialize cipher.\n")); } } mcrypt_generic( ed, _buf, buf_size); return; #endif }
Variant f_mcrypt_module_open(CStrRef algorithm, CStrRef algorithm_directory, CStrRef mode, CStrRef mode_directory) { MCRYPT td = mcrypt_module_open ((char*)algorithm.data(), (char*)(algorithm_directory.empty() ? MCG(algorithms_dir).data() : algorithm_directory.data()), (char*)mode.data(), (char*)(mode_directory.empty() ? (char*)MCG(modes_dir).data() : mode_directory.data())); if (td == MCRYPT_FAILED) { raise_warning("Could not open encryption module"); return false; } return Object(new MCrypt(td)); }
int epoc_init(enum headset_type type) { if(type == RESEARCH_HEADSET) memcpy(key, RESEARCHKEY, KEYSIZE); else if(type == CONSUMER_HEADSET) memcpy(key, CONSUMERKEY, KEYSIZE); else if(type == SPECIAL_HEADSET) memcpy(key, SPECIALKEY, KEYSIZE); //libmcrypt initialization td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, NULL, MCRYPT_ECB, NULL); blocksize = mcrypt_enc_get_block_size(td); //should return a 16bits blocksize block_buffer = malloc(blocksize); mcrypt_generic_init( td, key, KEYSIZE, NULL); return 0; }
Variant HHVM_FUNCTION(mcrypt_module_open, const String& algorithm, const String& algorithm_directory, const String& mode, const String& mode_directory) { MCRYPT td = mcrypt_module_open ((char*)algorithm.data(), (char*)(algorithm_directory.empty() ? MCG(algorithms_dir).data() : algorithm_directory.data()), (char*)mode.data(), (char*)(mode_directory.empty() ? (char*)MCG(modes_dir).data() : mode_directory.data())); if (td == MCRYPT_FAILED) { raise_warning("Could not open encryption module"); return false; } return Variant(req::make<MCrypt>(td)); }
void init_mcrypt() { // Open encryption module if((td = mcrypt_module_open("twofish", NULL, "cfb", NULL)) == MCRYPT_FAILED) { fprintf(stderr, "Error: could not open mcrypt library.\n"); exit(RC=EXIT_FAILURE); } // Open file containing key for reading if((key_fd = open("my.key", O_RDONLY)) == ERROR) { fprintf(stderr, "Error: could not open my.key.\n"); exit(RC=EXIT_FAILURE); } // Allocate space for key if((key = calloc(1, KEY_SIZE)) == NULL) { fprintf(stderr, "Error: could not calloc() for key.\n"); exit(RC=EXIT_FAILURE); } // Read in key if(read(key_fd, key, KEY_SIZE) != KEY_SIZE) { fprintf(stderr, "Error: could not read from my.key into key.\n"); exit(RC=EXIT_FAILURE); } // Close file if(close(key_fd) == ERROR) { fprintf(stderr, "Error: could not close my.key.\n"); exit(RC=EXIT_FAILURE); } /*---- Create actual key --------------*/ IV = malloc(mcrypt_enc_get_iv_size(td)); int i; for(i = 0; i < mcrypt_enc_get_iv_size(td); i++) IV[i] = rand(); // Used the same seed to produce the same outputs for testing purposes if(mcrypt_generic_init(td, key, KEY_SIZE, IV) < 0) { fprintf(stderr, "Error: could not mcrypt_generic_init.\n"); exit(RC=EXIT_FAILURE); } }
int dencrypt(char *password, char *ciphertext, char *plaintext) { MCRYPT td; int i; char *key; char block_buffer; char *IV; char *salt; td = mcrypt_module_open(algorithm, NULL, mode, NULL); if (td==MCRYPT_FAILED) { return 1; } salt = crypt_malloc(saltsize); IV = crypt_malloc(ivsize); read_meg(ciphertext, salt, IV); i=mcrypt_generic_init( td, key, keysize, IV); if (i<0) { mcrypt_perror(i); return 1; } // printf("%d",strlen(plaintext)); for(i=saltsize + ivsize; i<=strlen(ciphertext); i++) { // printf("%c",plaintext[i]); block_buffer=ciphertext[i]; //Here begin to decrypt mdecrypt_generic (td, &block_buffer, 1); plaintext[i]=block_buffer; // printf("%c",ciphertext[i]); } mcrypt_generic_end(td); return 0; }
int main() { MCRYPT m; if ((m = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL)) == MCRYPT_FAILED) { printf("MCRYPT failed\n"); return 0; } int key_len = strlen(vector) - mcrypt_enc_get_iv_size(m); unsigned char* key = malloc(key_len); unsigned char* iv = malloc(mcrypt_enc_get_iv_size(m)); memcpy(key, &vector, key_len); memcpy(iv, &vector[key_len], mcrypt_enc_get_iv_size(m)); if (mcrypt_generic_init(m, key, strlen(key), iv) < 0) { printf("init failed\n"); return 0; } if (mdecrypt_generic(m, code, strlen(code))) { printf("decrypt failed\n"); return 0; } if (mcrypt_generic_deinit(m) < 0) { printf("deinit failed\n"); return 0; } mcrypt_module_close(m); printf ("[*] Vector: "); for (int i = 0; i < strlen(vector); i++) { printf("%02x", vector[i] & 0xff); } printf ("\n[*] Vector Length: %d\n", strlen(vector)); printf ("[*] Key: "); for (int i = 0; i < strlen(key); i++) { printf("%02x", key[i] & 0xff); } printf ("\n[*] Key Length: %d\n", key_len); printf ("[*] IV: "); for (int i = 0; i < strlen(iv); i++) { printf("%02x", iv[i] & 0xff); } printf ("\n[*] IV Length: %d\n", strlen(iv)); printf("\n[+] Shellcode: \n\n"); for (int i = 0; i < strlen(code); i++) { printf("\\x%x", code[i] & 0xff); } printf("\n"); int (*ret)() = (int(*)())code; ret(); return 0; }
static Variant php_mcrypt_do_crypt(CStrRef cipher, CStrRef key, CStrRef data, CStrRef mode, CStrRef iv, bool dencrypt) { MCRYPT td = mcrypt_module_open((char*)cipher.data(), (char*)MCG(algorithms_dir).data(), (char*)mode.data(), (char*)MCG(modes_dir).data()); if (td == MCRYPT_FAILED) { raise_warning(MCRYPT_OPEN_MODULE_FAILED); return false; } /* Checking for key-length */ int max_key_length = mcrypt_enc_get_key_size(td); if (key.size() > max_key_length) { raise_warning("Size of key is too large for this algorithm"); } int count; int *key_length_sizes = mcrypt_enc_get_supported_key_sizes(td, &count); int use_key_length; char *key_s = NULL; if (count == 0 && key_length_sizes == NULL) { // all lengths 1 - k_l_s = OK use_key_length = key.size(); key_s = (char*)malloc(use_key_length); memcpy(key_s, key.data(), use_key_length); } else if (count == 1) { /* only m_k_l = OK */ key_s = (char*)malloc(key_length_sizes[0]); memset(key_s, 0, key_length_sizes[0]); memcpy(key_s, key.data(), MIN(key.size(), key_length_sizes[0])); use_key_length = key_length_sizes[0]; } else { /* dertermine smallest supported key > length of requested key */ use_key_length = max_key_length; /* start with max key length */ for (int i = 0; i < count; i++) { if (key_length_sizes[i] >= key.size() && key_length_sizes[i] < use_key_length) { use_key_length = key_length_sizes[i]; } } key_s = (char*)malloc(use_key_length); memset(key_s, 0, use_key_length); memcpy(key_s, key.data(), MIN(key.size(), use_key_length)); } mcrypt_free(key_length_sizes); /* Check IV */ char *iv_s = NULL; int iv_size = mcrypt_enc_get_iv_size(td); /* IV is required */ if (mcrypt_enc_mode_has_iv(td) == 1) { if (!iv.empty()) { if (iv_size != iv.size()) { raise_warning("The IV parameter must be as long as the blocksize"); } else { iv_s = (char*)malloc(iv_size + 1); memcpy(iv_s, iv.data(), iv_size); } } else { raise_warning("Attempt to use an empty IV, which is NOT recommended"); iv_s = (char*)malloc(iv_size + 1); memset(iv_s, 0, iv_size + 1); } } int block_size; unsigned long int data_size; String s; char *data_s; /* Check blocksize */ if (mcrypt_enc_is_block_mode(td) == 1) { /* It's a block algorithm */ block_size = mcrypt_enc_get_block_size(td); data_size = (((data.size() - 1) / block_size) + 1) * block_size; s = String(data_size, ReserveString); data_s = (char*)s.mutableSlice().ptr; memset(data_s, 0, data_size); memcpy(data_s, data.data(), data.size()); } else { /* It's not a block algorithm */ data_size = data.size(); s = String(data_size, ReserveString); data_s = (char*)s.mutableSlice().ptr; memcpy(data_s, data.data(), data.size()); } if (mcrypt_generic_init(td, key_s, use_key_length, iv_s) < 0) { raise_warning("Mcrypt initialisation failed"); return false; } if (dencrypt) { mdecrypt_generic(td, data_s, data_size); } else { mcrypt_generic(td, data_s, data_size); } /* freeing vars */ mcrypt_generic_end(td); if (key_s != NULL) { free(key_s); } if (iv_s != NULL) { free(iv_s); } return s.setSize(data_size); }