Docsity

monoalphabetic cipher, Assignments of Cybercrime, Cybersecurity and Data Privacy

Toggle sidebar
bg1
MONO:
class Main {
static String encoder(char[] key) {
String encoded = "";
boolean[] arr = new boolean[26];
for (int i = 0; i < key.length; i++) {
if (key[i] >= 'A' && key[i] <= 'Z') {
if (arr[key[i] - 65] == false) {
encoded += (char) key[i];
arr[key[i] - 65] = true; }}
else if (key[i] >= 'a' && key[i] <= 'z') {
if (arr[key[i] - 97] == false) {
encoded += (char) (key[i] - 32);
arr[key[i] - 97] = true; }}}
for (int i = 0; i < 26; i++) {
if (arr[i] == false) {
arr[i] = true;
encoded += (char) (i + 65); } }
return encoded; }static String cipheredIt(String msg,
String encoded)
{
String cipher = "";
for (int i = 0; i < msg.length(); i++)
{
if (msg.charAt(i) >= 'a' && msg.charAt(i) <= 'z')
{
int pos = msg.charAt(i) - 97;
cipher += encoded.charAt(pos);
}
else if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z')
{
int pos = msg.charAt(i) - 65;
cipher += encoded.charAt(pos);
}
else
{
cipher += msg.charAt(i);
}
}
return cipher;
}
public static void main(String[] args)
{
String key;
key = "Computer";
System.out.println("Keyword : " + key);
String encoded = encoder(key.toCharArray());
String message = "GeeksforGeeks";
System.out.println("Message before Ciphering : " +
message);
System.out.println("Ciphered Text : " +
cipheredIt(message,
encoded));
}
}
OUTPUT:
Keyword : Computer
Message before Ciphering : GeeksforGeeks
Ciphered Text : EUUDNTILEUUDN
CEASER CYPHER:
def encrypt(string, shift):
cipher = ''
for char in string:
if char == ' ':
cipher = cipher + char
elif char.isupper():
cipher = cipher + chr((ord(char) + shift - 65) % 26 +
65)
else:
cipher = cipher + chr((ord(char) + shift - 97) % 26 +
97)
return cipher
text = input("enter string: ")
s = int(input("enter shift number: "))
print("original string: ", text)
print("after encryption: ", encrypt(text, s))
output:
enter string: hii how
enter shift number: 5
original string: hii how
after encryption: mnn mtb
AFFINE :
def egcd(a, b):
x,y, u,v = 0,1, 1,0
while a != 0:
q, r = b//a, b%a
m, n = x-u*q, y-v*q
b,a, x,y, u,v = a,r, u,v, m,n
gcd = b
return gcd, x, y
def modinv(a, m):
pf3
pf4
pf5
pf8

Partial preview of the text

Download monoalphabetic cipher and more Assignments Cybercrime, Cybersecurity and Data Privacy in PDF only on Docsity!

MONO:

