Aim: Implementation of Caesar Cipher.: Program Code
Aim: Implementation of Caesar Cipher.: Program Code
Program Code:
import java.util.Scanner;
public class Caesar {
for(int i = 0;i<plain.length;i++){
for(int j = 0 ; j<=25;j++){
if(j<=22){
if(plain[i]==chars[j]){
plain[i] = chars[j+3];
break;
}
}//End nested If
else if(plain[i] == chars[j]){
plain[i] = chars [j-23];
} //End else
} //End nested for loop
} //End of For loop
cipher = String.valueOf(plain);
System.out.println(" cipher text is "+cipher);
Scanner in = new Scanner(System.in);
System.out.println("To Decrypt plaintext enter 1");
int choice = in.nextInt();
if(choice == 1){
return cipher;
}
else{
System.out.println("Thank you");}
Network Security
return empty;
}
Output:
Network Security
Program Code:
import java.lang.Math;
// This would be our vernam cipher (should be same length as our text)
System.out.print(encoded[i]);
}
Network Security
// Run through the encrypted text and against the cipher again
System.out.print(temp);
Output:
Network Security
Program Code:
for(int i=0;i<len;i+=2) {
output += input.charAt(i);
for(int i=1;i<len;i+=2) {
output += input.charAt(i);
}
Network Security
Output:
Network Security
Program Code:
import java.io.*;
String result="";
int offset;
for(int i=0;i<line.length();i++)
offset=((int)line.charAt(i)+shift)%256;
result+=(char)(offset);
return result;
String result="";
int offset;
for(int i=0;i<line.length();i++)
offset=((int)line.charAt(i)-shift)%256;
if(offset<0)
Network Security
offset+=256;
result+=(char)(offset);
return result;
int choice;
choice=Integer.parseInt(in.readLine());
int shift=Integer.parseInt(in.readLine());
String line=in.readLine();
System.out.println("Result:");
switch(choice)
case 1:System.out.println(obj.encrypt(shift,line));
break;
case 2:System.out.println(obj.decrypt(shift,line));
break;
default:
System.out.println("Invalid input!");
Network Security
break;
Output:
Network Security
Program Code:
import javax.crypto.*;
import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.*;
import javax.crypto.spec.IvParameterSpec;
import java.lang.*;
Cipher ecipher;
Cipher dcipher;
DesEncrypter(SecretKey key) {
try {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (javax.crypto.NoSuchPaddingException e) {
} catch (java.security.NoSuchAlgorithmException e) {
} catch (java.security.InvalidKeyException e) {
try {
Network Security
// Encrypt
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (java.io.IOException e) {
return null;
try {
// Decrypt
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (java.io.IOException e) {
Network Security
return null;
System.out.println();
System.out.println();
try {
SecretKey key =
KeyGenerator.getInstance("DES").generateKey();
String d="Hello";
// Encrypt
// Decrypt
}
Network Security
catch (Exception e) {}
Output:
Network Security
Program Code:
import java.security.*;
import javax.crypto.*;
import java.io.*;
public class AES_StringEncrypter
{
Cipher ecipher;
Cipher dcipher;
AES_StringEncrypter(SecretKey key)
{
try
{
ecipher = Cipher.getInstance("AES");
dcipher = Cipher.getInstance("AES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
}
catch (Exception e) {}
}
public String encrypt(String str)
{
try
{
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
}
catch(Exception e) {}
return null;
}
public String decrypt(String str)
{
try
{
// Decode base64 to get bytes
byte[]dec=new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
Network Security
}
catch(Exception e) {}
return null;
}
public static void main(String args[])
{
SecretKey key=null;
try
{
// Generate a AES key
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
key = keyGen.generateKey();
}
catch (Exception e)
{
e.printStackTrace();
}
AES_StringEncrypter dese = new AES_StringEncrypter(key);
String o = "Welcome to UDCS.....";
String en = dese.encrypt(o);
String de = dese.decrypt(en);
System.out.println("Original Text:"+o);
System.out.println("Encrypted Text:"+en);
System.out.println("Decrypted Text:"+de);
}
}
Output:
Network Security
Program Code:
import java.math.*;
import java.security.*;
public class Rsa3
{
BigInteger p,q,n,d,e,ph,t;
SecureRandom r;
public Rsa3()
{
r=new SecureRandom();
p=new BigInteger(512,100,r);
q=new BigInteger(512,100,r);
System.out.println("prime nos p and q are "+p.intValue()+" , "
+q.intValue());
n=p.multiply(q);
ph=(p.subtract(new BigInteger("1")));
ph=ph.multiply(q.subtract(new BigInteger("1")));
e=new BigInteger("2");//initial
while(ph.gcd(e).intValue() > 1 || e.compareTo(ph) !=-1)
e = e.add(new BigInteger("1"));//or "2" when bug
d=e.modInverse(ph);
System.out.println("public key is ("+n.intValue()+" , "
+e.intValue()+")");
System.out.println("pvt key is ("+n.intValue()+" , " +
d.intValue()+")");
BigInteger msg= new BigInteger("15");
System.out.println("\nMessage is: "+msg);
BigInteger enmsg=encrypt(msg,e,n);
System.out.println("\nEncrypted msg is: "+enmsg.intValue());
BigInteger demsg=decrypt(enmsg,d,n);
System.out.println("\nDecrypted msg is: "+demsg.intValue());
}
BigInteger encrypt(BigInteger msg,BigInteger e,BigInteger n)
{
return msg.modPow(e, n);
}
BigInteger decrypt(BigInteger msg,BigInteger d,BigInteger n)
{
return msg.modPow(d, n);
}
public static void main(String[] args)
{
new Rsa3();
Network Security
}
}
Output:
Network Security
Program Code:
class RC4Demo
String strPlainText;
this.strPlainText = strPlainText;
S[i] = i;
int i=0;
int j=0;
int Kc = key[modk];
S[i] = S[j];
Network Security
S[j] = temp;
int Ck = S[Sc];
i = i+1;
new RC4Demo(strOriginal,K);
{ System.out.print(" "+cipher[i]); }
Output: