How do I input a file in python .py running code

-4

the code is suppose to decrypt and encrypt a random file, but I can't figure out how to input it.

import array
import hashlib
import random

from Crypto.Cipher import Blowfish


MH3G_JP = 0
MH3G_NA = 1
MH3G_EU = 2
MH4_JP = 3
MH4_NA = 4
MH4_EU = 5
MH4G_JP = 6
MH4G_NA = 7
MH4G_EU = 8


class SavedataCipher:
def __init__(self, game):
    if game in (MH4G_JP, MH4G_NA, MH4G_EU):
        self._cipher = Blowfish.new(b'blowfish key
iorajegqmrna4itjeangmb agmwgtobjteowhv9mope')
    else:
        raise ValueError('Ivalid game selected.')

def _xor(self, buff, key):
    buff = array.array('H', buff)
    for i in range(len(buff)):
        if key == 0:
            key = 1
        key = key * 0xb0 % 0xff53
        buff[i] ^= key
    return buff.tostring()
def encrypt(self, buff):
    csum = sum(buff) & 0xffffffff
    buff = array.array('I', buff)
    buff.insert(0, csum)
    seed = random.getrandbits(16)
    buff = array.array('I', self._xor(buff.tostring(), seed))
    buff.insert(0, (seed << 16) + 0x10)
    buff.byteswap()
    buff = array.array('I', self._cipher.encrypt(buff.tostring()))
    buff.byteswap()
    return buff.tostring()

def decrypt(self, buff):
    buff = array.array('I', buff)
    buff.byteswap()
    buff = array.array('I', self._cipher.decrypt(buff.tostring()))
    buff.byteswap()
    seed = buff.pop(0) >> 16
    buff = array.array('I', self._xor(buff.tostring(), seed))
    csum = buff.pop(0)
    buff = buff.tostring()
    if csum != (sum(buff) & 0xffffffff):
        raise ValueError('Invalid checksum in header.')
    return buff

def encrypt_file(self, savedata_file, out_file):
    savedata = open(savedata_file, 'rb').read()
    savedata = self.encrypt(savedata)
    open(out_file, 'wb').write(savedata)

def decrypt_file(self, savedata_file, out_file):
    savedata = open(savedata_file, 'rb').read()
    savedata = self.decrypt(savedata)
    open(out_file, 'wb').write(savedata)
python
asked on Stack Overflow Jul 5, 2015 by tyo36 • edited Dec 1, 2018 by Cœur

1 Answer

0

This code is a class, you should create a function which calls this calls. You can do that in the same file, or in a different file and import the class.

if __name__ == "__main__":
    cipher = SavedataCipher(MH4G_JP) # or use different parameter
    # do something else
answered on Stack Overflow Jul 5, 2015 by RvdK

User contributions licensed under CC BY-SA 3.0