class Main { static String encoder(char[] key) { String encoded = ""; boolean[] arr = new boolean[26]; for (int i = 0; i < key.length; i++) { if (key[i] >= 'A' && key[i] <= 'Z') { if (arr[key[i] - 65] == false) { encoded += (char) key[i]; arr[key[i] - 65] = true; }} else if (key[i] >= 'a' && key[i] <= 'z') { if (arr[key[i] - 97] == false) { encoded += (char) (key[i] - 32); arr[key[i] - 97] = true; }}} for (int i = 0; i < 26; i++) { if (arr[i] == false) { arr[i] = true; encoded += (char) (i + 65); } } return encoded; }static String cipheredIt(String msg, String encoded) { String cipher = ""; for (int i = 0; i < msg.length(); i++) { if (msg.charAt(i) >= 'a' && msg.charAt(i) <= 'z') { int pos = msg.charAt(i) - 97; cipher += encoded.charAt(pos); } else if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z') { int pos = msg.charAt(i) - 65; cipher += encoded.charAt(pos); } else { cipher += msg.charAt(i); } } return cipher; } public static void main(String[] args) { String key; key = "Computer"; System.out.println("Keyword : " + key); String encoded = encoder(key.toCharArray()); String message = "GeeksforGeeks"; System.out.println("Message before Ciphering : " + message); System.out.println("Ciphered Text : " + cipheredIt(message, encoded)); } } OUTPUT: Keyword : Computer Message before Ciphering : GeeksforGeeks Ciphered Text : EUUDNTILEUUDN CEASER CYPHER: def encrypt(string, shift): cipher = '' for char in string: if char == ' ': cipher = cipher + char elif char.isupper(): cipher = cipher + chr((ord(char) + shift - 65) % 26 +

else: cipher = cipher + chr((ord(char) + shift - 97) % 26 +

return cipher text = input("enter string: ") s = int(input("enter shift number: ")) print("original string: ", text) print("after encryption: ", encrypt(text, s)) output: enter string: hii how enter shift number: 5 original string: hii how after encryption: mnn mtb AFFINE : def egcd(a, b): x,y, u,v = 0,1, 1, while a != 0: q, r = b//a, b%a m, n = x-uq, y-vq b,a, x,y, u,v = a,r, u,v, m,n gcd = b return gcd, x, y def modinv(a, m):

gcd, x, y = egcd(a, m) if gcd != 1: return None # modular inverse does not exist else: return x % m def affine_encrypt(text, key): return ''.join([ chr((( key[0](ord(t) - ord('A')) + key[1] ) % 26)+ ord('A')) for t in text.upper().replace(' ', '') ]) def affine_decrypt(cipher, key): return ''.join([ chr((( modinv(key[0], 26)(ord(c) - ord('A') - key[1]))% 26) + ord('A')) for c in cipher ]) def main(): text = 'AFFINE CIPHER' key = [17, 20] affine_encrypted_text = affine_encrypt(text, key) print('Encrypted Text: {}'.format( affine_encrypted_text )) print('Decrypted Text: {}'.format( affine_decrypt(affine_encrypted_text, key) )) if name == 'main':main() output: Encrypted Text: UBBAHKCAPJKX Decrypted Text: AFFINECIPHER

Diffie hellman:

from random import randint if name == 'main':

Both the persons will be agreed upon the

public keys G and P

A prime number P is taken

P = 23

A primitve root for P, G is taken

G = 9 print('The Value of P is :%d'%(P)) print('The Value of G is :%d'%(G))

Alice will choose the private key a

a = 4 print('The Private Key a for Alice is :%d'%(a))

gets the generated key

x = int(pow(G,a,P))

Bob will choose the private key b

b = 3 print('The Private Key b for Bob is :%d'%(b))

gets the generated key

y = int(pow(G,b,P))

Secret key for Alice

ka = int(pow(y,a,P))

Secret key for Bob

kb = int(pow(x,b,P)) print('Secret key for the Alice is : %d'%(ka)) print('Secret Key for the Bob is : %d'%(kb)) Output: The Value of P is : The Value of G is : The Private Key a for Alice is : The Private Key b for Bob is : Secret key for the Alice is : 9 Secret Key for the Bob is : 9

des:

Hexadecimal to binary conversion

def hex2bin(s): mp = {'0' : "0000", '1' : "0001", '2' : "0010", '3' : "0011", '4' : "0100", '5' : "0101", '6' : "0110", '7' : "0111", '8' : "1000", '9' : "1001", 'A' : "1010", 'B' : "1011", 'C' : "1100", 'D' : "1101", 'E' : "1110", 'F' : "1111" } bin = "" for i in range(len(s)): bin = bin + mp[s[i]] return bin

15, 7]

Expansion D-box Table

exp_d = [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 ]

Straight Permutaion Table

per = [ 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 ]

S-box Table

sbox = [[[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] ] ]

Final Permutaion Table

final_perm = [ 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30,

33, 1, 41, 9, 49, 17, 57, 25 ]

def encrypt(pt, rkb, rk): pt = hex2bin(pt)

Initial Permutation

pt = permute(pt, initial_perm, 64) print("After inital permutation", bin2hex(pt))

Splitting

left = pt[0:32] right = pt[32:64] for i in range(0, 16):

Expansion D-box: Expanding the 32

bits data into 48 bits right_expanded = permute(right, exp_d,

XOR RoundKey[i] and right_expanded

xor_x = xor(right_expanded, rkb[i])

S-boxex: substituting the value from s-

box table by calculating row and column sbox_str = "" for j in range(0, 8): row = bin2dec(int(xor_x[j * 6] + xor_x[j * 6 + 5])) col = bin2dec(int(xor_x[j * 6 + 1]

  • xor_x[j * 6 + 2] + xor_x[j * 6 + 3] + xor_x[j * 6 + 4])) val = sbox[j][row][col] sbox_str = sbox_str + dec2bin(val)

Straight D-box: After substituting

rearranging the bits sbox_str = permute(sbox_str, per, 32)

XOR left and sbox_str

result = xor(left, sbox_str) left = result

Swapper

if(i != 15): left, right = right, left print("Round ", i + 1, " ", bin2hex(left), " ", bin2hex(right), " ", rk[i])

Combination

combine = left + right

Final permutaion: final rearranging of bits to

get cipher text cipher_text = permute(combine, final_perm,

return cipher_text pt = "123456ABCD132536" key = "AABB09182736CCDD"

Key generation

--hex to binary

key = hex2bin(key)

--parity bit drop table

keyp = [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 ]

getting 56 bit key from 64 bit using the parity bits

key = permute(key, keyp, 56)

Number of bit shifts

shift_table = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 ]

Key- Compression Table : Compression of key from 56

bits to 48 bits key_comp = [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 ]

System.out.println("Error while encrypting: " + e.toString());} return null;} public static String decrypt(String strToDecrypt, String secret) { try{ setKey(secret); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, secretKey); return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecr ypt))); } catch (Exception e) { System.out.println("Error while decrypting: " + e.toString()); } return null; } public static void main(String[] args) { final String secretKey = "ssshhhhhhhhhhh!!!!"; String originalString = "welcome to cse"; String encryptedString = Main.encrypt(originalString, secretKey) ; String decryptedString = Main.decrypt(encryptedString, secretKey) ; System.out.println(originalString); System.out.println(encryptedString); System.out.println(decryptedString); } } Output: welcome to cse Ki4CvtFmk1rHNiSrhW80/w== welcome to cse RSA: #include #include int gcd(int a, int h) { int temp; while (1) { temp = a%h; if (temp == 0) return h; a = h;h = temp; }}int main() { int p = 3; int q = 7; int n = pq; int e = 2; int phi = (p-1)(q-1); while (e < phi) { // e must be co-prime to phi and // smaller than phi. if (gcd(e, phi)==1) break; else e++; } int d = 1; while (1){ if ( (d*e) % phi == 1) break; d++; } int msg = 11; printf("Message data = %d", msg); long c = pow(msg,e); c = c % n; printf("\nEncrypted data = %ld", c); long m = pow(c, d); m = m% n; printf("\nOriginal Message Sent = %ld", m); return 0; } OUTPUT: Message data = 11 Encrypted data = 2 Original Message Sent = 11 Sha: import hashlib str = "GeeksforGeeks" result = hashlib.sha256(str.encode()) print("The hexadecimal equivalent of SHA256 is : ") print(result.hexdigest()) result = hashlib.sha384(str.encode()) print("The hexadecimal equivalent of SHA384 is : ") print(result.hexdigest()) result = hashlib.sha224(str.encode()) print("The hexadecimal equivalent of SHA224 is : ") print(result.hexdigest()) result = hashlib.sha512(str.encode()) print("The hexadecimal equivalent of SHA512 is : ") print(result.hexdigest()) result = hashlib.sha1(str.encode()) print("The hexadecimal equivalent of SHA1 is : ") print(result.hexdigest()) OUTPUT: The hexadecimal equivalent of SHA256 is : f6071725e7ddeb434fb6b32b8ec4a2b14dd7db0d 7b2fb48f9975126178f The hexadecimal equivalent of SHA384 is :

d1e67b8819b009ec7929933b6fc1928dd64b5df31bcde 381b9d3f90488d253240490460c0a5a1a873da8236c12e f9b The hexadecimal equivalent of SHA224 is : 173994f309f727ca939bb185086cd7b36e66141c9e52ba 0bdcfd145d The hexadecimal equivalent of SHA512 is : 0d8fb9370a5bf7b892be4865cdf8b658a82209624e33ed 71cae353b0df254a75db63d1baa35ad99f26f1b399c31f c666a7fc67ecef3bdcdb7d60e8ada90b The hexadecimal equivalent of SHA1 is : 4175a37afd561152fb60c305d4fa6026b7e ONETIMEPAD: import onetimepad cipher = onetimepad.encrypt('One Time Cipher', 'random') print("Cipher text is ") print(cipher) print("Plain text is ") msg = onetimepad.decrypt(cipher, 'random') print(msg) OUTPUT: