Encode using RSA

 

Algorithm for encoding plaintext into cipher integers

 

Global

 

Global Constants:

1. blockLen: The number of bits to be encoded at a time (initially 6).

2. asciiLen: The number of ascii bits (7).

3. Various file numbers.

 

Main

 

Local variables:

1. e: The encrypting key value, e.

2. n: The encrypting key value, n.

 

Basic Algorithm:

1. Read e and n.

2. Call plainToBits to convert the plaintext to a bit stream.

3. Close plainBits file for writing and open it for reading.

4. Call Encode(e,n) to read plain bits and encode them.

 

plainToBits

 

Parameters: None

 

Local variables:

1. plainChar: To hold one plain text character.

2. plainNum: The ASCII value of that character.

3. i : Loop counter

4. asciiBit[0 . . asciiLen-1] : Array to hold the bits of plainNum.

 

Basic Algorithm:

1. Read first character of plaintext into plainChar.

2. While Loop: [Condition: not EOF(plain text file)]

   a. Set plainNum to ASCII value of plainChar.

   b. For Loop: i takes on [asciiLen-1 ® 0] {Convert plainNum to bits}

            i. asciiBit[i] takes on plainNum mod 2.

            ii. plainNum takes on plainNum div 2 {integer division}.

   c. For Loop: i takes on [0 ® asciiLen-1] {Print out bits of plainNum}

            i. Print into plainBits file asciiBit[i].

3. Read next character of plaintext into plainChar.

 

Encode(e,n)

 

Parameters:

1. e: The encrypting key value, e.

2. n: The encrypting key value, n.

 

Local variables:

1. plainInt: The integer value of blockLen bits of the plain text.

2. cipherInt: The encrypted value of plainInt.

 

Basic Algorithm:

1. While Loop: [Condition: not EOF(plain bits file)]

   a. Set plainInt to value returned from call to readPlainBits.

   b. Set cipherInt to value returned from call to powMod(plainInt, e, n).

   c. Print cipherInt into cipher text file.

 

readPlainBits

 

Parameters: None

 

Local variables:

1. plainInt: The integer value of blockLen bits of the plain text.

2. i,j : Loop counters

3. plainBit[0 . . blockLen-1] : The bits read from the plainBits file.

 

Basic Algorithm:

1. If statement: [Condition: not EOF(plain bits file)]

{Needed because it is called from Encode}

   a. For Loop: i takes on [0 ® blockLen-1] {Read blockLen bits}

            i. Read next bit from plainBits file into plainBit[i].

            ii. If statement: [Condition: not EOF(plain bits file)]

    {Needed because plain bits in file may not be a multiple of blockLen}

          s1. For Loop: j takes on [i ® blockLen-1] {Fill with garbage}

                        si. Set plainBit[j] to some arbitrary value, such as 1.

          S2. Exit For Loop

   b. Set plainInt to 0.

   c. For Loop: i takes on [0 ® blockLen-1] {Convert bits to integer}

            i. Set plainInt to plainInt times 2.

            ii. Set plainInt to plainInt plus plainBit[i].

   d. Return plainInt.

 

powMod(base, expt, modulus)

 

Parameters:

1. base: The number being raised to a power.

2. expt: The power to which base is to be raised.

3. modulus: The mod value.

 

Local variables:

1. i: Loop counter.

2. pm: The powMod value as it is computed.

 

Basic Algorithm:

1. Set pm to 1. {Any number to the zeroth power is 1}.

2. For Loop: i takes on [1 ® expt]

   a. Set pm to pm times base mod modulus.

3. Return pm