Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #task1
- from struct import pack
- import sys
- def calc_check_sum(s, polyn):
- s += '0' * (len(polyn) - 1)
- i = 0
- res = ""
- while True:
- if res == "0":
- while i < len(s) and s[i] == '0':
- i += 1
- if i == len(s):
- return res
- res = s[i]
- i += 1
- while i < len(s) and len(res) < len(polyn):
- res += s[i]
- i += 1
- if len(res) < len(polyn):
- return res
- res = '{0:b}'.format(int(res, 2) ^ int(polyn, 2))
- def error():
- print("The arguments are expected...\n--check [file] to check is the checksum correct according to the CRC-8\n--build [file] to calculate checksum and add it to the end of the file")
- def append_bytes(file, addition):
- for c in addition:
- file.write(pack('1B', int(c)))
- def main():
- if len(sys.argv) != 3 or sys.argv[1] not in ['--check', '--build']:
- error()
- return
- file = open(sys.argv[2], 'r+b')
- s = ""
- while True:
- cur_byte = file.read(1)
- if not cur_byte:
- break
- s += '{0:b}'.format(ord(cur_byte))
- print("Bytes = ", s, sep="")
- polyn = input("Enter the polynom for the CRC\n")
- check_sum = calc_check_sum(s, polyn)
- if sys.argv[1] == '--build':
- addition = '0' * (len(polyn) - len(check_sum) - 1) + check_sum
- print("Addition = ", addition, sep="")
- append_bytes(file, addition)
- file.close()
- print("The Check Sum is succesfully added to the end of the file.")
- else:
- if check_sum == '0':
- print("The file is correct.")
- else:
- print("The file is damaged.\nDelta = ", check_sum)
- if __name__ == "__main__":
- main()
- #task5
- import sys
- def encode(key, cycle):
- for line in sys.stdin:
- for c in line:
- idx = cycle.index(ord(c))
- print(chr(cycle[(idx + key) % len(cycle)]), end="")
- print()
- if __name__ == "__main__":
- cycle = list(range(48, 58)) + list(range(1040, 1104)) + \
- list(range(32, 47)) + list(range(58, 64))
- key = int(input("Enter encoding key\n"))
- cipher_type = input("Enter coding type (encode/decode)\n")
- print("Enter message")
- for line in sys.stdin:
- for c in line:
- if (ord(c) == 10):
- continue;
- idx = cycle.index(ord(c))
- if (cipher_type[0] == 'e'):
- idx = (idx + key) % len(cycle)
- else:
- idx = (idx - key + len(cycle)) % len(cycle)
- print(chr(cycle[idx]), end="")
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement