Example #1
0
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;

}
Example #2
0
static void
cipher_free(PX_Cipher * c)
{
	MCRYPT		ctx = (MCRYPT) c->ptr;

	if (c->pstat)
		mcrypt_generic_end(ctx);
	else
		mcrypt_module_close(ctx);

	px_free(c);
}
Example #3
0
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;

}
Example #4
0
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;

       }
Example #5
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);
}