January 27
2009

RSA Cryptography in C# Tutorial part 3b

C# Tutorial Windows

This is basically the same as 3a, but this example works for any bitstrength.

I STILL NEED TO FULLY TEST THIS

Firstly, convert the data into a byte array, possibly with the command 'byte[] byteData = Encoding.UTF32.GetBytes(text);'

To use any bitstrength, you need to include this information in the public and private key files.

For 2048 bit encryption, the maximum Length is 214. This number is got by dividing the bitstrength by 8, then taking 42 away

n/8 -42

From here on, it is exactly the same.

Then put the length of the byte array into a variable, like dataLength.

You will then want another variable, iterations, or loops, and store in it the dataLength divided by the maxLength. This number will be how many times the encryption loop will run.

Define a new StringBuilder. I will use sb.

Now make a loop, looping for the amount of iterations, for example:

for (int i = 0; i <= iterations; i++)

make sure to use <=, NOT <.

in this loop, create a temporary Byte array the dataLength long, UNLESS it is the last chunk, when just use up the remaining part:

byte[] tempBytes = new byte[(dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i];

Write some code to copy the corresponding part of data:

Buffer.BlockCopy(byteData, maxLength * i, tempBytes, 0, tempBytes.Length);

Pages: 1 2

One Comment on “RSA Cryptography in C# Tutorial part 3b”

Leave a Reply