1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
int encrypt(
void* buffer,
int buffer_len, /* Because the plaintext could include null bytes*/
char* IV,
char* key,
int key_len
){
int i = 0;
MCRYPT td = mcrypt_module_open("rijndael-256", 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;
}
int decrypt(
void* buffer,
int buffer_len,
char* IV,
char* key,
int key_len
){
MCRYPT td = mcrypt_module_open("rijndael-256", 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);
mdecrypt_generic(td, buffer, buffer_len);
mcrypt_generic_deinit (td);
mcrypt_module_close(td);
return 0;
}
void display(char* ciphertext, int len){
int v;
for (v=0; v<len; v++){
printf("%d ", ciphertext[v]);
}
printf("-\n");
}
int main()
{
char plaintext[SIZE + 1] = {0};
char key[SIZE_PWD + 1] = {0};
int rtn = 0;
MCRYPT td, td2;
char* IV;
int keysize = 16; /* 128 bits */
char* buffer;
int buffer_len = 0;
int i = 0;
int choix = 0;
td = mcrypt_module_open("rijndael-256", NULL, "cbc", NULL);
IV = malloc(mcrypt_enc_get_iv_size(td));
for (i=0; i< mcrypt_enc_get_iv_size( td); i++) {
IV[i]=rand();
}
printf("Saisir le pwd :\n");
do
{
rtn = scanf(" %"XSTR(SIZE_PWD)"[^\n]", key);
clean_stdin();
}
while (rtn != 1);
keysize = strlen(key);
printf("Saisir le texte :\n");
rtn = 0;
do
{
rtn = scanf(" %"XSTR(SIZE)"[^\n]", plaintext);
clean_stdin();
}
while (rtn != 1);
buffer_len = strlen(plaintext);
buffer = calloc(1, buffer_len);
strncpy(buffer, plaintext, buffer_len);
do {
printf("\nSaisir le choix (0 crypter, 1 décrypter, 2 quitter) :\n");
scanf("%d", &choix);
if(choix == 0 ){
printf("plain: %s\n", plaintext);
encrypt(buffer, buffer_len, IV, key, keysize);
printf("cipher: ");
display(buffer , buffer_len);
printf("IV :%x-", IV);
}
else if( choix == 1 ){
printf("Saisir le pwd :\n");
do
{
rtn = scanf(" %"XSTR(SIZE_PWD)"[^\n]", key);
clean_stdin();
}
while (rtn != 1);
keysize = strlen(key);
printf("Saisir le texte :\n");
rtn = 0;
do
{
rtn = scanf(" %"XSTR(SIZE)"[^\n]", plaintext);
clean_stdin();
}
while (rtn != 1);
buffer_len = strlen(plaintext);
buffer = calloc(1, buffer_len);
strncpy(buffer, plaintext, buffer_len);
fflush(stdin);
printf("Saisir l'IV :\n");
rtn = 0;
do
{
rtn = scanf(" %"XSTR(SIZE)"[^\n]", IV);
clean_stdin();
}
while (rtn != 1);
display(buffer , buffer_len);
decrypt(buffer, buffer_len, IV, key, keysize);
printf("decrypt: %s\n", buffer);
}
} while( choix != 2);
free(IV);
free(buffer);
return 0;
} |
Partager