Bilal Ahmed Awan
Bilal Ahmed Awan

Reputation: 19

Python print() and sys.stdout.write() issues

Guys see below code in python 2.7. I'm having issues with the "print(ciphertext)" and the "sys.stdout.write(ciphertext)" parts of the code when I run the code the "print(passline) and the "sys.stdout.write(passline)" come out fine i.e. if the file has a line that says "Billz" it will show as is but when I try to output using either function(i.e. sys.stdout.write() and print()) the ciphertext(via the encryptMessage(key, message) method) the output splits across lines depending on the "myKey" variable (see below for code and example)

*I understand the limitations of the transposition encryption method, but the 'ciphertext' going to a new line before the original line has finished outputting the line from the line it started from

I think the problem is with the encryptMessage() function and how it interacts with the enc() method, i.e. the for ...in... block of the code in particular

Does that make sense?

i think the answer to this can help when -reading data from files but not overwriting those files -when trying to code programmes related to logs, password/word lists -and understand how the for, in and .join works together

i.e. myKey = 1
C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 1
This is a list of Passwords to be encrypted

This is a list of Passwords to be encrypted

Billz786

Billz786

123456

123456

Milly

Milly

Bilklzcfvcx
Bilklzcfvcx

i.e. myKey = 2

C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 2
This is a list of Passwords to be encrypted

Ti sals fPswrst eecytdhsi  ito asod ob nrpe

Billz786

Blz8
il76
123456

135
246
Milly

Mlyil

Bilklzcfvcx
Bllcvxikzfc

i.e. myKey = 4

C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 4
This is a list of Passwords to be encrypted

T asfsrtecthi t sdo reisl Pws eyds ioao bnp

Billz786

Bz
i7l8l6
123456

15263
4
Milly

Myi
ll
Bilklzcfvcx
Blvizclcxkf

i.e. myKey = 8
C:\Users\baawan\Desktop\Cyber Sec\COMP_lang\python>py cypher7.py
would you like to Encrypt(e) or Decrypt(d): e
Enter file name: pass.txt
Enter Key: 8
This is a list of Passwords to be encrypted

Tafreth  d eilPsedsia n
 sstcitsors w y oobp
Billz786

B
illz786
123456

123456

Milly

Milly

Bilklzcfvcx
Bviclxklzcf

the code is

def enc():
    myMessage = raw_input('Enter file name: ')
    myKey = int(raw_input('Enter Key: '))
    text_file = open(myMessage, "r")
    lines = text_file.readlines()
    for passline in lines:
        myMessage = passline
        ciphertext = encryptMessage(myKey, myMessage)
        print(passline)
        #sys.stdout.write(passline)
        print ciphertext
        #sys.stdout.write(ciphertext)
    text_file.close()
def encryptMessage(key, message):
    ciphertext = [''] * key
    for col in range(key):
        pointer = col
        while pointer < len(message):
            ciphertext[col] += message[pointer]
            pointer += key
    return ''.join(ciphertext)

Upvotes: 0

Views: 526

Answers (1)

Mads Marquart
Mads Marquart

Reputation: 500

When using readlines and most other ways of reading lines in files, python includes the newline character(s) in the line (So, in your case, passline contains (a) newline character(s)). To prevent this, use something like passline = passline.rstrip('\n\r') in the start of your for loop

Upvotes: 0

Related Questions