Encrypted string to file on Win to be encrypted on Linux using Crypto.Cypher DES ...

Hi,

as part of a project I am encrypting a string as part of my Win
Application and write it to file.
Reading and decrypting the string from Win works without problem, but
when I try to run the decryption part on Linux (encrypted on Win),
I end up with an error, caused by the encrypted string in the file not
read properly (I suppose), i.e. the characters get misinterpreted ...
I can see it in my frontend that writes the encryped string to a simple
wx.TextCtrl ...

So my question:
What is the correct way to open the file and read the encrypted string
so I end up fine doing the decryption part on Linux?

ThanX

Sample follows

···

------
# encryption sample run from windows
# -*- coding: iso-8859-15 -*-

from Crypto.Cipher import DES
import os
import sys

#test: 2 strings
# string 1 whose decryption works on Linux
#serial = "2401623B"
#
# string 2 whose decryption DOES NOT WORK on Linux
serial = "AA4C4DF7"

# make the string a multiple of 8 ... 16 max is OK for now
if len(serial) == 8:
    pass
else:
    while len(serial) < 16:
        serial = serial + "0"
        print serial
        
print "Serial of char length " + str(len(serial))+ ": " + str(serial)

try:
    obj=DES.new('12345678', DES.MODE_ECB)rog
    encrypted_request = obj.encrypt(serial+'01234567')oki
    print encrypted_request
    data = encrypted_request
    rq = file('serial_test.txt','w')
    rq.write(data)
    rq.close()
except:
    print "Crypto error"

--------
# decryption sample run from Linux
# -*- coding: iso-8859-15 -*-

from Crypto.Cipher import DES

# for test purposes:
request_file = open("serial_test.txt", 'r')
encrypted_request = request_file.read()
print "Serial file request encrypted: "
# for test purposes
print str(encrypted_request)

obj=DES.new('12345678', DES.MODE_ECB)
# this throws the error
decrypted_request = obj.decrypt(encrypted_request)
decrypted_request_sanitized = decrypted_request.replace('01234567','')
print "Decrypted request"
print str(decrypted_request_sanitized)

Tobias,

I know little about encryption, and this has nothing to do with wxPython. However:

by default, Python opens files in text mode, which translates line feeds to/from the platform native form, ‘’\r\n’ on Windows, and plain ‘\n’ on *nix. I suspect that’s messign you up.

make sure to open your files, both for reading an writing, with the ‘b’ flag:

outfile = open(the_filename, ‘wb’)

infile = open(the_filename, ‘rb’)

HTH,

-Chris

···

On Thu, Oct 10, 2013 at 3:28 AM, Tobias Weber tobias.weber@roglink.net wrote:

Hi,

as part of a project I am encrypting a string as part of my Win

Application and write it to file.

Reading and decrypting the string from Win works without problem, but

when I try to run the decryption part on Linux (encrypted on Win),

I end up with an error, caused by the encrypted string in the file not

read properly (I suppose), i.e. the characters get misinterpreted …

I can see it in my frontend that writes the encryped string to a simple

wx.TextCtrl …

So my question:

What is the correct way to open the file and read the encrypted string

so I end up fine doing the decryption part on Linux?

ThanX

Sample follows


encryption sample run from windows

-- coding: iso-8859-15 --

from Crypto.Cipher import DES

import os

import sys

#test: 2 strings

string 1 whose decryption works on Linux

#serial = “2401623B”

string 2 whose decryption DOES NOT WORK on Linux

serial = “AA4C4DF7”

make the string a multiple of 8 … 16 max is OK for now

if len(serial) == 8:

pass

else:

while len(serial)  < 16:

    serial = serial + "0"

    print serial

print "Serial of char length " + str(len(serial))+ ": " + str(serial)

try:

obj=DES.new('12345678', DES.MODE_ECB)rog

encrypted_request = obj.encrypt(serial+'01234567')oki

print encrypted_request

data = encrypted_request

rq = file('serial_test.txt','w')

rq.write(data)

rq.close()

except:

print "Crypto error"

decryption sample run from Linux

-- coding: iso-8859-15 --

from Crypto.Cipher import DES

for test purposes:

request_file = open(“serial_test.txt”, ‘r’)

encrypted_request = request_file.read()

print "Serial file request encrypted: "

for test purposes

print str(encrypted_request)

obj=DES.new(‘12345678’, DES.MODE_ECB)

this throws the error

