samedi 27 mars 2021

How to add randomly generated characters in specific locations in a string?

I am working on a problem that requires me to read a string from the file and:

  1. Reverse it
  2. An integer encryption strength will be defined. It can be set to any integer value between 0 and 255.
  3. Based on the value of encryption strength, some random letters or digits will be inserted between every letter in the reversed string from step 1.

For example, if the strength was set to 1, the word hello can be encrypted to oxlal9elh.

Another example, if the strength is 2, the word hello can be encrypted to oxil4hlrce6rh.

My code works overall fine, but the problem is I get repeated random characters inserted between the letters of string, every time.

Here's my code, kindly help me identify the error.

Code

def encrypt():
    data = "hello"
    content = data[::-1]

    encryption_str = 2
    
    characters = string.ascii_uppercase +string.ascii_lowercase+ 
    string.digits
    
    
    res = (random.choice(characters).join(content[i:i + encryption_str] for i 
    in range(0, len(content)))) #I am stuck here
    
    print(res)
encrypt()

Output

enter image description here enter image description here




Aucun commentaire:

Enregistrer un commentaire