Cis Lab Notes
Cis Lab Notes
import java.util.Scanner;
int n = msg.length;
scanner.close();
}
}
pLAYFAIR:
key=input("Enter key")
key=key.replace(" ", "")
key=key.upper()
def matrix(x,y,initial):
return [[initial for i in range(x)] for j in range(y)]
result=list()
for c in key: #storing key
if c not in result:
if c=='J':
result.append('I')
else:
result.append(c)
flag=0
for i in range(65,91): #storing other character
if chr(i) not in result:
if i==73 and chr(74) not in result:
result.append("I")
flag=1
elif flag==0 and i==73 or i==74:
pass
else:
result.append(chr(i))
k=0
my_matrix=matrix(5,5,0) #initialize matrix
for i in range(0,5): #making matrix
for j in range(0,5):
my_matrix[i][j]=result[k]
k+=1
while(1):
choice=int(input("\n 1.Encryption \n 2.Decryption: \n 3.EXIT"))
if choice==1:
encrypt()
elif choice==2:
decrypt()
elif choice==3:
exit()
else:
print("Choose correct choice")
VIGNERE:
import java.util.Scanner;
System.out.print("Enter the length of the plain text stream (Without spaces): ");
int pl = scanner.nextInt();
int count = 0;
while (count < pl) {
for (int i = 0; i < kl; ++i) {
if (65 <= k[i] && k[i] <= 91)
s[1][count + i] = k[i] % 65;
else
s[1][count + i] = k[i] % 97;
}
count = count + kl;
}
System.out.println("\n");
for (int i = 0; i < pl; ++i)
System.out.print(s[1][i] + " ");
System.out.println("\n");
for (int i = 0; i < pl; ++i)
System.out.print(s[2][i] + " ");
scanner.close();
}
}
Railfence :
import java.util.Scanner;
char[] s = inputString.toCharArray();
char[] a = new char[10];
char[] b = new char[10];
int k = 0, l = 0, m = 0;
scanner.close();
}
}
AFFINE CIPHER:
import java.util.*;
public class Affine
{
static int a;
static int b;
public static String encrypt(int a,int b,String msg){
String cipher =" ";
for(int i=0;i<msg.length();i++){
if (msg.charAt(i)!=' '){
cipher=cipher+(char)((((a * (msg.charAt(i) - 'A')) + b)
% 26) + 'A');
}
else{
cipher=cipher+msg.charAt(i);
}
}
return cipher;
}
public static String decrypt(int a,int b,String cipher){
String msg = "";
int a_inv = 0;
int flag = 0;
for(int i=0;i<26;i++){
flag=(a*i)%26;
if(flag==1){
a_inv=i;
}
}
for (int i = 0; i < cipher.length(); i++)
{
return msg;
}
sc.close();
}
}
DES:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;
class Des {
encryptCipher = Cipher.getInstance(ALGORITHM);
encryptCipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
decryptCipher = Cipher.getInstance(ALGORITHM);
decryptCipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
}
MD5:
import hashlib
# initializing string
str2hash = input("ENTER MESSAGE :");
SHA ALGORITHM:
import sys
import hashlib
print(s.hexdigest())
DIGITAL SIGNATURE:
import hashlib
import base64
from Crypto.PublicKey import DSA
from Crypto.Signature import DSS
from Crypto.Random import get_random_bytes
def main():
# Generate a key-pair
key = DSA.generate(1024)
pubk = key.publickey()
prvk = key
if __name__ == "__main__":
main()