1

I am making using linear congruential generator to generate the keystream and then I will use this keystream to encrypt and decrypt the plaintext.I am trying to generate 47 pseudorandom number the Plaintext will be "Meetmeattownhallatsevenpmforpaymentandbringbear" The length of the keystream need to be the same as length of the plaintext. Then we map the numbers generated to the alphabets in the look-up alphabet.

alphabet of letters

I am creating a python script to perform the Linear congruential generator to produce the keystream. I am attempting to encrypt the plaintext, "Get ready to meet at town hall at 5pm". I am having trouble doing this. I am using the algorithm for the linear congruential generator below

Xn+1 = (aXn + c) mod m

where Xn is the sequence of pseudorandom values, and

• Modulus: m, 0 < m
• Multiplier: a, 0 < a < m
• Increment: c, 0 ≤ c < m
• Seed: X0, 0 ≤ X0 < m

Python Code

# Initialize the seed state
#!/usr/bin/env python#!/usr/bin/env python3
def linearCongruentialGenerator(Xo, m, a, c,randomNums,noOfRandomNums):

# Initialize the seed state
randomNums[0] = Xo

# Traverse to generate required
# numbers of random numbers
for i in range(1, noOfRandomNums):
     
    # Follow the linear congruential method
    randomNums[i] = ((randomNums[i - 1] * a) +
                                     c) % m

#Main function
if __name__ == '__main__':
 
# Seed value
Xo = 27
 
# Modulus parameter
m = 100
 
# Multiplier term
a = 17
 
# Increment term
c = 43

# Number of Random numbers
# to be generated
noOfRandomNums = 47

#Variable to declare the randomnumber
randomNums = [0] * (noOfRandomNums)

#Call the function
linearCongruentialGenerator(Xo, m, a, c,randomNums,noOfRandomNums)

# Print the generated random numbers
for i in randomNums:
    print(i, end = " ")

I want the output to be something like this.

Output of the script

Could you kindly help me in the linear congruential generator python to generate keystream to encrypt and decrypt the plaintext and how to map the numbers generated to the characters of the plaintext.

Thank you

1 Answers1

0

Your implementation of Linear Congruential Generator looks correct.


So remaining task is mapping. You can use zip to create mapping of characters with random numbers.

plainttext = "randomtext"

unique = set(plainttext)

random_numbers = [37, 11, 23, 41, 28, 19, 27, 9, 31]

mapping = dict(zip(unique, random_numbers))
print(mapping)
{'o': 37, 'm': 11, 'a': 23, 'e': 41, 'x': 28, 'd': 19, 'r': 27, 'n': 9, 't': 31}
nobleknight
  • 755
  • 6
  • 15
  • Hi @nobleknight, thank for the assistance. I am not clear on your code below regarding the mapping. Could you kindly use the plaintext provided by me and explain on how to use keystream generated by LCG and used it to encrypt it to get a cipher text? I am very weak in this. – bully_programming May 22 '21 at 09:02
  • I just extracted unique characters from plaintext using `unique = set(plainttext)`, so that we can assign a mapping to each unique character in plaintext. – nobleknight May 22 '21 at 10:02
  • Your plaintext contains the digit '5'. You may want to extend your alphabet to cover 36 characters: a..z, 0..9 and adjust your LCG to cover the range 0..35 to match. – rossum May 22 '21 at 10:29