Wednesday, March 2, 2022

Implementation of GSM security algorithms (a3/a5/a8) code python source code

 Hello !!

For many other Language Programs of Computer Network, Mobile Computing, microprocessor 8086, C++ , Data Structures , etc and MSBTE diploma,BCA,MCA and engineering related concepts visit our  BLOG

We will see the Implementation of GSM security algorithms program code(A3/A5/A8) using Python  Programming Language in today's post.

Theory:-

  • The term GSM stands for Global System for Mobile Communications.
  • Mobile communications have to communicate using the global positioning for providing the user with better and efficient communication service by protecting against unauthorized access a privacy of mobile subscribers against  any kind of eavesdropping.
  • The system of GSM provides a way of ciphering the text which is an crucial way to protect user from evesdropping . and for the whole ciphering process we require the TSMI i.e.,Temporary Subscriber Identity. there's is a 4 to 8 digit PIN asssociated to validate the identity of the subscribers. The PIN here stands for Password Identification Number.
  • The way this whole process is carried out is shown as follows accoring to the steps:
    • At the time of service provisioning the IMSI, 
    • individual subscriber authentication key (Ki), 
    • authentication algorithm (A3) performed
    • cipher key generation algorithm (A8) performed
    • encryption algorithm (A5) performed

As we understood the background of the GSM and their use. In the above theory all the steps of GSM process are shown, we will be moving further at the coding part.

Explanation of the source codes:-
             
Algorithm of A3 i.e the first procedure of  authentication is carried out by this algorithm. It is used to authticate the identity of bothe the subscribers connected in the network.The keys are 128-bit and we use the RES algorithm of cryptography here to send the authentication information to each other.

Algorithm of A8 i.e. the process for generation of cipher text also called as the process of encrypting the text into secret form which prevents the attacker to spoof the data.
 

Source code in Python for A3 Algorithm of GSM:-

import random  

k=random.getrandbits(128)  

m=random.getrandbits(128)  

kb=bin(k)[6:] 

mb=bin(m)[4:]  

kbl=kb[0:64]  

kbr=kb[64:]  

mbl=mb[0:64]  

mbr=mb[64:] 

a1=int(kbl,2) 

int(mbr,2)  

a2=int(kbr,2) 

int(mbl,2)  

a3=a1^a2  

a4=bin(a3)[2:].zfill(64)  

a5=a4[0:32] 

a6=a4[32:] 

a7=int(a5,2) 

int(a6,2)  

print("128 Bit Key = ",kb) 

print("128 Random Bits Generated = ",mb)

print("RES/SRES =",bin(a7)[2:].zfill(len(a5)))



Source code in Python for A8 Algorithm of GSM:-

 

import re import  

copy import sys

reg_x_length = 19 

reg_y_length = 22 

reg_z_length = 23 

key_one = ""  

reg_x = []  

reg_y = [] 

reg_z = [] 

def loading_registers(key): i = 0 

while(i < reg_x_length): 

reg_x.insert(i, int(key[i])) 

i = i + 1 

j = 0 

p = reg_x_length while(j < 

reg_y_length): 

reg_y.insert(j,int(key[p])) p = p + 1 

j = j + 1 

k = reg_y_length + reg_x_length r = 0 

while(r < reg_z_length): 

reg_z.insert(r,int(key[k])) k =  

k + 1 

r = r + 1 

def set_key(key): 

if(len(key) == 64 and re.match("^([01])+", key)): key_one=key 

loading_registers(key) return True 

return False 

def get_key(): 

return key_one 

def to_binary(plain): s = "" 

i = 0 

for i in plain: 

binary = str(' '.join(format(ord(x), 'b') for x in i)) j = len(binary) 

while(j < 8): 

binary = "0" + binary s = 

s + binary 

j = j + 1  

binary_values = [] 

k = 0 

while(k < len(s)): 

binary_values.insert(k, int(s[k])) 

k = k + 1 return  

binary_values


def get_majority(x,y,z):


if(x + y + z > 1): 

return 1 

else: 

return 0 

def get_keystream(length): 

reg_x_temp = copy.deepcopy(reg_x)  

reg_y_temp = copy.deepcopy(reg_y)  

reg_z_temp = copy.deepcopy(reg_z)  

keystream = [] 

i = 0 

while i < length: 

