DevOTechS Forums

HIME => General HIME discussions => Topic started by: gwynw on May 06, 2008, 04:37:51 PM



Title: Truncation problems
Post by: gwynw on May 06, 2008, 04:37:51 PM
 ???  Hello,

I am using C# and finding problems. Below are some calculations I have been doing with RSA.

These work for small values (a few bytes) and I am able to encrypt and decrypt succesfully.

However, when I try to scale up to, say, 96 bytes RSA keyset, then I get truncation problems. The first few bytes of my caclucated values are OK, but then the rest is wrong.

Any ideas?


HIME.hi_PutReg(pString, 10);      //p = 10
HIME.hi_PutReg(qString, 11);      //q = 11
HIME.hi_Mul(10, 11, 12);            //Modulus = 12
                                             //e = 2
HIME.hi_Decr(10,15);                //p-1 = 15
HIME.hi_Decr(11,16);                //q-1 = 16
HIME.hi_Mul(15,16,17);              //(p-1)(q-1)
HIME.hi_Modinv(2, 17, 18);
HIME.hi_Encrypt_Rsa(1, 2, 12, 4);

Any help would be most welcome. Is there a manual showing examples?

Thank you

Gwyn


Title: Re: Truncation problems
Post by: Admin on May 06, 2008, 05:06:25 PM
Hi Gwyn,

You seem to be trying to create the RSA keys yourself ?
Unless you have a good reason not to, I highly recommend to use the HIME hi_GenerateRSAKeys or hi_GenerateRSAKeys_CRT  functions to generate the RSA public and private keys.

The HIME manual (included with the zip package you downloaded) has a page called "RSA encryption/decryption" that shows a complete example (though not in C#) of how to encrypt and decrypt data with RSA, both the 'classic' way as with using CRT (Chinese Remainder Theorem).

Kind regards
Eddy


Title: Re: Truncation problems
Post by: Admin on May 06, 2008, 05:08:28 PM
PS. Also remember that with RSA, your plaintext length must always be smaller than the modulus length.

Eddy


Title: Re: Truncation problems
Post by: Admin on May 06, 2008, 08:56:26 PM
Hi Gwyn,

I looked a little deeper into your code snippet. Two suggestions:
1 - Use HIME function hi_GenerateRSAKeys to generate public key, private key and modulus, because you are cutting corners generating the key in your code example. The relation between public key exponent 'e' and '(p-1)(q-1)' must be so that GCD of e and (p-1)(q-1) = 1 , where GCD is the Greatest Common Divisor.
Also: 1 < e < (p-1)(q-1) .
If these conditions are not fulfilled, you must choose new p and q (and/or e) until the conditions are fulfilled. P and q are both prime numbers, as you know.

Maybe you are doing this, but it is not shown in your code.

2 - As stated before, make sure that plaintext is smaller than the modulus.

Hope this helps.

Kind regards
Eddy


Title: Re: Truncation problems
Post by: gwynw on May 07, 2008, 07:52:03 AM
Eddy,

Thank you for your support.

My P, Q and e values are from a know good RSA key, hence I should be able to calculate all the other components from these.
The reason I am doing this is that I want to write a tool to test that RSA keys have been correctly migrated from a pre existing system to a new system - hence I have to use the existing keys.

I will do a bit more experimentation before getting back to you with an example of my truncation problem.

Best regards,

Gwyn


Title: Re: Truncation problems
Post by: Admin on May 07, 2008, 09:26:32 AM
My P, Q and e values are from a know good RSA key, hence I should be able to calculate all the other components from these.
Gwyn,
That is correct.

Meanwhile, just thinking out loud:
  • Is your plaintext pure ascii string or is it binary (perhaps containing possible zero bytes) ?
  • After encryption, do you retrieve the ciphertext from HIME to restore it later in HIME, or do you just leave it in the HIME register ?
  • Truncation: is you decrypted ciphertext (which should normally match your original plaintext) actually truncated (chopped off) or does it contain a series of correct characters/bytes and, following that, a series of incorrect characters ? From your first post I assume the latter, but it has me a little confused with 'truncation'  :)

Kind regards
Eddy