0% found this document useful (0 votes)
76 views9 pages

All Cns Code

The document discusses several encryption algorithms: 1) Caesar cipher which shifts each letter by a set number of positions. 2) Vigenere cipher which uses a keyword to shift letters by varying amounts. 3) Rail fence cipher which arranges letters in a zigzag pattern in a matrix. 4) RSA which uses public/private key encryption based on large prime numbers. 5) SHA-1 which generates a cryptographic hash or digital fingerprint of data. Code samples and outputs are provided for each algorithm.

Uploaded by

KARRTHIK M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views9 pages

All Cns Code

The document discusses several encryption algorithms: 1) Caesar cipher which shifts each letter by a set number of positions. 2) Vigenere cipher which uses a keyword to shift letters by varying amounts. 3) Rail fence cipher which arranges letters in a zigzag pattern in a matrix. 4) RSA which uses public/private key encryption based on large prime numbers. 5) SHA-1 which generates a cryptographic hash or digital fingerprint of data. Code samples and outputs are provided for each algorithm.

Uploaded by

KARRTHIK M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CEASER CIPHER:-

public class CaesarAlg {

public static StringBuffer encrypt(String text, int s) {


StringBuffer result=new StringBuffer();

for (int i=0;i<text.length();i++) {


if(Character.isUpperCase(text.charAt(i)))
{
char ch=(char)(((int)text.charAt(i)+s-65)%26+65);
result.append(ch);
}
else {
char ch=(char)(((int)text.charAt(i)+s-98)%25+66);
result.append(ch);
}
}
return result;
}
public static void main(String args[]) {
String text="ATTACKATONCE";
int s=4;
System.out.println("Text :"+text);
System.out.println("Shift : "+s);
System.out.println("Cipher : "+ encrypt(text,s));
}
}

OUTPUT:-
Text :ATTACKATONCE
Shift : 4
Cipher : EXXEGOEXSRGI
Vigenere Cipher:-

public class VigenereCipher


{

public static void main(String arg[])


{
String plaintext = "JAVAHUNGRYBLOG";
String keyword = "LEMON";
encryptDecrypt(plaintext,keyword);
}

public static void encryptDecrypt(String plaintext, String keyword)


{
//Converting plaintext to char array
char msg[] = plaintext.toCharArray();
int msgLen = msg.length;
int i,j;

// Creating new char arrays


char key[] = new char[msgLen];
char encryptedMsg[] = new char[msgLen];
char decryptedMsg[] = new char[msgLen];

/* generate key, using keyword in cyclic


manner equal to the length of
original message i.e plaintext */
for(i = 0, j = 0; i < msgLen; ++i, ++j)
{
if(j == keyword.length())
{
j = 0;
}
key[i] = keyword.charAt(j);
}

//encryption code
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = (char) (((msg[i] + key[i]) % 26) + 'A');

//decryption code
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (char)((((encryptedMsg[i] - key[i]) + 26) % 26) + 'A');

System.out.println("Original Message: " + plaintext);


System.out.println("Keyword: " + keyword);
/* String.valueOf() method converts
char[] to String */
System.out.println("Key: " + String.valueOf(key));
System.out.println();
System.out.println("Encrypted Message: " + String.valueOf(encryptedMsg));
System.out.println();
System.out.println("Decrypted Message: " + String.valueOf(decryptedMsg));
}
}
OUTPUT:-
Original Message: JAVAHUNGRYBLOG
Keyword: LEMON
Key: LEMONLEMONLEMO

Encrypted Message: UEHOUFRSFLMPAU

Decrypted Message: JAVAHUNGRYBLOG

RailFenceBasic:-
import java.util.*;
class RailFenceBasic{
int depth;
String Encryption(String plainText,int depth)throws Exception
{
int r=depth,len=plainText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;

String cipherText="";

for(int i=0;i< c;i++)


{
for(int j=0;j< r;j++)
{
if(k!=len)
mat[j][i]=plainText.charAt(k++);
else
mat[j][i]='X';
}
}
for(int i=0;i< r;i++)
{
for(int j=0;j< c;j++)
{
cipherText+=mat[i][j];
}
}
return cipherText;
}

String Decryption(String cipherText,int depth)throws Exception


{
int r=depth,len=cipherText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;

String plainText="";

for(int i=0;i< r;i++)


{
for(int j=0;j< c;j++)
{
mat[i][j]=cipherText.charAt(k++);
}
}
for(int i=0;i< c;i++)
{
for(int j=0;j< r;j++)
{
plainText+=mat[j][i];
}
}

return plainText;
}
}

