示例#1
0
文件: crypt.c 项目: ctos/ctos-crypt
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;

}
示例#2
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;

}
示例#3
0
文件: crypt.c 项目: ctos/ctos-crypt
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;

       }
示例#4
0
void dump_testcase_block(MCRYPT td, unsigned char *key, int key_size,
                         unsigned char *iv, int iv_size, int data_size,
                         padding_t padding) {
    int mc_ret;
    int is_block, block_size, block_overlap, block_fill;
    int i;
    unsigned char *plaintext, *ciphertext;

    mc_ret = mcrypt_generic_init(td, (void *)key, key_size, (void *)iv);
    if (mc_ret < 0) {
        mcrypt_perror(mc_ret);
        return;
    }

    plaintext = gen_rand_data(data_size);
    if (plaintext == NULL) {
        return;
    }

    is_block = mcrypt_enc_is_block_mode(td);
    if (is_block) {
        block_size = mcrypt_enc_get_block_size(td);
        block_overlap = data_size % block_size;
        block_fill = block_size - block_overlap;
        if (padding == PADDING_NONE) {
            /* do nothing */
        }
        else if (padding == PADDING_PKCS7) {
            if (block_fill == 0) {
                /* ALWAYS add padding */
                block_fill = block_size;
            }
            plaintext = (unsigned char *)realloc(plaintext,
                                                 data_size + block_fill);
            for (i = 0; i < block_fill; i++) {
                plaintext[data_size+i] = block_fill;
            }
            data_size = data_size + block_fill;
            if ((data_size % block_size) != 0) {
                fprintf(stderr, "bad data size!\n");
                exit(1);
            }
        }
        else if (padding == PADDING_ZEROS) {
            if (block_overlap != 0) {
                plaintext = (unsigned char *)realloc(plaintext,
                                                     data_size + block_fill);
                for (i = 0; i < block_fill; i++) {
                    plaintext[data_size+i] = '\0';
                }
                data_size = data_size + block_fill;
            }
        }
        else {
            fprintf(stderr, "bad error\n");
            exit(1);
        }
    }

    ciphertext = malloc(data_size);
    if (ciphertext == NULL) {
        fprintf(stderr, "Out of memory\n");
        return;
    }

    memcpy( (void *)ciphertext, (void *)plaintext, data_size);

    mc_ret = mcrypt_generic(td, ciphertext, data_size);
    if (mc_ret == 0) {
        char *enc_key, *enc_iv, *enc_pt, *enc_ct;
        enc_key = dump_value( (void *)key, key_size );
        enc_iv  = dump_value( (void *)iv, iv_size );
        enc_pt  = dump_value( (void *)plaintext, data_size );
        enc_ct  = dump_value( (void *)ciphertext, data_size );

        printf("algo=%s,mode=%s,key=%s,iv=%s,padding=%s,pt=%s,ct=%s\n",
               testing_algo, testing_mode, enc_key, enc_iv,
               padding_name(padding), enc_pt, enc_ct);

        free(enc_key);
        free(enc_iv);
        free(enc_pt);
        free(enc_ct);
    }

    free(plaintext);
    free(ciphertext);

    mc_ret = mcrypt_generic_deinit(td);
    if (mc_ret < 0) {
        fprintf(stderr, "Error %d during deinit of %s in %s mode"
                " (%d-byte key)\n", testing_algo, testing_mode, key_size);
        return;
    }
}