Cryptography  1.0
Implementation of Cryptography Algorithms
Functions | Variables
DESKeygen Namespace Reference

Functions

def binary_string (k)
 BINARY STRING COVERSION. More...
 
def get_x_bit_permutation (k, pc)
 GET PERMUTATION STRING. More...
 
def perform_xor (first_d, second_d)
 
def compression_to_32 (string)
 GET PERMUTATION STRING. More...
 

Variables

list EXPANSION
 This is an implementation of DES Cipher Algorithm DES = Data Encryption Standard STEP 1: create all constants first. More...
 
list IP
 IP = Initial Permutation This is used to permute the plain_text binary form based on the beloe stated table. More...
 
list PC1
 PC1 = Permuted Choice 1 This is used to permute the plain_text binary form based on the below stated table. More...
 
list PC2
 PC2 = Permuted Choice 2 This is used to permute the plain_text binary form based on the below stated table. More...
 
list LEFTROTS
 
list S
 S-Boxes We now do something strange with each group of six bits: we use them as addresses in tables called "S boxes". More...
 
list Reduced_ka_permutation
 
int hexa_scale = 16
 
 key = str(input())
 
 plaintext = str(input())
 
 L = plaintext[0:8]
 
 R = plaintext[8:16]
 
 plain_binary = binary_string(plaintext)
 
 b_key = binary_string(key)
 
 five_six_perm = get_x_bit_permutation(b_key, PC1)
 
list C = []
 
list D = []
 
 first = C[i - 1][0]
 
 second = D[i - 1][0]
 
 Cx = C[i - 1][1:28]+first
 
 Dx = D[i - 1][1:28]+second
 
list key_list = []
 
list hex_key_list = []
 
 initial_permutation = get_x_bit_permutation(plain_binary, IP)
 
 L0 = initial_permutation[0:32]
 
 R0 = initial_permutation[32:64]
 
 L1 = copy(R0)
 
 ER0 = get_x_bit_permutation(R0, EXPANSION)
 
 func_ER0K1 = perform_xor(key_list[0], ER0)
 
 reduced_value = compression_to_32(func_ER0K1)
 
 permuted_reduced_val = get_x_bit_permutation(reduced_value, Reduced_ka_permutation)
 
 R1 = perform_xor(L0, permuted_reduced_val)
 
 final_bit_string = L1+R1
 
 final_res = hex(int(final_bit_string, 2))[2:].upper()
 

Detailed Description

@package DESKeygen
Documentation for this module.

Function Documentation

def DESKeygen.binary_string (   k)

BINARY STRING COVERSION.

Parameters
khexanumber This basically returns a string of 64 bits to the caller
137 def binary_string(k):
138  string = bin(int(k, hexa_scale))[2:].zfill(64)
139  return string
140 
141 
def binary_string(k)
BINARY STRING COVERSION.
Definition: DESKeygen.py:137
def DESKeygen.compression_to_32 (   string)

GET PERMUTATION STRING.

Parameters
stringthe 48-bit key Returns a compressed string of 32-bits using the S-boxes
166 def compression_to_32(string):
167  compressed_val = ''
168  for j in range(0, 8):
169  six_bit = string[j * 6: (j + 1) * 6]
170  bin1 = six_bit[0] + six_bit[5]
171  bin2 = six_bit[1:5]
172  row_val = int(bin1, 2)
173  col_val = int(bin2, 2)
174  # print(str(row_val) + " " + str(col_val))
175  check_val = S[j][row_val * 16 + col_val]
176  compressed_val += (bin(check_val)[2:].zfill(4))
177  return compressed_val
178 
179 
180 # plaintext = '0123456789ABCDEF'
181 # key = '133457799BBCDFF1'
def compression_to_32(string)
GET PERMUTATION STRING.
Definition: DESKeygen.py:166
def DESKeygen.get_x_bit_permutation (   k,
  pc 
)

GET PERMUTATION STRING.

Parameters
khexanumber
pcPermutation Array This basically returns a string formed by the PC array using permutation bits
146 def get_x_bit_permutation(k, pc):
147  new_perm = ""
148  size = len(pc)
149  for it in range(0, size):
150  new_perm += str(k[pc[it]])
151  return new_perm
152 
153 
def get_x_bit_permutation(k, pc)
GET PERMUTATION STRING.
Definition: DESKeygen.py:146
def DESKeygen.perform_xor (   first_d,
  second_d 
)
154 def perform_xor(first_d, second_d):
155  bit_len = len(first_d)
156  xorred_data = ""
157  for it in range(0, bit_len):
158  val = int(first_d[it]) ^ int(second_d[it])
159  xorred_data += (str(val))
160  return xorred_data
161 
162 
def perform_xor(first_d, second_d)
Definition: DESKeygen.py:154

Variable Documentation