class RailFence{
public static void main(String args[])throws Exception
{
RailFenceBasic rf=new RailFenceBasic();
Scanner scn=new Scanner(System.in);
int depth;

String plainText,cipherText,decryptedText;

System.out.println("Enter plain text:");


plainText=scn.nextLine();

System.out.println("Enter depth for Encryption:");


depth=scn.nextInt();

cipherText=rf.Encryption(plainText,depth);
System.out.println("Encrypted text is:\n"+cipherText);

decryptedText=rf.Decryption(cipherText, depth);

System.out.println("Decrypted text is:\n"+decryptedText);

}
}

OUTPUT:-
Enter plain text:
KARRTHIK
Enter depth for Encryption:
4
Encrypted text is:
KTAHRIRK
Decrypted text is:
KARRTHIK
RSA:-

import java.io.DataInputStream;

import java.io.IOException;

import java.math.BigInteger;

import java.util.Random;

public class RSA

private BigInteger P;

private BigInteger Q;

private BigInteger N;

private BigInteger PHI;

private BigInteger e;

private BigInteger d;

private int maxLength = 1024;

private Random R;

public RSA()

R = new Random();

P = BigInteger.probablePrime(maxLength, R);

Q = BigInteger.probablePrime(maxLength, R);

N = P.multiply(Q);

PHI = P.subtract(BigInteger.ONE).multiply( Q.subtract(BigInteger.ONE));

e = BigInteger.probablePrime(maxLength / 2, R);

while (PHI.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(PHI) < 0)


{

e.add(BigInteger.ONE);

d = e.modInverse(PHI);

public RSA(BigInteger e, BigInteger d, BigInteger N)

this.e = e;

this.d = d;

this.N = N;

public static void main (String [] arguments) throws IOException

RSA rsa = new RSA();

DataInputStream input = new DataInputStream(System.in);

String inputString;

System.out.println("Enter message you wish to send.");

inputString = input.readLine();

System.out.println("Encrypting the message: " + inputString);

System.out.println("The message in bytes is:: "

+ bToS(inputString.getBytes()));

// encryption

byte[] cipher = rsa.encryptMessage(inputString.getBytes());

// decryption

byte[] plain = rsa.decryptMessage(cipher);

System.out.println("Decrypting Bytes: " + bToS(plain));


System.out.println("Plain message is: " + new String(plain));

private static String bToS(byte[] cipher)

String temp = "";

for (byte b : cipher)

temp += Byte.toString(b);

return temp;

// Encrypting the message

public byte[] encryptMessage(byte[] message)

return (new BigInteger(message)).modPow(e, N).toByteArray();

// Decrypting the message

public byte[] decryptMessage(byte[] message)

return (new BigInteger(message)).modPow(d, N).toByteArray();

OUTPUT:
Enter message you wish to send.
KARRTHIK
Encrypting the message: KARRTHIK
The message in bytes is:: 7565828284727375
Decrypting Bytes: 7565828284727375
Plain message is: KARRTHIK
SHA-1
import java.security.*;
class JceSha1Test {
public static void main(String[] a) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Message digest object info: ");
System.out.println(" Algorithm = "+md.getAlgorithm());
System.out.println(" Provider = "+md.getProvider());
System.out.println(" toString = "+md.toString());
String input = "";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") =");
System.out.println(" "+bytesToHex(output));
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") =");
System.out.println(" "+bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();

System.out.println();
System.out.println("SHA1(\""+input+"\") =");
System.out.println(" "+bytesToHex(output));
} catch (Exception e) {
System.out.println("Exception: "+e);
}
}
public static String bytesToHex(byte[] b) {
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
StringBuffer buf = new StringBuffer();
for (int j=0; j<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]);
}
return buf.toString();
}
}

OUTPUT:-

Message digest object info:


Algorithm = SHA1
Provider = SUN version 17
toString = SHA1 Message Digest from SUN, <initialized>

SHA1("") =
DA39A3EE5E6B4B0D3255BFEF95601890AFD80709

SHA1("abc") =
A9993E364706816ABA3E25717850C26C9CD0D89D

SHA1("abcdefghijklmnopqrstuvwxyz") =
32D10C7B8CF96570CA04CE37F2A19D84240D3A89

You might also like