File line encryption

I am trying to write a program that opens a text file and shifts each of the characters in the file 5 characters to the right. He should do this only for alphanumeric characters and leave nonalphanumerics as they are. (for example: C becomes H) I have to use an ASCII table for this, and I have a problem when characters are wrapped. ex: w should become b, but my program gives me the character that is in the ASCII table. Another problem I am facing is that all characters are printed on separate lines, and I would like all of them to be printed on the same line. I can not use lists or dictionaries.

This is what I have, I'm not sure how to make the final if statement

def main(): fileName= input('Please enter the file name: ') encryptFile(fileName) def encryptFile(fileName): f= open(fileName, 'r') line=1 while line: line=f.readline() for char in line: if char.isalnum(): a=ord(char) b= a + 5 #if number wraps around, how to correct it if print(chr(c)) else: print(chr(b)) else: print(char) 
+4
source share
3 answers

Using str.translate :

 In [24]: import string In [25]: string.uppercase Out[25]: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' In [26]: string.uppercase[5:]+string.uppercase[:5] Out[26]: 'FGHIJKLMNOPQRSTUVWXYZABCDE' In [27]: table = string.maketrans(string.uppercase, string.uppercase[5:]+string.uppercase[:5]) In [28]: 'CAR'.translate(table) Out[28]: 'HFW' In [29]: 'HELLO'.translate(table) Out[29]: 'MJQQT' 
+7
source

First, it matters if it has lower or upper case. I'm going to assume here that all characters are lowercase (if they weren’t, it would be easy enough to make them)

 if b>122: b=122-b #z=122 c=b+96 #a=97 

w = 119 in ASCII and z = 122 (decimal in ASCII), so 119 + 5 = 124 and 124-122 = 2, which is our new b, then we add this to a-1 (this takes care if we get 1 back, 2 + 96 = 98 and 98 - b.

To print on the same line instead of printing when you have it, I would write them to a list and then create a line from that list.

but instead

  print(chr(c)) else: print(chr(b)) 

I would do

  someList.append(chr(c)) else: somList.append(chr(b)) 

then combine each item in the list together on one line.

+1
source

You can create a dictionary to process it:

 import string s = string.lowercase + string.uppercase + string.digits + string.lowercase[:5] encryptionKey = {s[i]:s[i+5] for i in range(len(s)-5)} 

The final addition to s ( + string.lowercase[:5] ) adds the first 5 letters to the key. Then we use a simple understanding of the word to create an encryption key.

Put in your code (I also changed it so that you f.readline() over the lines and not use f.readline() :

 import string def main(): fileName= input('Please enter the file name: ') encryptFile(fileName) def encryptFile(fileName): s = string.lowercase + string.uppercase + string.digits + string.lowercase[:5] encryptionKey = {s[i]:s[i+5] for i in range(len(s)-5)} f= open(fileName, 'r') line=1 for line in f: for char in line: if char.isalnum(): print(encryptionKey[char]) else: print(char) 
0
source

All Articles