ROTOR MACHINE CIPHER IMPLEMENTATION

profilensama01
ReadMe.zip

a1crypto.py

from __future__ import absolute_import import random import string import time import keyword def shifting_rotor (rotor): rv = list(rotor.values()) rk = list(rotor.keys()) last_shift = rv[0] for i in xrange(1,len(rv)): rotor[rk[i-1]] = rotor[rk[i]] rotor[rk[i]] = last_shift return rotor def rotor_state(rotor1, rotor2): rflag = raw_input(u"\nInspect Rotor Pairs ? (y/n)") if rflag == u'y': print u"\n== Inner Rotor ==" rk = list(rotor1.keys()) for i in xrange (52): print u"{}<->{}".format(rk[i], rotor1[rk[i]]) print u"\n== Outer Rotor ==" rk_ = list(rotor2.keys()) for i in xrange(52): print u"{}<->{}".format(rk_[i], rotor2[rk_[i]]) r1,r2 = [0]*52,[0]*52 j, j_ = 0, 0 print u"Initiating Rotor Pairs ..." time.sleep(1) #*********RANDOM MODULE*********** #Generating random upper and lower case alphabets for rotors while j < 52 or j_ < 52: q = random.choice(random.choice(string.ascii_lowercase)+random.choice(string.ascii_uppercase)) q_ = random.choice(random.choice(string.ascii_lowercase)+random.choice(string.ascii_uppercase)) if not q in r1 and j < 52: r1[j] = q j = j+1 if not q_ in r2 and j_ < 52: r2[j_] = q_ j_ = j_+1 #print("{}{}".format(len(r1), len(r2))) print u"Rotor Pairs Initialised" print u"\n***Direction of Shift is UPWARDS***" #***********MAPPING MODULE************** rotor1, rotor2 = {},{} # MAPPING key value pair like a->F, b->z etc. based on random results for i in xrange (0, 26): rotor1.update({unichr(i+65):r1[i]}) rotor1.update({unichr(i + 97): r1[i+26]}) rotor2.update({unichr(i + 65): r2[i]}) rotor2.update({unichr(i + 97): r2[i + 26]}) flag = u'' #*********ENCRYPTION MODULE************* while not flag == u'n': rotor_state(rotor1, rotor2) s = raw_input(u"String to Encrypt : ") # Validating the input s_split = s.split() valid_flag = 0 #**** split checking each word for validation for i in s_split: if keyword.iskeyword(i): valid_flag = 1 while valid_flag == 1: s = raw_input(u"Reserved Word detected. Re-Enter the String : ") valid_flag = 0 # **** split checking each word for validation for i in s.split(): if keyword.iskeyword(i): valid_flag = 1 # Rotor1 in action s1 = u"" for i in xrange(len(s)): if not s[i] in rotor1: # do not encrypt other thn alphabets s1 += s[i] print u"{}<->{}".format(s[i], s[i]) else: # alphbet encryption from dict by mapping s1 += rotor1[s[i]] print u"{}<->{}".format(s[i], rotor1[s[i]]) #counter += 1 rotor1 = shifting_rotor(rotor1) #shifting every iteration # print(rotor1) print u"Intermediate Encryption: {}".format(s1) # Rotor2 in action s2 = u"" counter = 0 for i in xrange(len(s1)): if not s1[i] in rotor2: # do not encrypt s2 += s1[i] print u"{}<->{}".format(s1[i], s1[i]) else: # encrypt by mapping from the dict s2 += rotor2[s1[i]] print u"{}<->{}".format(s1[i], rotor2[s1[i]]) counter += 1 if counter % 3 == 0: # Shift 1 out of 3 times rotor2 = shifting_rotor(rotor2) # print(rotor1) print u"Final Encryption: {}".format(s2) rotor_state(rotor1, rotor2) flag = raw_input(u"\nRetry Encryption ? (y/n) : ") print u"" print u"Thank You for using Rotor Machine"

makefile

PYTHON = python .PHONY = all help setup run clean all: setup run FILES = a1crypto.py help: @echo "---------------HELP-----------------" @echo "To setup the project type make setup" @echo "To test the project type make test" @echo "To run the project type make run" @echo "------------------------------------" setup: @echo "---------------Setting Up-----------------" @echo "Checking if project files are present..." $ @[ -f ${FILES} ] && (echo "python file exist") || (echo "No python file found, please get it") @echo "-----------------Done-----------------" run: @echo "---------------Running-----------------" ${PYTHON} ${FILES} clean: @echo "---------------Cleaning-----------------" @echo "-----------------Done-------------------"

ReadMe.pdf

PROJECT TITLE

Implementing Rotor Machine Cipher

TECHNOLOGY USED

