Crypto Lab 2
Crypto Lab 2
Modern Cryptosystems:
1. Encode a message using DES when encrypted. Try the following experiments and note how they
change the output:
a) Change one character at the end of the message. How much of the encoded message
changes?
CODE:
#include <iostream>
#include <string>
#include <cmath>
string round_keys[16];
string pt;
}
counter++;
}
return decimal;
}
string shift_left_once(string key_chunk){
string shifted="";
shifted += key_chunk[i];
}
shifted += key_chunk[0];
return shifted;
}
string shift_left_twice(string key_chunk)
shifted += key_chunk[j];
}
shifted += key_chunk[0];
key_chunk= shifted;
shifted ="";
}
return key_chunk;
}
string Xor(string a, string b){
string result = "";
right= shift_left_once(right);
}
else{
left= shift_left_twice(left);
right= shift_left_twice(right);
round_key += combined_key[pc2[i]-1];
}
round_keys[i] = round_key;
}
string DES(){
int initial_permutation[64] = {
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7
};
int expansion_table[48] = {
32,1,2,3,4,5,4,5,
6,7,8,9,8,9,10,11,
12,13,12,13,14,15,16,17,
16,17,18,19,20,21,20,21,
22,23,24,25,24,25,26,27,
28,29,28,29,30,31,32,1
};
int substition_boxes[8][4][16]=
{{
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8, 4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
},
{
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9
},
{
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
},
{
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
},
{
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3
},
{
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11, 10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13
},
{
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12
},
{
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
}};
int permutation_tab[32] = {
16,7,20,21,29,12,28,17,
1,15,23,26,5,18,31,10,
2,8,24,14,32,27,3,9,
19,13,30,6,22,11,4,25
};
int inverse_permutation[64]= {
40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29, 36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25
};
perm += pt[initial_permutation[i]-1];
}
right_expanded += right[expansion_table[i]-1];
};
string xored = Xor(round_keys[i], right_expanded);
string res = "";
res += convertDecimalToBinary(val);
temp = right;
right = xored;
left = temp;
}
}
return ciphertext;
}
int main(){
string key=
"1010101010111011000010010001100000100111001101101100110011011101";
pt= "101010111100110111100110101010111100110100010011001001010011011";
generate_keys(key);
cout<<"KEY: "<<key<<endl;
cout<<"Ciphertext: "<<ct<<endl;
OUTPUT:
b) Change one character at the beginning of the message. How much of the encoded
message changes?
OUTPUT:
As seen a small change in the first bit leads to completely different output which is not a relatable to
the previous encoded text. There is huge change in the encoded text which is not predictible since
due to BLOCK CIPHER property of passing from various round the single bit change leads to
completely different output.
As it can be seen that deletion of a bit doesn’t changes the number and the outputs size of the final
encoded text is 64 bit only. Additional bit was added and the text was encrypted.
e) Decrypt a message using a key with one character changed. Does it look anything like the
original?
CODE:
#include <iostream>
#include <string> #include <cmath> using namespace std; string round_keys[16];
string pt; string convertDecimalToBinary(int decimal)
{
string binary;
while(decimal != 0) {
binary = (decimal % 2 == 0 ? "0" : "1") + binary;
decimal = decimal/2;
}
while(binary.length() < 4){
binary = "0" + binary;
}
return binary;
}
int convertBinaryToDecimal(string binary)
{
int decimal = 0;
int counter = 0;
int size = binary.length();
for(int i = size-1; i >= 0; i--)
{
if(binary[i] == '1'){ decimal += pow(2, counter);
}
counter++;
}
return decimal;
}
string shift_left_once(string key_chunk){
string shifted=""; for(int i = 1; i < 28; i++){ shifted += key_chunk[i];
}
shifted += key_chunk[0]; return shifted;
}
string shift_left_twice(string key_chunk){
string shifted=""; for(int i = 0; i < 2; i++){ for(int j = 1; j < 28; j++){ shifted +=
key_chunk[j];
}
shifted += key_chunk[0]; key_chunk= shifted;
shifted ="";
}
return key_chunk;
}
string Xor(string a, string b){
string result = ""; int size = b.size();
for(int i = 0; i < size; i++){
if(a[i] != b[i]){
result += "1";
}
else{
result += "0";
}
}
return result;
}
void generate_keys(string key){
int pc1[56] = {
57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4
};
int pc2[48] = {
14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32
};
string perm_key ="";
for(int i = 0; i < 56; i++){
perm_key+= key[pc1[i]-1];
}
string left= perm_key.substr(0, 28); string right= perm_key.substr(28, 28); for(int i=0;
i<16; i++){ if(i == 0 || i == 1 || i==8 || i==15 ){ left=
shift_left_once(left); right= shift_left_once(right);
}
else{
left= shift_left_twice(left); right=
shift_left_twice(right);
}
string combined_key = left + right; string round_key = "";
for(int i = 0; i < 48; i++){
round_key += combined_key[pc2[i]-1]; }
round_keys[i] = round_key;
}
}
string DES(){
int initial_permutation[64] = { 58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7
};
int expansion_table[48] = { 32,1,2,3,4,5,4,5,
6,7,8,9,8,9,10,11,
12,13,12,13,14,15,16,17,
16,17,18,19,20,21,20,21,
22,23,24,25,24,25,26,27,
28,29,28,29,30,31,32,1
};
int substition_boxes[8][4][16]=
{{
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
},
{
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9
},
{
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8, 13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
},
{
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
},
{
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9, 14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3
},
{
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13
},
{
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12
},
{
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
}};
int permutation_tab[32] = { 16,7,20,21,29,12,28,17,
1,15,23,26,5,18,31,10,
2,8,24,14,32,27,3,9,
19,13,30,6,22,11,4,25
};
int inverse_permutation[64]= { 40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25
};
string perm = ""; for(int i = 0; i < 64; i++){ perm +=
pt[initial_permutation[i]-1];
}
string left = perm.substr(0, 32); string right = perm.substr(32, 32); for(int i=0;
i<16; i++) { string right_expanded = "";
for(int i = 0; i < 48; i++) { right_expanded += right[expansion_table[i]-1];
string xored = Xor(round_keys[i], right_expanded);
string res = ""; for(int i=0;i<8; i++){
string row1= xored.substr(i*6,1) + xored.substr(i*6 + 5,1); int row =
convertBinaryToDecimal(row1);
string col1 = xored.substr(i*6 + 1,1) + xored.substr(i*6 + 2,1) + xored.substr(i*6 +
3,1) + xored.substr(i*6 + 4,1);;
int col = convertBinaryToDecimal(col1); int val =
substition_boxes[i][row][col]; res += convertDecimalToBinary(val);
}
string perm2 ="";
for(int i = 0; i < 32; i++){
perm2 += res[permutation_tab[i]-1];
}
xored = Xor(perm2, left);
generate_keys(key);
cout<<"Key: "<<key<<endl; cout<<"Plain text: "<<pt<<endl;
string ct= DES();
cout<<"Ciphertext: "<<ct<<endl;
As it can be seen there was no change when I changed once character in the key.
2.Create text file sample.txt; encrypt the file sample.txt using a symmetric key. The first time
when code is run, a folder is created. You will be asked to enter a key. The key used is
“infosec”. An encryptedfile is now created in the same location as the plaintext file with the name
“sample1.txt” and sees difference in file. And also perform the Decryption also. (Use AES
algorithm)
CODE:
def aes():
E=(
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
)
IP_INV = (
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
)
P=(
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
)
m1 = 0x123456ABCD142536
msg3 = 0x123456ABCD142537
msg4 = 0x223456ABCD142536
msg5 = 0x123456ABCD14253
key1 = "AABB09182746CCDD"
key2 = "133457799BCCDFF1"
PC1 = (
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
)
PC2 = (
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
)
Sboxes = {
0: (
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
),
1: (
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
),
2: (
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
len(key_bin) key_bin=str(y*str(0))
bit56_key=bit56_key+key_bin[x-
1] l=bit56_key[:28]
r=bit56_key[28:] l11=[]
for i in l:
l11.append(i) def
circularshift1(left_key):
l1=[] for i in
left_key:
l1.append(i)
y=l1[0]
l1.remove(l1[0]
) l1.append(y)
return l1
shifted_keys=[]
shifted_keys.ap
pend(circularsh
ift1(l));shifted_
keys.append(ci
rcularshift1(l))
shifted_keys2=
[] for i in
range(0,170):
shifted_keys.append(circularshift1(shifted_keys[i])) for i in
shifted_keys2.append(circularshift1(shifted_keys2[6]))
shifted_keys4=[] shifted_keys_original_left=[]
shifted_keys3.append(circularshift1(circularshift1(shifted_keys2[-
shifted_keys4.append(shifted_keys3[i])
shifted_keys4.append(circularshift1(shifted_keys4[-1]))
for i in range(len(shifted_keys2)):
shifted_keys_original_left.append(shifted_keys2[i])
for i in range(len(shifted_keys4)):
shifted_keys_original_left.append(shifted_keys4[i])
r11=[]
for i in r:
r11.append(i) def
circularshift2(right_key):
r1.append(i) y=r1[0]
r1.remove(r1[0])
r1.append(y) return r1
shifted_keys_r=[]
shifted_keys_r.append(circ
ularshift2(r));shifted_keys_r
.append(circularshift2(r))
shifted_keys2_r=[] for i in
range(0,170):
shifted_keys_r.append(circularshift2(shifted_keys_r[i])) for i in
shifted_keys2_r.append(circularshift2(shifted_keys2_r[6]))
shifted_keys4_r=[] shifted_keys_original_right=[]
shifted_keys3_r.append(circularshift2(circularshift2(shifted_keys2_r[-
shifted_keys4_r.append(shifted_keys3_r[i]) shifted_keys4_r.append(circularshift2(shifted_keys4_r[-
shifted_keys_original_right.append(shifted_keys2_r[i])
for i in range(len(shifted_keys4_r)):
shifted_keys_original_right.append(shifted_keys4_r[i])
shifted_keys_original=[]
k = 0 for i in
shifted_keys_original_right:
for j in i:
shifted_keys_original_left[k].append(j) k = k+1
shifted_keys_original =
shifted_keys_original_left final_key=[]
q=0 for i in
shifted_keys_original: for j
in PC2:
final_key.append(shifted_keys_original[q][j-
final_key_original=[] for i in
range(0,len(final_key),48):
final_key_original.append(final_key[m:i]
) m=i final_key_original.remove([])
for j in i:
print(j,end=''
) print() IP =
C0 = key >> 28
D0 = key & (2 ** 28 - 1)
L0 = msg_block >> 32
R0 = msg_block & (2 ** 32 - 1)
L_last = L0 R_last =
R0 for i in range(1,
17):
if decrypt:
i = 17 - i
L_round = R_last
R_r = L_last ^ r_fun(R_last, round_keys[i])
L_last = L_round
R_last = R_r
cipher_block = (R_r << 32) + L_round
return cipher_block
enumerate(Ri_blocks):
block_str = bin(block)
range(len(table)):
perm.append(block_str[table[pos] - 1])
enaa2(C0, D0):
in enumerate(lrot_values): i += 1
Ci = lrot(round_keys[i - 1][0], rot_val,
round_keys[0]
@classmethod
def create(cls):
if hasattr(cls, "AES_CREATED"):
return cls.num_rounds = {16: {16: 10, 24: 12, 32: 14}, 24: {16: 12, 24: 12, 32: 14}, 32: {16:
14, 24:
1], [1, 1, 1, 1, 0, 0,
in range(255):
1] if j & 0x100 != 0: j ^=
range(1, 255):
log[alog[i]] = i
== 0 or b == 0:
G = [[2, 1, 1, 3],
[3, 2, 1, 1],
[1, 3, 2, 1],
[1, 1, 3, 2]] AA = [[0] * 8 for i
pivot == 0: t = i + 1 while
t += 1
assert t != 4, 'G matrix must be
in range(8): if AA[i][j] != 0:
for t in range(4):
if i != t: for j in
range(i+1, 8):
[j + 4]
def mul4(a,
bs): if a == 0:
return 0 r = 0
for b in bs: r
<<= 8 if b != 0:
r = r | mul(a,
b) return r
cls.T1 = []
cls.T2 = []
cls.T3 = []
cls.T4 = []
cls.T5 = []
cls.T6 = []
cls.T7 = []
cls.T8 = []
cls.U1 = []
cls.U2 = []
cls.U3 = [] cls.U4 =
[] for t in
range(256):
s = cls.S[t]
cls.T1.append(mul4(s, G[0]))
cls.T2.append(mul4(s, G[1]))
cls.T3.append(mul4(s, G[2]))
cls.T4.append(mul4(s, G[3]))
s = cls.Si[t] cls.T5.append(mul4(s,
cls.T7.append(mul4(s, iG[2]))
cls.T8.append(mul4(s, iG[3]))
cls.U1.append(mul4(t, iG[0]))
cls.U2.append(mul4(t, iG[1]))
cls.U3.append(mul4(t, iG[2]))
mul(2, r) cls.rcon.append(r)
block_size != 32:
self.block_size = block_size
ROUNDS = AES.num_rounds[len(key)][block_size]
BC = int(block_size / 4)
Ke = [[0] * BC for i in range(ROUNDS + 1)]
Kd = [[0] * BC for i in range(ROUNDS + 1)]
ROUND_KEY_COUNT = (ROUNDS + 1) * BC
KC = int(len(key) / 4)
tk = [] for i in
range(0, KC):
3])) t = 0 j = 0
tk[j] j += 1 t += 1
tt = 0
rconpointer = 0
while t < ROUND_KEY_COUNT:
tt = tk[KC - 1] tk[0] ^= (AES.S[(tt >> 16) & 0xFF] &
0xFF) << 24 ^ \
24 rconpointer += 1 if KC != 8:
tk[i] ^= tk[i-1]
j=0
tk[j] j += 1 t += 1
tt = Kd[r][j]
plaintext): if len(plaintext) !=
self.block_size: raise
Ke = self.Ke
BC = int(self.block_size / 4)
ROUNDS = len(Ke) - 1
if BC == 4:
AES.SC = 0
elif BC == 6:
AES.SC = 1
else:
AES.SC = 2 s1 =
AES.shifts[AES.SC][1][0] s2
= AES.shifts[AES.SC][2][0]
s3 = AES.shifts[AES.SC][3]
[0] a = [0] * BC
t = [] for i in
range(BC):
t.append((ord(plaintext[i * 4 ]) << 24
& 0xFF] ^ (tt >> 24)) & 0xFF) result.append((AES.S[(t[(i + s1) % BC] >>
16) & 0xFF] ^ (tt >> 16)) & 0xFF) result.append((AES.S[(t[(i + s2) % BC]
>> 8) & 0xFF] ^ (tt >> 8)) & 0xFF) result.append((AES.S[t[(i + s3) % BC] &
raise ValueError('wrong block length, expected ' + str(self.block_size) + ' got ' +
str(len(ciphertext)))
Kd = self.Kd
BC = int(self.block_size / 4)
ROUNDS = len(Kd) - 1
if BC == 4: AES.SC = 0 elif
BC == 6: AES.SC = 1 else:
AES.SC = 2 s1 =
AES.shifts[AES.SC][1][1] s2
= AES.shifts[AES.SC][2][1]
s3 = AES.shifts[AES.SC][3]
for i in range(BC):
for i in range(BC):
t = copy.deepcopy(a)
i] >> 24) & 0xFF] ^ (tt >> 24)) & 0xFF) result.append((AES.Si[(t[(i + s1) %
BC] >> 16) & 0xFF] ^ (tt >> 16)) & 0xFF) result.append((AES.Si[(t[(i + s2)
% BC] >> 8) & 0xFF] ^ (tt >> 8)) & 0xFF) result.append((AES.Si[t[(i + s3) %
@staticmethod
def test(): def t(kl, bl):
b = 'b' * bl r = AES('a' *
assert x != b assert
r.decrypt(x) == b t(16,
(eeJ]ʅNF+|Q+.^5"""
cipher619="""MrXsKxgogIjAZYjXZRVKDl2wyoWi+06l2EaZK4YH+Y58kgy/UQYrLuTWjLdeNRU
"""
a=("Plain text:- abcdefg1234567890123451111112345 "+"Key:-
cipher619)
print(a)
print(b)
x=open("sample.txt","w")
x.write(a)
y=open("sample1.txt","w")
y.write(c)
def aes2():
k2 = 0xAABB09182735CCDE
k1 = 0xAABB09182735CCDD
E=(
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
)
IP_INV = (
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
)
P=(
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
)
m1 = 0x123456ABCD142536
msg3 = 0x123456ABCD142537
msg4 = 0x223456ABCD142536
msg5 = 0x123456ABCD14253
key1 = "AABB09182746CCDD"
key2 = "133457799BCCDFF1"
PC1 = (
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
)
PC2 = (
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
)
Sboxes = {
0: (
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
),
1: (
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
),
2: (
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
"{0:08b}".format(int(key, 16)) y = 64 -
= []
for i in l:
l11.append(i) def
circularshift1(left_key):
l1 = [] for i in
left_key:
l1.append(i)
y = l1[0] l1.remove(l1[0])
[]
shifted_keys.append(circularshift1(l))
shifted_keys.append(circularshift1(l))
shifted_keys2 = [] for i in range(0,
170):
shifted_keys.append(circularshift1(shifted_keys[i])
break shifted_keys2.append(shifted_keys[i])
shifted_keys2.append(circularshift1(shifted_keys2[6])) l12 =
shifted_keys4 = [] shifted_keys_original_left = []
shifted_keys3.append(circularshift1(circularshift1(shifted_keys2[-
shifted_keys4.append(shifted_keys3[i])
shifted_keys4.append(circularshift1(shifted_keys4[-1]))
for i in range(len(shifted_keys2)):
shifted_keys_original_left.append(shifted_keys2[i]) for i in
range(len(shifted_keys4)):
shifted_keys_original_left.append(shifted_keys4[i])
r11 = []
for i in r:
r11.append(i) def
circularshift2(right_key):
return r1 shifted_keys_r = []
shifted_keys_r.append(circularshift2(r))
shifted_keys_r.append(circularshift2(r))
170):
shifted_keys_r.append(circularshift2(shifted_keys_r[i]))
shifted_keys2_r.append(circularshift2(shifted_keys2_r[6])
shifted_keys3_r = [] shifted_keys4_r = []
shifted_keys_original_right = []
shifted_keys3_r.append(circularshift2(circularshift2(shifted_keys2_r[-
shifted_keys3_r.append(circularshift2(d))
shifted_keys4_r.append(circularshift2(shifted_keys4_r[-1]))
for i in range(len(shifted_keys2_r)):
shifted_keys_original_right.append(shifted_keys2_r[i])
for i in range(len(shifted_keys4_r)):
shifted_keys_original_right.append(shifted_keys4_r[i]
) shifted_keys_original = [] k = 0 for i in
shifted_keys_original_right:
for j in i:
shifted_keys_original_left[k].append(j)
k = k + 1 shifted_keys_original =
i in shifted_keys_original:
for j in PC2:
final_key.append(shifted_keys_original[q][j -
1]) q = q + 1
len(final_key), 48):
final_key_original.append(final_key[m:i])
m=i
final_key_original.remove([]
) print('Keys:-') for i in
final_key_original:
for j in i:
print(j, end='')
print()
IP = (
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
)
def encrypt(msg, key, decrypt=False): assert
C0 = key >> 28
D0 = key & (2 ** 28 - 1)
L0 = msg_block >> 32
R0 = msg_block & (2 ** 32 - 1)
L_last = L0
R_last = R0 for i in
range(1, 17): if
decrypt: i = 17 - i
L_round = R_last
R_r = L_last ^ r_fun(R_last, round_keys[i])
L_last = L_round R_last = R_r cipher_block =
Ri = per_t(Ri, 32, E)
Ri ^= Ki
Ri_blocks = [((Ri & (0b111111 << shift_val)) >> shift_val) for shift_val in (42, 36, 30, 24, 18,
12, 6, 0)] for i, block in
enumerate(Ri_blocks):
block_str = bin(block)[2:].zfill(block_len)
perm = []
perm.append(block_str[table[pos] -
enaa2(C0, D0):
in enumerate(lrot_values): i += 1
round_keys