Home All Groups Group Topic Archive Search About

NCrypto Encryption & Decryption

Author
2 May 2005 9:58 AM
sushant.bhatia
Hi All.
I'm using the NCrypto dll for RSA Encryption/Decryption
(http://sourceforge.net/projects/ncrypto/). My encryption code in .Net
is pretty simple. The dataToEncrypt length is 1024. The returned data
is 1161 in length.

public static byte[] EncryptRSA(string certFilePublic, byte[]
dataToEncrypt)
{
    RSAParameters rsaParams = GetRSAPubKey(certFilePublic);

    byte[] decryptedData = null;
    try
    {
        decryptedData =
NCrypto.Security.Cryptography.CryptoHelper.Encrypt(dataToEncrypt,
rsaParams, false);
        return decryptedData;
    }
    catch (Exception eer)
    {
        System.Windows.Forms.MessageBox.Show(eer.ToString());
        return null;
    }
}


The problem is when I try to decrypt I get a "System.ArgumentOutOfRange
Exception: Data length must be a multiple of 128. Parameter name:
cipherText".
The problem is that the encrypt function creates data that is 1161
bytes long which isn't a multiple of 128.

I've tried changing the data size going into the decrypt function but
the data returned is always a multiple of 129 and not 128.

Please HELP! I'm so stuck as to what to do. :-(

Author
3 May 2005 11:58 AM
Hernan de Lahitte
Hi,

The problem that you describe is actually related to the encrypted data
length (padding bytes). You 1024 clear text should give a 1152 byte array
after encryption (not a 1162 as you describe).

Try this simple test:

byte[] cipher = CryptoHelper.Encrypt( CryptoHelper.ComputeRandomBytes(1024),
CryptoHelper.RsaInstance.ExportParameters(false),false );

// cipher length should be 1152 bytes without OAEP (oaep parameter = false).


byte[] plain = CryptoHelper.Decrypt( cipher,
CryptoHelper.RsaInstance.ExportParameters(true), false );

Perhaps there is a difference in your RSAParameters value or your OS version
(assume an XP SP2?) and or your NCrypto version (assume you have the last
published?).

--
Hernan de Lahitte
http://clariusconsulting.net/hdl



<sushant.bha***@gmail.com> wrote in message
Show quoteHide quote
news:1115027906.665598.223210@l41g2000cwc.googlegroups.com...
> Hi All.
> I'm using the NCrypto dll for RSA Encryption/Decryption
> (http://sourceforge.net/projects/ncrypto/). My encryption code in .Net
> is pretty simple. The dataToEncrypt length is 1024. The returned data
> is 1161 in length.
>
> public static byte[] EncryptRSA(string certFilePublic, byte[]
> dataToEncrypt)
> {
> RSAParameters rsaParams = GetRSAPubKey(certFilePublic);
>
> byte[] decryptedData = null;
> try
> {
> decryptedData =
> NCrypto.Security.Cryptography.CryptoHelper.Encrypt(dataToEncrypt,
> rsaParams, false);
> return decryptedData;
> }
> catch (Exception eer)
> {
> System.Windows.Forms.MessageBox.Show(eer.ToString());
> return null;
> }
> }
>
>
> The problem is when I try to decrypt I get a "System.ArgumentOutOfRange
> Exception: Data length must be a multiple of 128. Parameter name:
> cipherText".
> The problem is that the encrypt function creates data that is 1161
> bytes long which isn't a multiple of 128.
>
> I've tried changing the data size going into the decrypt function but
> the data returned is always a multiple of 129 and not 128.
>
> Please HELP! I'm so stuck as to what to do. :-(
>