majority = get_majority(reg_x_temp[8], reg_y_temp[10], 

reg_z_temp[10]) 

if reg_x_temp[8] == majority: 

new = reg_x_temp[13] ^ reg_x_temp[16] ^ reg_x_temp[17] 

^ reg_x_temp[18] 

reg_x_temp_two = copy.deepcopy(reg_x_temp) j = 1 

while(j < len(reg_x_temp)): 

reg_x_temp[j] = reg_x_temp_two[j-1] j = j +  

reg_x_temp[0] = new 

if reg_y_temp[10] == majority: 

new_one = reg_y_temp[20] ^ reg_y_temp[21]  

reg_y_temp_two = copy.deepcopy(reg_y_temp) k = 1 

while(k < len(reg_y_temp)): 

reg_y_temp[k] = reg_y_temp_two[k-1] k = k  

+ 1 

reg_y_temp[0] = new_one 

if reg_z_temp[10] == majority: 

new_two = reg_z_temp[7] ^ reg_z_temp[20] ^ reg_z_temp[21] ^ 

reg_z_temp[22] 

reg_z_temp_two = copy.deepcopy(reg_z_temp) m =  

while(m < len(reg_z_temp)): 

reg_z_temp[m] = reg_z_temp_two[m-1] m =  

m + 1 

reg_z_temp[0] = new_two 

keystream.insert(i, reg_x_temp[18] ^ reg_y_temp[21] ^ 

reg_z_temp[22]) 

i = i + 1 

return keystream


def convert_binary_to_str(binary): s = "" 

length = len(binary) - 8 i = 0 

while(i <= length): 

s = s + chr(int(binary[i:i+8], 2)) i = i + 8 

return str(s) 

def encrypt(plain): 

s = "" 

binary = to_binary(plain) 

keystream = get_keystream(len(binary)) i = 0 

while(i < len(binary)): 

s = s + str(binary[i] ^ keystream[i]) i = i + 1 

return s 

def decrypt(cipher): 

s = "" 

binary = [] 

keystream = get_keystream(len(cipher)) i = 0 

while(i < len(cipher)): 

binary.insert(i,int(cipher[i])) 

s = s + str(binary[i] ^ keystream[i]) i = i + 1 

return convert_binary_to_str(str(s)) 

def user_input_key(): 

tha_key = str(input('Enter a 64-bit key: ')) 

if (len(tha_key) == 64 and re.match("^([01])+", tha_key)): return 

tha_key 

else:  

while(len(tha_key) != 64 and not re.match("^([01])+", if 

tha_key)):  

(len(tha_key) == 64 and re.match("^([01])+", 

tha_key)): 

return tha_key 

tha_key = str(input('Enter a 64-bit key: ')) 

return tha_key 

def user_input_choice(): 

someIn = str(input('[0]: Quit\n[1]: Encrypt\n[2]: Decrypt\nPress 0, 1, or 2: ')) if (someIn == '0' or someIn == '1' or someIn == '2'): return someIn


else: 

while(someIn != '0' or someIn != '1' or someIn != '2'): 

if (someIn == '0' or someIn == '1' or someIn == 

'2'): return someIn 

someIn = str(input('[0]: Quit\n[1]: Encrypt\n[2]: 

Decrypt\nPress 0, 1, or 2: ')) return 

someIn 

def user_input_plaintext(): try: 

someIn = str(input('Enter the plaintext: ')) 

except: 

someIn = str(input('Try again: ')) 

return someIn 

def user_input_ciphertext(): 

ciphertext = str(input('Enter a ciphertext: ')) if 

(re.match("^([01])+", ciphertext)): 

return ciphertext 

else: 

while(not re.match("^([01])+", ciphertext)): 

if (re.match("^([01])+", ciphertext)):  

return ciphertext 

ciphertext = str(input('Enter a ciphertext: '))

return ciphertext 

def main_fun(): 

key = str(user_input_key())  

set_key(key) 

first_choice = user_input_choice() if(first_choice == 

'0'): 

print('Have an awesome day!!!') sys.exit(0) 

elif(first_choice == '1'): 

plaintext = str(user_input_plaintext())  

print(plaintext) print(encrypt(plaintext)) 

elif(first_choice == '2'): 

ciphertext = str(user_input_ciphertext()) print(decrypt(ciphertext)) 

main_fun()



No comments:

Post a Comment