/**
 * RSA 
 *   Contient 4 fonctions principales de l'agorithme RSA
 *     - RSAPublicKey pour le support de la clé publique et ses propriétés
 *     - RSAPrivateKey pour le support de la clé privée et ses propriétés
 *     - encryptedString pour encrypter un texte à l'aide de la clé public
 *     - decryptedString pour décrypter un texte à l'aide de la clé privée
 *     
 *   Utilise BigInt.js et BigIntMath.js
 *   
 * @package Aedituus
 * @subpackage javascript
 * @version 2.0
 * @author Wamania
 * @copyright Copyright (c) 2008, Wamania
 * @license Creative Commons Paternité 2.0 ( http://creativecommons.org/licenses/by/2.0/fr/ )
 * @link http://www.wamania.com
 */


/**
 * Objet supportant la clé publique
 * 
 * @param {Object} encryptionExponent
 * @param {Object} modulus
 * @param {Integer} nbBits
 */ 
function RSAPublicKey(encryptionExponent, modulus, nbBits)
{
	this.e = biFromString(encryptionExponent,10);
	this.m = biFromString(modulus,10);
    
	this.chunk_len = (nbBits - 1);
    this.block_len = Math.ceil(this.chunk_len / 8);
}

/**
 * Chiffre une chaîne de caractère en utilisant la clé publique.
 * 
 * @param {Object} key
 * @param {String} s
 */
function encryptedString(key, str)
{ 
    var block;
    str += String.fromCharCode(01);
    block = bin2int(str);
    
    var data_len = bitLen(block);
    
    var curr_pos = 0;
    var encryptedData = '';
    var i = 0;
    while (curr_pos < data_len) 
    {
        var tmp = subint(block, curr_pos, key.chunk_len);
        var encryptedBlock = int2bin(biPowMod(tmp, key.e, key.m));
        for (i=encryptedBlock.length; i<key.block_len; i++)
        {   
            encryptedBlock += "\0";
        }
        encryptedData += encryptedBlock;
        curr_pos += key.chunk_len;
    }
    return biToString(bin2int(encryptedData), 10);
}