Docsity

Implementing Substitution and Transposition Ciphers: Practical Examples in Python, Study Guides, Projects, Research of Network security

Toggle sidebar
bg1
Practical 1
Implementing Substitution and Transposition Ciphers
Caesar Cipher-
import java.io.*;
class CaesarCipherProgram
{
public static void main(String args[])throws Exception
{
String pt,ct="";
int key=3;
BufferedReader brk=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Plain Text: ");
pt=brk.readLine();
pt=pt.toLowerCase();
for(int i=0;i<pt.length();i++)
{
int acvalue=(int)pt.charAt(i);
acvalue=acvalue+key;
if(acvalue>122)
{
int diff=acvalue-122;
acvalue=96+diff;
}
ct=ct+(char)acvalue;
}
System.out.print(ct);
}
}
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Implementing Substitution and Transposition Ciphers: Practical Examples in Python and more Study Guides, Projects, Research Network security in PDF only on Docsity!

Practical 1

Implementing Substitution and Transposition Ciphers

Caesar Cipher-

import java.io.*; class CaesarCipherProgram { public static void main(String args[])throws Exception { String pt,ct=""; int key=3; BufferedReader brk=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Plain Text: "); pt=brk.readLine(); pt=pt.toLowerCase(); for(int i=0;i<pt.length();i++) { int acvalue=(int)pt.charAt(i); acvalue=acvalue+key; if(acvalue>122) { int diff=acvalue-122; acvalue=96+diff; } ct=ct+(char)acvalue; } System.out.print(ct); } }

Output-

Monoalphabetic Cipher-

import java.util.Scanner; public class MonoalphabeticCipher { public static char p[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; public static char ch[]={'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'}; public static String doEncryption (String s) { char c[]=new char[(s.length())]; for (int i = 0;i<s.length();i++) { for (int j = 0;j<26;j++) { if(p[j] == s.charAt(i)) { c[i] = ch[j]; break; } } }

Output-

Railfence Technique-

import java.io.*; class Railfence { public static void main(String args[])throws Exception { String pt,ct=""; BufferedReader brk=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Plain Text: "); pt=brk.readLine(); pt=pt.toLowerCase(); for(int i=0; i<pt.length(); i++) { if(i%2==0) ct=ct+pt.charAt(i); } for(int i=0; i<pt.length(); i++) { if(i%2!=0) ct=ct+pt.charAt(i); } System.out.println("Cipher Text " +ct);

msg = 11 print(f'Original message:{msg}') C = pow(msg, e) C = math.fmod(C, n) print(f'Encrypted message: {C}') M = pow(C, d) M = math.fmod(M, n) print(f'Decrypted message: {M}') Output-

Practical 3

Message Authentication Codes (MAC)

Python Code for implementing MD5 Algorithm-

import hashlib result=hashlib.md5(b'Ismile') result1=hashlib.md5(b'Esmile') print("The byte equivalent of hash is: ",end="") print(result.digest()) print("The byte equivalent of hash is: ",end="") print(result1.digest())

Python Code for implementing SHA Algorithm-

import hashlib str = input("Enter the value to encode") result = hashlib.sha1(str.encode()) print("The hexadecimal equivalent of SHA1 is: ") print(result.hexdigest())

Practical 5

Key Exchange using Diffie-Hellman

from random import randint if name == 'main': P = 23 G = 9 print('The Value of P is :%d'%(P)) print('The Value of G is :%d'%(G)) a = 4 print('Secret Number for Alice is :%d'%(a)) x = int(pow(G,a,P)) b = 6 print('Secret Number for Bob is :%d'%(b)) y = int(pow(G,b,P)) ka = int(pow(y,a,P)) kb = int(pow(x,b,P)) print('Secret key for the Alice is : %d'%(ka)) print('Secret Key for the Bob is : %d'%(kb))