Skip to content

Commit

Permalink
AES encrypt/decrypt works
Browse files Browse the repository at this point in the history
IV is now in the beginning of the files
regex on IV works
check size of IV
QLineEdit max size for IV is set
need to consider IV as hex not ASCII
  • Loading branch information
QuentinGouchet committed Feb 28, 2016
1 parent de82869 commit 5c734a9
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 39 deletions.
42 changes: 29 additions & 13 deletions src/aes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ int AES::generateKey(unsigned int nbits, const char *root_keys)
return err;
}

int AES::aes_cbc_128_encrypt(const char *plaintextPath, const char *key,
const char *cipherPath, const char *iv)
int AES::aes_cbc_128_encrypt(const char *plaintextPath, const char *cipherPath,
const char *key, const char *iv)
{
FILE *plaintextFile = NULL;
FILE *ciphertextFile = NULL;
Expand Down Expand Up @@ -126,8 +126,8 @@ int AES::aes_cbc_128_encrypt(const char *plaintextPath, const char *key,

fileContent[plaintext_len] = 0x10;

fprintf(stdout, "FileContent: ");
print.printBuff(fileContent, ciphertext_len);
//fprintf(stdout, "FileContent: ");
//print.printBuff(fileContent, ciphertext_len);

err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
if(err)
Expand Down Expand Up @@ -158,8 +158,8 @@ int AES::aes_cbc_128_encrypt(const char *plaintextPath, const char *key,

err = gcry_cipher_encrypt(hd, fileContent, ciphertext_len, NULL, 0);

fprintf(stdout, "FileContent: ");
print.printBuff(fileContent, ciphertext_len);
//fprintf(stdout, "FileContent: ");
//print.printBuff(fileContent, ciphertext_len);

if(err)
{
Expand All @@ -181,6 +181,14 @@ int AES::aes_cbc_128_encrypt(const char *plaintextPath, const char *key,

printf("%s\n", pathCipherFile);

// start by writing the 16 bytes IV in the file
if(16 != fwrite(iv, 1, 16, ciphertextFile))
{
err = 1;
fprintf(stderr, "Could not write IV into cipher file\n");
goto out;
}

if(ciphertext_len != fwrite(fileContent, 1, ciphertext_len, ciphertextFile))
{
err = 1;
Expand All @@ -203,34 +211,33 @@ int AES::aes_cbc_128_encrypt(const char *plaintextPath, const char *key,
return err;
}

int AES::aes_cbc_128_decrypt(const char *ciphertextPath, const char *key,
const char *plaintextPath, const char *iv)
int AES::aes_cbc_128_decrypt(const char *ciphertextPath, const char *plaintextPath, const char *key)
{
FILE *plaintextFile = NULL;
FILE *ciphertextFile = NULL;

int blklen = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128);

unsigned char *fileContent = NULL;
unsigned char *ciphertext = NULL;
//unsigned char *ciphertext = NULL;

char pathPlainFile[50];

long fsize = 0;
int plaintext_len, ciphertext_len, i;
int plaintext_len, ciphertext_len;

gcry_cipher_hd_t hd = NULL;
gcry_error_t err = 0;

Util print;

printf("iv: ");
print.printBuff((unsigned char *)iv, blklen);
printf("key: ");
print.printBuff((unsigned char *)key, blklen);

printf("ciphertextpath: %s\n", ciphertextPath);

unsigned char *iv = (unsigned char *) gcry_malloc(blklen*sizeof(unsigned char));

if((ciphertextFile = fopen(ciphertextPath, "r")) == NULL)
{
err = 1;
Expand All @@ -252,7 +259,7 @@ int AES::aes_cbc_128_decrypt(const char *ciphertextPath, const char *key,
goto out;
}

ciphertext_len = fsize;
ciphertext_len = fsize - blklen;

fprintf(stdout, "ciphertext: %d\n", ciphertext_len);

Expand All @@ -271,6 +278,13 @@ int AES::aes_cbc_128_decrypt(const char *ciphertextPath, const char *key,
goto out;
}

if(fread(iv, 1, blklen, ciphertextFile) != blklen)
{
err = 1;
fprintf(stderr, "fread() failure on ciphertext file\n");
goto out;
}

if(fread(fileContent, 1, ciphertext_len, ciphertextFile) != ciphertext_len)
{
err = 1;
Expand Down Expand Up @@ -368,6 +382,8 @@ int AES::aes_cbc_128_decrypt(const char *ciphertextPath, const char *key,
fclose(ciphertextFile);
if(fileContent)
gcry_free(fileContent);
/*if(iv)
gcry_free(iv);*/
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion src/aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AES
AES();
int generateKey(unsigned int, const char *);
int aes_cbc_128_encrypt(const char *, const char *, const char *, const char *);
int aes_cbc_128_decrypt(const char *, const char *, const char *, const char *);
int aes_cbc_128_decrypt(const char *, const char *, const char *);
int aes_cbc_256_encrypt(const char *, const char *, const char *, const char *);
int aes_cbc_256_decrypt(const char *, const char *, const char *, const char *);
};
Expand Down
63 changes: 46 additions & 17 deletions src/cipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ Cipher::Cipher(int index, int public_cipher): QDialog()
gl->addWidget(comboSize, 2, 2);

gl->addWidget(labelIv, 3, 0);

// index == 4 is AES; we want a 128 bits IV only
if(index == 4)
leIv->setMaxLength(gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES));
// index = 5 3DES, IV is 64 bits
else if(index == 5)
leIv->setMaxLength(gcry_cipher_get_algo_blklen(GCRY_CIPHER_3DES));
gl->addWidget(leIv, 3, 1);
gl->addWidget(radioIv, 3, 2);

Expand Down Expand Up @@ -379,33 +386,42 @@ void Cipher::computeAES(){
// Next line is not needed since we derive the key from the passphrase
// reKey = new QRegExp("^[\\w|/]+\\.(key)$");

reIv = new QRegExp("[a-fA-F0-9]+");

// Let's derive the key given by the password
if( (leKey->text().isEmpty() || lePlain->text().isEmpty() || leCipher->text().isEmpty() || leIv->text().size() != 16) )
{
mb = new QMessageBox(this);
mb->setWindowTitle("Information");
mb->setText("One field is still empty!");
mb->exec();
return ;
}

fprintf(stdout, "passphrase: %s\n", leKey->text().toLocal8Bit().constData());
fprintf(stdout, "size in bytes: %d\n", comboSize->currentText().toInt()/8);

int keylen = comboSize->currentText().toLocal8Bit().toInt()/8;
int pass_len = leKey->text().length();

// Here check whether adioIv is checked and if not randomly generate IV and store in the file
/*unsigned char *iv;
int ivSize = comboSize->currentText().toInt();
if(!radioIv->isChecked())
// Here check whether radioIv is checked and if not randomly generate IV and store in the file
unsigned char *iv = NULL;
int ivSize = 16;
if(!radioIv->isChecked())
{
iv = (unsigned char *) malloc(ivSize*sizeof(unsigned char));
gcry_randomize(&iv, ivSize, GCRY_STRONG_RANDOM);
iv = (unsigned char *) gcry_malloc(ivSize*sizeof(unsigned char));
gcry_randomize(iv, ivSize, GCRY_STRONG_RANDOM);
Util print;
print.printBuff(iv, ivSize);
}
else
iv = (unsigned char *) leIv->text().toLocal8Bit().constData();

printf("we pass here\n");*/



fprintf(stdout, "size of pass: %d\n", pass_len);

Util print;
printf("iv: ");
print.printBuff(iv, ivSize);

int hash_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256);

Expand Down Expand Up @@ -440,23 +456,31 @@ void Cipher::computeAES(){
QLineEdit contienne bien une extension .in pour le fichier d'entrée, une extension .out pour le fi-
chier de sortie et une extension .key pour le fichier de clé
*/
if(!reIv->exactMatch(leIv->text()))
{
mb = new QMessageBox(this);
mb->setText("The IV field is incorrect");
mb->setWindowTitle("Information");
mb->exec();
return ;
}

if(reCipher->exactMatch(leCipher->text()))
{
aes = new AES();

if(!(strcmp(comboMode->currentText().toLocal8Bit().constData(), "CBC") ||
strcmp(comboSize->currentText().toLocal8Bit().constData(), "128")))
{
printf("AES128 encryption\n");
printf("AES-128-CBC encryption\n");
rep = aes->aes_cbc_128_encrypt(lePlain->text().toLocal8Bit().constData(),
(const char *) pass,
leCipher->text().toLocal8Bit().constData(),
leIv->text().toLocal8Bit().constData());
leCipher->text().toLocal8Bit().constData(),
(const char *) pass, (const char*) iv);
}
else if(!(strcmp(comboMode->currentText().toLocal8Bit().constData(), "CBC") ||
strcmp(comboSize->currentText().toLocal8Bit().constData(), "256")))
{
printf("AES256 encryption\n");
printf("AES-256 encryption\n");
rep = aes->aes_cbc_256_encrypt(lePlain->text().toLocal8Bit().constData(),
(const char *) pass,
leCipher->text().toLocal8Bit().constData(),
Expand Down Expand Up @@ -502,11 +526,16 @@ void Cipher::computeAES(){
}

// not actually needed, it is done by derivePassphrase
// why is gcry_free(pass) failing?
out:
if(hd)

if(hd)
gcry_md_close(hd);
return ;
/* if(hd)
gcry_md_close(hd);
if(pass)
gcry_free(pass);
gcry_free(pass);*/
/*if(iv)
free(iv);*/
}
Expand Down
1 change: 1 addition & 0 deletions src/cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Cipher : public QDialog
QRegExp *rePlain;
QRegExp *reCipher;
QRegExp *reKey;
QRegExp *reIv;

QMessageBox *mb;

Expand Down
15 changes: 7 additions & 8 deletions src/decipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Decipher::Decipher(int index, int public_cipher): QDialog()
else
labelKey = new QLabel("Type in your passphrase :", this);
labelMode = new QLabel("Block chaining mode and size :");
labelIv = new QLabel("Enter IV :", this);
//labelIv = new QLabel("Enter IV :", this);

buttonBrowseCipher = new QPushButton("Browse", this);
if(public_cipher)
Expand All @@ -46,7 +46,7 @@ Decipher::Decipher(int index, int public_cipher): QDialog()
if(!public_cipher)
leKey->setEchoMode(QLineEdit::Password);

leIv = new QLineEdit(this);
//leIv = new QLineEdit(this);

fdCipher = new QFileDialog(this);
fdKey = new QFileDialog(this);
Expand Down Expand Up @@ -82,8 +82,8 @@ Decipher::Decipher(int index, int public_cipher): QDialog()
gl->addWidget(comboMode, 2, 1);
gl->addWidget(comboSize, 2, 2);

gl->addWidget(labelIv, 3, 0);
gl->addWidget(leIv, 3, 1);
/*gl->addWidget(labelIv, 3, 0);
gl->addWidget(leIv, 3, 1);*/

gl->addWidget(labelPlain, 4, 0);
gl->addWidget(lePlain, 4, 1);
Expand Down Expand Up @@ -459,11 +459,10 @@ void Decipher::computeAES()
if(!(strcmp(comboMode->currentText().toLocal8Bit().constData(), "CBC") ||
strcmp(comboSize->currentText().toLocal8Bit().constData(), "128")))
{
printf("AES128 decryption\n");
printf("AES-128-CBC decryption\n");
rep = aes->aes_cbc_128_decrypt(leCipher->text().toLocal8Bit().constData(),
(const char *) pass,
lePlain->text().toLocal8Bit().constData(),
leIv->text().toLocal8Bit().constData());
lePlain->text().toLocal8Bit().constData(),
(const char *) pass);
}
else if(!(strcmp(comboMode->currentText().toLocal8Bit().constData(), "CBC") ||
strcmp(comboSize->currentText().toLocal8Bit().constData(), "256")))
Expand Down
Binary file modified src/src
Binary file not shown.

0 comments on commit 5c734a9

Please sign in to comment.