Decode using RSA
Algorithm for decoding cipher text encoded with RSA
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. d: The decrypting key value, e.
2. n: The decrypting key value, n.
Basic Algorithm:
1. Read d and n.
2. Call cipherToBits(d, n) to convert integers of cipher text to a plain text bit stream.
3. Close plainBits file for writing and open it for reading.
4. Call writeChars to write out the recovered plain text.
cipherToBits(d, n)
Parameters: None
Local variables:
1. cipherInt: To hold one cipher text integer.
2. plainInt: To hold one recovered plain text integer.
3. i : Loop counter
4. plainBit[0 . . blockLen-1] : Array to hold the bits of plainInt.
Basic Algorithm:
1. While Loop: [Condition: not EOF(cipher text file)]
a. Read next integer from cipher text file into cipherInt.
b. Set plainInt to value returned from call to powMod(cipherInt, d, n).
c. For Loop: i takes on [blockLen-1 ® 0] {Convert plainInt to bits}
i. plainBit[i] takes on plainInt mod 2.
ii. plainInt takes on plainInt div 2 {integer division}.
c. For Loop: i takes on [0 ® blockLen-1] {Print out bits of plainNum}
i. Print into plainBits file plainBit[i].
writeChars
Parameters: None
Local variables:
1. asciiInt: The integer value of asciiLen bits of the plain text.
2. i,j : Loop & bit counters
3. asciiBit[0 . . asciiLen-1] : The bits read from the plainBits file.
Basic Algorithm:
1. While Loop: [Condition: not EOF(plain bits file)]
a. Set i to 0 {Start counting number of bits read}
i. While Loop: [Condition: not EOF(plain bits file) & i < asciiLen]
s1. Read next bit into asciiBit[i].
S2. Increment i.
ii. If statement: [Condition: i < asciiLen]
{Needed because plain bits in file may not be a multiple of asciiLen, i.e,. we
ran out of bits to fill the last integer}
s1. For Loop: j takes on [i ® asciiLen-1] {Fill with garbage}
si. Set asciiBit[j] to some arbitrary value, such as 1.
b. Set asciiInt to 0 {Prepare to calculate value of asciiInt }
c. For Loop: i takes on [0 ® asciiLen-1] { calculate value of asciiInt}
i. Set asciiInt to asciiInt times 2 plus asciiBit[i].
d. Print to the plain text file the character represented by asciiInt.
powMod(base, expt, modulus)
Note: Same as for encoding