TPElement* OpenSSLRSAPermutation::compute(TPElement * tpEl) {
	if (!isKeySet())
		throw IllegalStateException("keys aren't set");
	RSAElement * rsaEl = dynamic_cast<RSAElement *>(tpEl);
	if (!rsaEl) 
		throw invalid_argument("trapdoor element type doesn't match the trapdoor permutation type");

	// Get the pointer for the native object.
	biginteger elementP = rsaEl->getElement();

	//Call the native function.
	biginteger result = computeRSA(elementP);

	// Create and initialize a RSAElement with the result.
	RSAElement * returnEl = new RSAElement(modulus, result, false);

	return returnEl; // return the created TPElement.
}
Verify::Verify(int index) : QDialog(){
    this->setFixedSize(800, 400);
    this->setWindowTitle("Verify");

    labelHash = new QLabel("Choose the file which contain the hashed message (*.hash)", this);
    labelSigned = new QLabel("Choose the file which contain the signed message (*.sign)", this);
    labelPuKey = new QLabel("Choose the file which contain the public key (*.puKey)", this);

    buttonBrowseHash = new QPushButton("Browse", this);
    buttonBrowseSigned = new QPushButton("Browse", this);
    buttonBrowsePuKey = new QPushButton("Browse", this);
    buttonCancel = new QPushButton("Cancel", this);
    buttonCompute = new QPushButton("Compute", this);

    leHash = new QLineEdit(this);
    leSigned = new QLineEdit(this);
    lePuKey = new QLineEdit(this);

    fdHash = new QFileDialog(this);
    fdSigned = new QFileDialog(this);
    fdPuKey = new QFileDialog(this);

    fdHash->setDirectory("../ressources/");
    fdSigned->setDirectory("../ressources/");
    fdPuKey->setDirectory("../ressources/");
    fdPuKey->setNameFilter("*.puKey");
    fdSigned->setNameFilter("*.sign");
    fdHash->setNameFilter("*.hash");

    gl = new QGridLayout(this);

    gl->addWidget(labelHash, 0, 0);
    gl->addWidget(leHash, 0, 1);
    gl->addWidget(buttonBrowseHash, 0, 2);

    gl->addWidget(labelSigned, 1, 0);
    gl->addWidget(leSigned, 1, 1);
    gl->addWidget(buttonBrowseSigned, 1, 2);

    gl->addWidget(labelPuKey, 2, 0);
    gl->addWidget(lePuKey, 2, 1);
    gl->addWidget(buttonBrowsePuKey, 2, 2);

    gl->addWidget(buttonCancel, 3, 1);
    gl->addWidget(buttonCompute, 3, 2);

    QObject::connect(buttonCancel, SIGNAL(clicked()), this, SLOT(close()));

    QObject::connect(buttonBrowseHash, SIGNAL(clicked()), fdHash, SLOT(exec()));
    QObject::connect(buttonBrowseSigned, SIGNAL(clicked()), fdSigned, SLOT(exec()));
    QObject::connect(buttonBrowsePuKey, SIGNAL(clicked()), fdPuKey, SLOT(exec()));
    QObject::connect(fdHash, SIGNAL(fileSelected(QString)), leHash, SLOT(setText(QString)));
    QObject::connect(fdSigned, SIGNAL(fileSelected(QString)), leSigned, SLOT(setText(QString)));
    QObject::connect(fdPuKey, SIGNAL(fileSelected(QString)), lePuKey, SLOT(setText(QString)));

    this->setLayout(gl);

    switch(index){
        case 0:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeRSA()));
            break;
        case 1:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeElGamal()));
            break;
        case 2:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeDSA()));
            break;
        default:
            this->close();
    }
}
Cipher::Cipher(int index, int public_cipher): QDialog()
{
    setFixedSize(800, 400);
    this->setWindowTitle("Cipher");

    labelPlain = new QLabel("Choose file to cipher :",this);
    labelCipher = new QLabel("Name the output file :",this);    
    if(public_cipher)
        labelKey = new QLabel("Choose which key to use :",this);
    else
        labelKey = new QLabel("Type in your passphrase :", this);
    labelMode = new QLabel("Block chaining mode and size :");
    labelIv = new QLabel("Enter IV :", this);

    buttonBrowsePlain = new QPushButton("Browse", this);
    if(public_cipher)
        buttonBrowseKey = new QPushButton("Browse", this);
    buttonCancel = new QPushButton("Cancel", this);
    buttonCompute = new QPushButton("Compute", this);

    //QStringList listMode(QStringList() << "CBC");
    QStringList listSize(QStringList() << "128" << "256");

    if(index == 4)
    {
        comboSize->addItem("128", GCRY_CIPHER_AES);
        comboSize->addItem("256", GCRY_CIPHER_AES256);
    }

    comboMode = new QComboBox(this);
    //comboMode->addItems(listMode);

    comboMode->addItem("CBC", GCRY_CIPHER_MODE_CBC);

    comboSize = new QComboBox(this);
    comboSize->addItems(listSize);

    lePlain = new QLineEdit(this);
    leCipher = new QLineEdit(this);
    leKey = new QLineEdit(this);

    // EchoMode(1) sets a password type of echo
    if(!public_cipher)
        leKey->setEchoMode(QLineEdit::Password);

    leIv = new QLineEdit(this);
    leIv->setEnabled(false);

    radioIv = new QRadioButton(tr("Manually enter IV"));

    fdPlain = new QFileDialog(this);
    fdKey = new QFileDialog(this);

    fdPlain->setDirectory("../ressources/");
    fdKey->setDirectory("../ressources/");

    QStringList listFilters;

    if(public_cipher)
        listFilters << "*.puKey" << "*";
    else
        listFilters << "*.key" << "*";

    fdKey->setNameFilters(listFilters);

    gl = new QGridLayout(this);

    gl->addWidget(labelPlain, 0, 0);
    gl->addWidget(lePlain, 0, 1);
    gl->addWidget(buttonBrowsePlain, 0, 2);

    gl->addWidget(labelKey, 1, 0);
    gl->addWidget(leKey, 1, 1);

    if(public_cipher)
        gl->addWidget(buttonBrowseKey, 1, 2);

    gl->addWidget(labelMode, 2, 0);
    gl->addWidget(comboMode, 2, 1);
    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);

    gl->addWidget(labelCipher, 4, 0);
    gl->addWidget(leCipher, 4, 1);

    gl->addWidget(buttonCancel, 5, 1);
    gl->addWidget(buttonCompute, 5, 2);

    this->setLayout(gl);

    QObject::connect(radioIv,SIGNAL(clicked()), this, SLOT(hideIvBox()));

    QObject::connect(buttonCancel,SIGNAL(clicked()),this,SLOT(close()));
    QObject::connect(buttonBrowsePlain, SIGNAL(clicked()), fdPlain, SLOT(exec()));

    if(public_cipher)
        QObject::connect(buttonBrowseKey, SIGNAL(clicked()), fdKey, SLOT(exec()));

    QObject::connect(fdPlain, SIGNAL(fileSelected(QString)), lePlain, SLOT(setText(QString)));
    QObject::connect(fdKey, SIGNAL(fileSelected(QString)), leKey, SLOT(setText(QString)));

    switch(index){
        case 0:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeRSA()));
            comboMode->setEnabled(false);
            comboSize->setEnabled(false);
            leIv->setEnabled(false);
            break;
        case 1:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeElGamal()));
            comboMode->setEnabled(false);
            comboSize->setEnabled(false);
            leIv->setEnabled(false);
            break;
        case 2:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeRabin()));
            comboMode->setEnabled(false);
            comboSize->setEnabled(false);
            leIv->setEnabled(false);
            break;
        case 3:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeRSAOAEP()));
            comboMode->setEnabled(false);
            comboSize->setEnabled(false);
            leIv->setEnabled(false);
            break;
        case 4:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeAES()));
            break;
        case 5:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeDES()));
            break;
        default:
            this->close();
            break;
    }
}
Decipher::Decipher(int index, int public_cipher): QDialog()
{
    setFixedSize(800, 400);
    this->setWindowTitle("Decipher");

    labelCipher = new QLabel("Choose file to decipher :",this);
    labelPlain = new QLabel("Choose where to create output file :",this);
    if(public_cipher)
        labelKey = new QLabel("Choose which key to use :",this);
    else
        labelKey = new QLabel("Type in your passphrase :", this);
    labelMode = new QLabel("Block chaining mode and size :");
    //labelIv = new QLabel("Enter IV :", this);

    buttonBrowseCipher = new QPushButton("Browse", this);
    if(public_cipher)
        buttonBrowseKey = new QPushButton("Browse", this);
    buttonCancel = new QPushButton("Cancel", this);
    buttonCompute = new QPushButton("Compute", this);

    QStringList listMode(QStringList() << "CBC");
    QStringList listSize(QStringList() << "128" << "256");

    comboMode = new QComboBox(this);
    comboMode->addItems(listMode);

    comboSize = new QComboBox(this);
    comboSize->addItems(listSize);

    lePlain = new QLineEdit(this);
    leCipher = new QLineEdit(this);
    leKey = new QLineEdit(this);

    // EchoMode(1) sets a password type of echo
    if(!public_cipher)
        leKey->setEchoMode(QLineEdit::Password);

    //leIv = new QLineEdit(this);

    fdCipher = new QFileDialog(this);
    fdKey = new QFileDialog(this);

    fdCipher->setDirectory("../ressources/");
    fdKey->setDirectory("../ressources/");

    QStringList listFilters;

    if(public_cipher)
        listFilters << "*.puKey" << "*";
    else
        listFilters << "*.key" << "*";

    fdCipher->setNameFilter("*.cipher");
    fdKey->setNameFilter("*.prKey");

    fdKey->setNameFilters(listFilters);

    gl = new QGridLayout(this);

    gl->addWidget(labelCipher, 0, 0);
    gl->addWidget(leCipher, 0, 1);
    gl->addWidget(buttonBrowseCipher, 0, 2);

    gl->addWidget(labelKey, 1, 0);
    gl->addWidget(leKey, 1, 1);

    if(public_cipher)
        gl->addWidget(buttonBrowseKey, 1, 2);

    gl->addWidget(labelMode, 2, 0);
    gl->addWidget(comboMode, 2, 1);
    gl->addWidget(comboSize, 2, 2);

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

    gl->addWidget(labelPlain, 4, 0);
    gl->addWidget(lePlain, 4, 1);

    gl->addWidget(buttonCancel, 5, 1);
    gl->addWidget(buttonCompute, 5, 2);

    this->setLayout(gl);

    QObject::connect(buttonCancel,SIGNAL(clicked()),this,SLOT(close()));
    QObject::connect(buttonBrowseCipher, SIGNAL(clicked()), fdCipher, SLOT(exec()));

    if(public_cipher)
        QObject::connect(buttonBrowseKey, SIGNAL(clicked()), fdKey, SLOT(exec()));

    QObject::connect(fdCipher, SIGNAL(fileSelected(QString)), leCipher, SLOT(setText(QString)));
    QObject::connect(fdKey, SIGNAL(fileSelected(QString)), leKey, SLOT(setText(QString)));

    switch(index){
        case 0:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeRSA()));
            comboMode->setEnabled(false);
            comboSize->setEnabled(false);
            leIv->setEnabled(false);
            break;
        case 1:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeRSACRT()));
            comboMode->setEnabled(false);
            comboSize->setEnabled(false);
            leIv->setEnabled(false);
            break;
        case 2:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeElGamal()));
            comboMode->setEnabled(false);
            comboSize->setEnabled(false);
            leIv->setEnabled(false);
            break;
        case 3:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeRabin()));
            comboMode->setEnabled(false);
            comboSize->setEnabled(false);
            leIv->setEnabled(false);
            break;
        case 4:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeRSAOAEP()));
            comboMode->setEnabled(false);
            comboSize->setEnabled(false);
            leIv->setEnabled(false);
            break;
        case 5:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeAES()));
            break;
        case 6:
            QObject::connect(buttonCompute, SIGNAL(clicked()), this, SLOT(computeDES()));
            break;
        default:
            this->close();
            break;
    }
}