DESKeygen.b_key = binary_string(key)
list DESKeygen.C = []
DESKeygen.Cx = C[i - 1][1:28]+first
list DESKeygen.D = []
DESKeygen.Dx = D[i - 1][1:28]+second
DESKeygen.ER0 = get_x_bit_permutation(R0, EXPANSION)
list DESKeygen.EXPANSION
Initial value:
1 = [
2  31, 0, 1, 2, 3, 4,
3  3, 4, 5, 6, 7, 8,
4  7, 8, 9, 10, 11, 12,
5  11, 12, 13, 14, 15, 16,
6  15, 16, 17, 18, 19, 20,
7  19, 20, 21, 22, 23, 24,
8  23, 24, 25, 26, 27, 28,
9  27, 28, 29, 30, 31, 0
10 ]

This is an implementation of DES Cipher Algorithm DES = Data Encryption Standard STEP 1: create all constants first.

The follow lists are values used for encryption

DESKeygen.final_bit_string = L1+R1
DESKeygen.final_res = hex(int(final_bit_string, 2))[2:].upper()
DESKeygen.first = C[i - 1][0]
DESKeygen.five_six_perm = get_x_bit_permutation(b_key, PC1)
DESKeygen.func_ER0K1 = perform_xor(key_list[0], ER0)
list DESKeygen.hex_key_list = []
int DESKeygen.hexa_scale = 16
DESKeygen.initial_permutation = get_x_bit_permutation(plain_binary, IP)
list DESKeygen.IP
Initial value:
1 = [57, 49, 41, 33, 25, 17, 9, 1,
2  59, 51, 43, 35, 27, 19, 11, 3,
3  61, 53, 45, 37, 29, 21, 13, 5,
4  63, 55, 47, 39, 31, 23, 15, 7,
5  56, 48, 40, 32, 24, 16, 8, 0,
6  58, 50, 42, 34, 26, 18, 10, 2,
7  60, 52, 44, 36, 28, 20, 12, 4,
8  62, 54, 46, 38, 30, 22, 14, 6
9  ]

IP = Initial Permutation This is used to permute the plain_text binary form based on the beloe stated table.

DESKeygen.key = str(input())
list DESKeygen.key_list = []
DESKeygen.L = plaintext[0:8]
DESKeygen.L0 = initial_permutation[0:32]
DESKeygen.L1 = copy(R0)
list DESKeygen.LEFTROTS
Initial value:
1 = [
2  1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
3 ]
list DESKeygen.PC1
Initial value:
1 = [56, 48, 40, 32, 24, 16, 8,
2  0, 57, 49, 41, 33, 25, 17,
3  9, 1, 58, 50, 42, 34, 26,
4  18, 10, 2, 59, 51, 43, 35,
5  62, 54, 46, 38, 30, 22, 14,
6  6, 61, 53, 45, 37, 29, 21,
7  13, 5, 60, 52, 44, 36, 28,
8  20, 12, 4, 27, 19, 11, 3]

PC1 = Permuted Choice 1 This is used to permute the plain_text binary form based on the below stated table.

Basically PC1 converts the 64 bits key to 56 bit and values at 8th, 16th bits and soon are removed

list DESKeygen.PC2
Initial value:
1 = [13, 16, 10, 23, 0, 4,
2  2, 27, 14, 5, 20, 9,
3  22, 18, 11, 3, 25, 7,
4  15, 6, 26, 19, 12, 1,
5  40, 51, 30, 36, 46, 54,
6  29, 39, 50, 44, 32, 47,
7  43, 48, 38, 55, 33, 52,
8  45, 41, 49, 35, 28, 31
9  ]

PC2 = Permuted Choice 2 This is used to permute the plain_text binary form based on the below stated table.

Basically PC1 converts the 56 bits key to 48 bit.

DESKeygen.permuted_reduced_val = get_x_bit_permutation(reduced_value, Reduced_ka_permutation)
DESKeygen.plain_binary = binary_string(plaintext)
DESKeygen.plaintext = str(input())
DESKeygen.R = plaintext[8:16]
DESKeygen.R0 = initial_permutation[32:64]
list DESKeygen.Reduced_ka_permutation
Initial value:
1 = [
2  15, 6, 19, 20, 28, 11,
3  27, 16, 0, 14, 22, 25,
4  4, 17, 30, 9, 1, 7,
5  23, 13, 31, 26, 2, 8,
6  18, 12, 29, 5, 21, 10,
7  3, 24
8 ]
DESKeygen.reduced_value = compression_to_32(func_ER0K1)
list DESKeygen.S

S-Boxes We now do something strange with each group of six bits: we use them as addresses in tables called "S boxes".

Each group of six bits will give us an address in a different S box. Located at that address will be a 4 bit number. This 4 bit number will replace the original 6 bits. The net result is that the eight groups of 6 bits are transformed into eight groups of 4 bits (the 4-bit outputs from the S boxes) for 32 bits total.

DESKeygen.second = D[i - 1][0]