#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author: feng
from random import randint
import timefrom collections import deque
import codecs
#生成会话秘钥和帧号码
def randomInput(clock_type):
randomClock = 0
counter = 0
if (clock_type == "session_key"):
randomClock = randint(0, (2 ** 65) - 1)
randomClock = "{0:{fill}64b}".format(randomClock, fill='0')
elif (clock_type == "frame_counter"):
randomClock = randint(0, (2 ** 23) - 1)
randomClock = "{0:{fill}22b}".format(randomClock, fill='0')
else:
print("Access Denied!")
return randomClock
#抽头
def tappedBit(shift_register):
result = []
if (shift_register == "lfsr1"):
result = [13, 16, 17, 18]
elif (shift_register == "lfsr2"):
result = [12,16,20, 21]
elif (shift_register == "lfsr3"):
result = [17, 18, 21, 22]
else:
print("Access Denied!")
return result
def clocking(lfsr, tapped_bit, key):
lfsr = deque(lfsr)
for num in range(0, len(key)):
if len(tapped_bit) == 4:
resultXorLfsr = ((int(lfsr[tapped_bit[3]]) ^ int(lfsr[tapped_bit[2]])) ^ int(lfsr[tapped_bit[1]])) ^ int(
lfsr[tapped_bit[0]])
else:
resultXorLfsr = int(lfsr[tapped_bit[1]]) ^ int(lfsr[tapped_bit[0]])
resultFinal = int(key[num]) ^ resultXorLfsr
lfsr.rotate(1)
lfsr[0] = str(resultFinal)
# Validation Item
# print "loop-"+str(num)
# result = ''.join(lfsr)
# print result
return lfsr
# Main Program
lfsr1 = "{0:{fill}19b}".format(0, fill='0') # 19bit
lfsr2 = "{0:{fill}22b}".format(0, fill='0') # 22bit
lfsr3 = "{0:{fill}23b}".format(0, fill='0') # 23bit
print("A5/1 Algorithm\n")
# Inisialisasi LFSR
print("LFSR initiating...\n")
# time.sleep(1)
print(lfsr1)
print(lfsr2)
print(lfsr3)
# Generating Tapped Bit
print("\nTapped Bit generating...\n")
# time.sleep(3)
print("LFSR 1 : " + str(tappedBit("lfsr1")))
print("LFSR 2 : " + str(tappedBit("lfsr2")))
print("LFSR 3 : " + str(tappedBit("lfsr3")))
print("\nStep 1\n")
# Input Plain Text
route=input("please input the path of your file needed crypto:")
with codecs.open(route,'r','ansi') as f:
plainText = f.read()
plainText = ''.join("{0:{fill}8b}".format(ord(x),fill='0') for x in plainText)
plainText = list(plainText)
# Pembuatan session key secara random
print("\nCreating Session Key...\n")
# time.sleep(3)
sessionKey = randomInput("session_key")
print("Session Key Created : " + str(sessionKey) + " \n")
sessionKey = deque(sessionKey)
# XOR-ing session key dengan lfsr menggunakan tapped bit
print("XOR-ing between session key and LFSR with tapped bit\n")
# time.sleep(3)
lfsr1 = clocking(lfsr1, tappedBit("lfsr1"), sessionKey)
print("XOR-ing result on lfsr 1 : " + ''.join(lfsr1))
# time.sleep(1)
lfsr2 = clocking(lfsr2, tappedBit("lfsr2"), sessionKey)
print("XOR-ing result on lfsr 2 : " + ''.join(lfsr2))
# time.sleep(1)
lfsr3 = clocking(lfsr3, tappedBit("lfsr3"), sessionKey)
print("XOR-ing result on lfsr 3 : " + ''.join(lfsr3))
# time.sleep(3)
print("\nStep 2\n")
# Pembuatan frame counter secara random
print("Creating Frame Counter...\n")
# time.sleep(1)
frameCounter = randomInput("frame_counter")
print("Frame Counter Created : " + frameCounter + " \n")
frameCounter = deque(frameCounter)
# XOR-ing frame counter dengan lfsr pada step1 menggunakan tapped bit
print("XOR-ing between frame counter and LFSR with tapped bit\n")
# time.sleep(3)
lfsr1 = clocking(lfsr1, tappedBit("lfsr1"), frameCounter)
print("XOR-ing result on lfsr 1 : " + ''.join(lfsr1))
# time.sleep(1)
lfsr2 = clocking(lfsr2, tappedBit("lfsr2"), frameCounter)
print("XOR-ing result on lfsr 2 : " + ''.join(lfsr2))
# time.sleep(1)
lfsr3 = clocking(lfsr3, tappedBit("lfsr3"), frameCounter)
print("XOR-ing result on lfsr 3 : " + ''.join(lfsr3))
# time.sleep(3)
print("\nStep 3\n")
# Pengacakan lfsr 100 kali irregular clock menggunakan majority bit
majority = "0"
majorityKey = [1]
counterZero = 0
counterOne = 0
# Checking majority bit dan loop irregular clock
print("Irregular clocking of LFSR with majority bit\n")
for num in range(0, 99):
if (lfsr1[8] == majority):
counterZero = counterZero + 1
else:
counterOne = counterOne + 1
if (lfsr2[10] == majority):
counterZero = counterZero + 1
else:
counterOne = counterOne + 1
if (lfsr2[10] == majority):
counterZero = counterZero + 1
else:
counterOne = counterOne + 1
if (counterZero > counterOne):
if (lfsr1[8] == majority):
lfsr1 = clocking(lfsr1, tappedBit("lfsr1"), majorityKey)
if (lfsr2[10] == majority):
lfsr2 = clocking(lfsr2, tappedBit("lfsr2"), majorityKey)
if (lfsr3[10] == majority):
lfsr3 = clocking(lfsr3, tappedBit("lfsr3"), majorityKey)
else:
if (lfsr1[8] != majority):
lfsr1 = clocking(lfsr1, tappedBit("lfsr1"), majorityKey)
if (lfsr2[10] != majority):
lfsr2 = clocking(lfsr2, tappedBit("lfsr2"), majorityKey)
if (lfsr3[10] != majority):
lfsr3 = clocking(lfsr3, tappedBit("lfsr3"), majorityKey)
lfsr1 = clocking(lfsr1, tappedBit("lfsr1"), frameCounter)
print("Irregular clocking result on lfsr 1 : " + ''.join(lfsr1))
lfsr2 = clocking(lfsr2, tappedBit("lfsr2"), frameCounter)
print("Irregular clocking result on lfsr 2 : " + ''.join(lfsr2))
lfsr3 = clocking(lfsr3, tappedBit("lfsr3"), frameCounter)
print("Irregular clocking result on lfsr 3 : " + ''.join(lfsr3))
print("\nStep 4\n")
# Checking majority bit dan loop irregular clock part 2 + creating key stream
print("Irregular clocking of LFSR (part2) with majority bit\n")
keyStream = ""
for num in range(0, 228):
counterZero = 0
counterOne = 0
if (lfsr1[8] == majority):
counterZero = counterZero + 1
else:
counterOne = counterOne + 1
if (lfsr2[10] == majority):
counterZero = counterZero + 1
else:
counterOne = counterOne + 1
if (lfsr2[10] == majority):
counterZero = counterZero + 1
else:
counterOne = counterOne + 1
temp = int(lfsr1[len(lfsr1) - 1]) ^ int(lfsr2[len(lfsr2) - 1]) ^ int(lfsr3[len(lfsr3) - 1])
keyStream = keyStream + str(temp)
if (counterZero > counterOne):
if (lfsr1[8] == majority):
lfsr1 = clocking(lfsr1, tappedBit("lfsr1"), majorityKey)
if (lfsr2[10] == majority):
lfsr2 = clocking(lfsr2, tappedBit("lfsr2"), majorityKey)
if (lfsr3[10] == majority):
lfsr3 = clocking(lfsr3, tappedBit("lfsr3"), majorityKey)
else:
if (lfsr1[8] != majority):
lfsr1 = clocking(lfsr1, tappedBit("lfsr1"), majorityKey)
if (lfsr2[10] != majority):
lfsr2 = clocking(lfsr2, tappedBit("lfsr2"), majorityKey)
if (lfsr3[10] != majority):
lfsr3 = clocking(lfsr3, tappedBit("lfsr3"), majorityKey)
lfsr1 = clocking(lfsr1, tappedBit("lfsr1"), frameCounter)
print("Irregular clocking (part2) result on lfsr 1 : " + ''.join(lfsr1))
lfsr2 = clocking(lfsr2, tappedBit("lfsr2"), frameCounter)
print("Irregular clocking (part2) result on lfsr 2 : " + ''.join(lfsr2))
lfsr3 = clocking(lfsr3, tappedBit("lfsr3"), frameCounter)
print("Irregular clocking (part2) result on lfsr 3 : " + ''.join(lfsr3))
print("\nKey Stream : " + keyStream)
keyStream = list(keyStream)
#加密
print("is cryptoing,please wait......")
startTime=time.time()
chiperText = ""
plainText_length=len(plainText)
key_length=len(keyStream)
for i in range(0,plainText_length):
temp=int(plainText[i])^int(keyStream[i%key_length])
chiperText+=str(temp)
print("encrypto Done,using %f s" % round(time.time() - startTime))
with codecs.open(r'A5加密密文.txt','w','utf-8') as f:
f.write(chiperText)
# 解密
cipher_length=len(chiperText)
startTime=time.time()
print("is decryptoing ,please wait...")
# print(plainText_length)
sign=0
octBit=""
bit_list=[]
for i in range(0,cipher_length):
temp=int(chiperText[i])^int(keyStream[i%key_length])
sign+=1
octBit+=str(temp)
if sign %8==0:
bit_list.append(octBit)
sign=0
octBit=""
chiper_plain=""
for i in bit_list:
chiper_plain+=chr(int(i,2))
print("decrypto Done,using %f s" % round(time.time() - startTime))
with codecs.open(r'A5解密密文.txt','w','utf-8') as f:
f.write(chiper_plain)