Cns Lab Programs
Cns Lab Programs
// Caesar cipher
import java.util.Scanner;
public class caesar{
public static String encrypt(String str, int key)
{
String encrypted = "";
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if (Character.isUpperCase(c))
{
c = (char) (c + key);
if (c > 'Z') {
c = (char) (c - 26);
}
}
else if (Character.isLowerCase(c))
{
c = (char) (c + key);
if (c > 'z')
{
c = (char) (c - 26);
}
}
encrypted += c;
}
return encrypted;
}
public static String decrypt(String str, int key)
{
String decrypted = "";
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if (Character.isUpperCase(c))
{
c = (char) (c - key);
if (c < 'A')
{
c = (char) (c + 26);
}
} else if (Character.isLowerCase(c))
{
c = (char) (c - key);
if (c < 'a')
{
c = (char) (c + 26);
}
}
decrypted += c;
}
return decrypted;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter any string: ");
String inputStr = sc.nextLine();
System.out.println("Enter the key: ");
int shiftKey = Integer.valueOf(sc.nextLine());
System.out.println("Encrypted string is: " + encrypt(inputStr, shiftKey));
System.out.println("Decrypted string is: " + decrypt(encrypt(inputStr, shiftKey), shiftKey));
sc.close();
}
}
1c....
#include <stdio.h>
int main() {
char *str="Hello World";
char result[12];
for(int i=0;str[i]!='\0';i++)
{
result[i]=str[i]^0;
}
result[11]='\0';
printf("XOR-ed Result:%s",result);
}
2b....
//SubstitutionCipher
import java.io.*;
import java.util.*;
2c...
#include <stdio.h>
int main() {
char *str="Hello World";
char AND[12];
char OR[12];
char XOR[12];
for(int i=0;str[i]!='\0';i++)
{
AND[i]=str[i]&127;
OR[i]=str[i]|127;
XOR[i]=str[i]^127;
}
AND[11]='\0';
OR[11]='\0';
XOR[11]='\0';
printf("AND Result:%s",AND);
printf("\nOR-ed Result:%s",OR);
printf("\nXOR-ed Result:%s",XOR);
}
3.....
//hill cipher
class hillcipher {
public static int[][] keymat = new int[][] { { 17, 17, 5 }, { 21, 18, 21 }, { 2, 2, 19 } };
public static int[][] invkeymat = new int[][] { { 4, 9, 15 }, { 15, 17, 6 }, { 24, 0, 17 } };
public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
a = key.charAt(x % 26);
b = key.charAt(y % 26);
c = key.charAt(z % 26);
ret = "" + a + b + c;
return ret;
}
ret = "" + a + b + c;
return ret;
}
msg = ("paymoremoney");
System.out.println("Simulation of Hill Cipher\n-------------------------");
System.out.println("Input message : " + msg);
msg = msg.toUpperCase();
msg = msg.replaceAll("\\s", ""); // Remove spaces
n = msg.length() % 3;
// Encoding
for (int i = 0; i < msg.length(); i += 3) {
enc += encode(pdchars[i], pdchars[i + 1], pdchars[i + 2]);
}
System.out.println("Encoded message : " + enc);
// Decoding
char[] dechars = enc.toCharArray();
for (int i = 0; i < enc.length(); i += 3) {
dec += decode(dechars[i], dechars[i + 1], dechars[i + 2]);
}
System.out.println("Decoded message : " + dec);
}
}
4.....
//DES
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
try {
encrypt(sampleFile, encryptedFile);
decrypt(encryptedFile, decryptedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void doCrypto(int cipherMode, File sampleFile, File outputFile) throws Exception {
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(cipherMode, secretKey);
inputStream.close();
outputStream.close();
}
}
6....
//AES or Rijndael
import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import java.util.Base64;
8....
//RSA (Rivest, Shamir, Adleman)
import java.util.*;
import java.math.*;
class rsa {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int p,q,n,z,d=0,e,i;
System.out.println("Enter the number to be encrypted and decrypted");
int msg=sc.nextInt();
double c;
BigInteger msgback;
System.out.println("Enter 1st prime number p");
p=sc.nextInt();
System.out.println("Enter 2nd prime number q");
q=sc.nextInt();
n=p*q;
z=(p-1)*(q-1);
System.out.println("the value of z = "+z);
for(e=2;e<z;e++) {
if(gcd(e,z)==1) { // e is for public key exponent
break; }
}
System.out.println("the value of e = "+e);
for(i=0;i<=9;i++)
{
int x=1+(i*z);
if(x%e==0) //d is for private key exponent
{
d=x/e;
break;
}
}
System.out.println("the value of d = "+d);
c=(Math.pow(msg,e))%n;
System.out.println("Encrypted message is : -");
System.out.println(c);
BigInteger N = BigInteger.valueOf(n); //converting int value of n to BigInteger
//converting float value of c to BigInteger
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Derypted message is : -");
System.out.println(msgback); }
9....
//Diffie–Hellman Key Exchange
import java.util.*;
class Diffie_Hellman {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter modulo(p)");
int p=sc.nextInt();
System.out.println("Enter primitive root of "+p);
int g=sc.nextInt();
System.out.println("Choose 1st secret no of A");
int a=sc.nextInt();
System.out.println("Choose 2nd secret no of B");
int b=sc.nextInt();
int A = (int)Math.pow(g,a)%p;
int B = (int)Math.pow(g,b)%p;
if(S_A==S_B) {
System.out.println("Al and can communicate with each other!!!");
System.out.println("They share a secret no = "+S_A); }
else {
System.out.println("A and B cannot communicate with each other!!!");
}
}
}
10...
//Sha
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class Sha {
public static byte[] getSHA(String input) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-512");
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
11.....
// MD5
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;