PYTHON – It is a high-level programming language, objected-oriented, dynamic binding and

dynamic which makes it very attractive for rapid application development.

INSTALLATION

In this assignment python 2.7 is being used for coding as it is compatible with IRIS, but you can

also code more easily with newer version of python. You can follow steps listed below to

download and install python development tool:

1. Select version of python to install

The installation procedure includes downloading and executing python.exe file. To

choose this file you need to consider what you want to do and how.

The version you need to download depends on your coding skills. If you already have

half of coding done than you compulsory need that version and if you are starting from

beginning that you can download any version of python may be latest version.

2. Download python executable installer

(a) Open your web browser and navigate to https://www.python.org/downloads/

(b) Search desired version of python and download executable installer

3. Run executable installer

Double-click on downloaded file and follow steps accordingly to install.

Now you can start using python by searching in menu bar or its icon.

Running Code

Python Code is not needed to be compiled separately like JAVA or C in command line. To run

the python code just type $ python [filename]. You can also run the program via make command

which will automate the running of program and also check for any missing file before

executing.

For running the Rotor Machine Code, the main python file is named a1crypto.py:

1. $ python a1crypto.py

2. $ make

Make sure to be in the same directory as the file before running the code.

It would be recommended to run $make clean at the end of program to release the space

occupied by the file. That is optional step and can be skipped.

ROTOR MACHINE CIPHER IMPLEMENTATION.pdf

ROTOR MACHINE CIPHER IMPLEMENTATION

A Cipher Technique That Uses Different Combination of Mappings

Developed by:

Divyang Sabhadiya (ID: 1274420)

Yash Patel (ID: 1285893)

Nithin Reddy Sama (ID: 1281342)

For:

New York Institute of Technology, Vancouver

October 12, 2020

2

Contents

Question ..................................................................................................................... 3

Test 1 ......................................................................................................................... 4

Test 2 ......................................................................................................................... 6

Test 3 ......................................................................................................................... 9

3

• What can you say about the computational complexity of your algorithm relative to the size of the input string?

Answer: Considering input length to be n, complexity of the encryption algorithm would be n*n + n*( n / 3 ) as the rotor1 shifts every time and produces Intermediate Encrypted String which is fed to rotor2 that produces final Encrypted String but shifts 1 of 3 encounters of character. Hence, the complexity of algorithm would be O(n^2).

Example:

i/p String: EEE

Rotor 1

1st ‘E’ = 1 mapping + 3 rotor1 Shift

2nd ‘E’ = 1 mapping + 3 rotor1 Shift

3rd ‘E’ = 1 mapping + 3 rotor1 Shift

Total = 9 shifts i.e n^2

Note: not counting mappings as it is linear process while rotor shifts are done in loop. We can add mapping count to complexity evaluation which would give us n + n^2, but as n^2 has higher power we consider that as Big-O Notation.

Same for rotor2 but rotor shifts are 1/3rd => n*(n/3)

Adding both complexities => n^2 + (n^2/3) = O(n^2)

4

Test – 1

1) Output the state of your cylinders.

5

2) Encrypt the string “A” and output its encrypted value

3) Output the state of your cylinders.

6

Test - 2

1) Output the state of your cylinders.

7

2) Encrypt the string “EE” and output its encrypted value.

3) Output the state of your cylinders.

8

4) Is a rotor machine a monoalphabetic or polyalphabetic cipher? Comment based

on the results above.

Answer: Monoalphabetic. As the program maps each single character based on rotor pairs, it is

monoalphabetic. Moreover, after each mapping, 1st rotor shifts and all the pair changes. Same is

with 2nd rotor, but it occurs after every 3rd mapping from last rotor. The program maps each

character from 1st and creates an intermediate encryption which is then fed to the second rotor,

character by character.

9

Test – 3

1) Output the state of your cylinders.

10

2) Encrypt the string “Mr. Jock, TV quiz PhD, bags few lynx” and output its

encrypted value.

11

3) Output the state of your cylinders.

12

4) Comment on the state of your cylinders before and after encryption. Please provide an

explanation for what you observe.

Answer: Both Cylinders are randomly assigned with some alphabetical value. Later, a string is

given input to rotor1(1st Cylinder) character by character. On every successful mapping

of character, Cylinder 1 makes an upward shift changing all the mapping pairs. Only

similarity after the shift would be that, every index now has the mapping value as it was

with its following neighbour before the shift. Obviously, value of 1st index will be

transferred to the last index in the Cylinder. The process is exactly same, only difference

being, shifts occurs after every 3rd mapped character. This creates enough confusion in

the encrypted string that even for the same character we would get different encrypted

character. The encryption is different for each character no matter if it is appearing

consecutively like “EEE” or anywhere in the string without concerning how many times

it occurs in the string.