decrypted_request = obj.decrypt(encrypted_request)

decrypted_request_sanitized = decrypted_request.replace(‘01234567’,‘’)

print “Decrypted request”

print str(decrypted_request_sanitized)

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


Christopher Barker, PhD

Python Language Consulting

  • Teaching

  • Scientific Software Development

  • Desktop GUI and Web Development

  • wxPython, numpy, scipy, Cython

Thanks- I will give it a shot and check …

···

Am 10.10.13 18:08, schrieb Christopher
Barker:

Tobias,

      I know little about encryption, and this has nothing to do

with wxPython. However:

      by default, Python opens files in text mode, which

translates line feeds to/from the platform native form,
‘’\r\n’ on Windows, and plain ‘\n’ on *nix. I suspect that’s
messign you up.

      make sure to open your files, both for reading an writing,

with the ‘b’ flag:

outfile = open(the_filename, ‘wb’)

infile = open(the_filename, ‘rb’)

HTH,

-Chris

      On Thu, Oct 10, 2013 at 3:28 AM, Tobias

Weber tobias.weber@roglink.net
wrote:

Hi,

        as part of a project I am encrypting a string as part of my

Win

        Application and write it to file.

        Reading and decrypting the string from Win works without

problem, but

        when I try to run the decryption part on Linux (encrypted on

Win),

        I end up with an error, caused by the encrypted string in

the file not

        read properly (I suppose), i.e. the characters get

misinterpreted …

        I can see it in my frontend that writes the encryped string

to a simple

        wx.TextCtrl ...



        So my question:

        What is the correct way to open the file and read the

encrypted string

        so I end up fine doing the decryption part on Linux?



        ThanX



        Sample follows



        ------

        # encryption sample run from windows

        # -*- coding: iso-8859-15 -*-





        from Crypto.Cipher import DES

        import os

        import sys



        #test: 2 strings

        # string 1 whose decryption works on Linux

        #serial = "2401623B"

        #

        # string 2 whose decryption DOES NOT WORK on Linux

        serial = "AA4C4DF7"



        # make the string a multiple of 8 ... 16 max is OK for now

        if len(serial) == 8:

            pass

        else:

            while len(serial)  < 16:

                serial = serial + "0"

                print serial





        print "Serial of char length " + str(len(serial))+ ": " +

str(serial)

        try:

            obj=DES.new('12345678', DES.MODE_ECB)rog

            encrypted_request = obj.encrypt(serial+'01234567')oki

            print encrypted_request

            data = encrypted_request

            rq = file('serial_test.txt','w')

            rq.write(data)

            rq.close()

        except:

            print "Crypto error"



        --------

        # decryption sample run from Linux

        # -*- coding: iso-8859-15 -*-



        from Crypto.Cipher import DES





        # for test purposes:

        request_file = open("serial_test.txt", 'r')

        encrypted_request = request_file.read()

        print "Serial file request encrypted: "

        # for test purposes

        print str(encrypted_request)







        obj=DES.new('12345678', DES.MODE_ECB)

        # this throws the error

        decrypted_request = obj.decrypt(encrypted_request)

        decrypted_request_sanitized =

decrypted_request.replace(‘01234567’,‘’)

        print "Decrypted request"

        print str(decrypted_request_sanitized)









            --

            You received this message because you are subscribed to

the Google Groups “wxPython-users” group.

            To unsubscribe from this group and stop receiving emails

from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

            For more options, visit [https://groups.google.com/groups/opt_out](https://groups.google.com/groups/opt_out).

    Christopher Barker, PhD



    Python Language Consulting

      - Teaching

      - Scientific Software Development

      - Desktop GUI and Web Development

      - wxPython, numpy, scipy, Cython

  You received this message because you are subscribed to the Google

Groups “wxPython-users” group.

  To unsubscribe from this group and stop receiving emails from it,

send an email to .
For more options, visit .


-- --------------------------------------------------
Tobias Weber
CEO
The ROG Corporation GmbH
Donaustaufer Str. 200
93059 Regensburg
Tel: +49 941 4610 57 55
Fax: +49 941 4610 57 56
Geschäftsführer: Tobias Weber
Registergericht: Amtsgericht Regensburg - HRB 8954
UStID DE225905250 - Steuer-Nr.184/59359
--------------------------------------------------

wxpython-users+unsubscribe@googlegroups.com
https://groups.google.com/groups/opt_out
www.roglink.com