Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. #task1
  2. from struct import pack
  3. import sys
  4.  
  5.  
  6. def calc_check_sum(s, polyn):
  7.     s += '0' * (len(polyn) - 1)
  8.     i = 0
  9.     res = ""
  10.     while True:
  11.         if res == "0":
  12.             while i < len(s) and s[i] == '0':
  13.                 i += 1
  14.             if i == len(s):
  15.                 return res
  16.             res = s[i]
  17.             i += 1
  18.        
  19.         while i < len(s) and len(res) < len(polyn):
  20.             res += s[i]
  21.             i += 1
  22.         if len(res) < len(polyn):
  23.             return res
  24.            
  25.         res = '{0:b}'.format(int(res, 2) ^ int(polyn, 2))
  26.  
  27.  
  28. def error():
  29.     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")
  30.    
  31.    
  32. def append_bytes(file, addition):
  33.     for c in addition:
  34.         file.write(pack('1B', int(c)))
  35.    
  36.        
  37. def main():
  38.     if len(sys.argv) != 3 or sys.argv[1] not in ['--check', '--build']:
  39.         error()
  40.         return
  41.     file = open(sys.argv[2], 'r+b')
  42.     s = ""
  43.    
  44.     while True:
  45.         cur_byte = file.read(1)
  46.         if not cur_byte:
  47.             break
  48.         s += '{0:b}'.format(ord(cur_byte))
  49.     print("Bytes = ", s, sep="")
  50.    
  51.     polyn = input("Enter the polynom for the CRC\n")
  52.     check_sum = calc_check_sum(s, polyn)
  53.    
  54.     if sys.argv[1] == '--build':
  55.         addition = '0' * (len(polyn) - len(check_sum) - 1) + check_sum
  56.         print("Addition = ", addition, sep="")
  57.        
  58.         append_bytes(file, addition)
  59.         file.close()
  60.    
  61.         print("The Check Sum is succesfully added to the end of the file.")
  62.     else:
  63.         if check_sum == '0':
  64.             print("The file is correct.")
  65.         else:
  66.             print("The file is damaged.\nDelta = ", check_sum)
  67.        
  68.    
  69. if __name__ == "__main__":
  70. main()
  71.  
  72.  
  73.  
  74.  
  75. #task5
  76. import sys
  77.  
  78. def encode(key, cycle):
  79.     for line in sys.stdin:
  80.         for c in line:
  81.             idx = cycle.index(ord(c))
  82.             print(chr(cycle[(idx + key) % len(cycle)]), end="")
  83.         print()
  84.        
  85.  
  86. if __name__ == "__main__":
  87.     cycle = list(range(48, 58)) + list(range(1040, 1104)) + \
  88.         list(range(32, 47)) + list(range(58, 64))
  89.    
  90.     key = int(input("Enter encoding key\n"))
  91.     cipher_type = input("Enter coding type (encode/decode)\n")
  92.     print("Enter message")
  93.    
  94.     for line in sys.stdin:
  95.         for c in line:
  96.             if (ord(c) == 10):
  97.                 continue;
  98.             idx = cycle.index(ord(c))
  99.            
  100.             if (cipher_type[0] == 'e'):
  101.                 idx = (idx + key) % len(cycle)
  102.             else:
  103.                 idx = (idx - key + len(cycle)) % len(cycle)
  104.                
  105.             print(chr(cycle[idx]), end="")
  106. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement