Human being from ages had two inherent needs − (a) to communicate and share information and (b) to communicate selectively. These two needs gave rise to the art of coding the messages in such a way that only the intended people could have access to the information. Unauthorized people could not extract any information, even if the scrambled messages fell in their hand.
The art and science of concealing the messages to introduce secrecy in information security is recognized as cryptography.
The word ‘cryptography’ was coined by combining two Greek words, ‘Krypto’ meaning hidden and ‘graphene’ meaning writing
Program No.
1
Write a Java
Program to perform
encryption and decryption using Ceaser Cipher.
import
java.util.Scanner;
public class myCaeserCipher {
public
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'};
int key
= 3;
public
String doEncryption (String s){
String s1 = new String(s);
s1 = "";
int t;
for (int i=0; i<s.length();i++){
for(int j=0; j<26;j++){
if(s.charAt(i)==' '){
s1 =
s1+" ";
break;
}
if(p[j]==s.charAt(i)){
t=(j-key+26)%26;
s1=s1+p[t];
break;
}
}
}
return(s1);
}
public
String doDecrypt(String s){
String s1 = new String(s);
s1="";
int t;
for(int i=0;
i<s.length();i++){
for(int j=0;j<26;
j++)
{
if(s.charAt(i)=='
'){
s1=s1+"
";
break;
}
if(p[j]==s.charAt(i)){
t=(j+key+26)%26;
s1=s1+p[t];
break;
}
}
}
return(s1);
}
public
static void main (String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Ente a string: \n");
String msg = sc.nextLine();
String emsg,nmsg;
myCaeserCipher c1 = new myCaeserCipher();
emsg = c1.doEncryption(msg);
System.out.println("\nafter encryption:
"+ emsg);
nmsg = c1.doDecrypt(emsg);
System.out.println("\nafter decryption :
" + nmsg);
}
}
OUTPUT
Program No.
2
Write a Java
Program to perform
encryption and decryption using Monoalphabetic Cipher.
import java.net.*;
import java.io.*;
public class MonoAlphabeticSubstitutionCipher
{
public 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 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 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;
}
}
}
return(new String(c));
}
public String
doDecryption(String s)
{
char p1[]=new char[(s.length())];
for(int i=0;i<s.length();i++)
{
for(int j=0;j<26;j++)
{
if(ch[j]==s.charAt(i))
{
p1[i]=p[j];
break;
}
}
}
return(new
String(p1));
}
public
static void main(String args[])
{
String
msg="helloworld";
String
emsg,nmsg;
MonoAlphabeticSubstitutionCipher
c1=new MonoAlphabeticSubstitutionCipher();
emsg=
c1.doEncryption(msg);
System.out.println("\n\n
Normal Text : " + msg);
System.out.println("\n\n
Encrypted Text : " + emsg);
nmsg=
c1.doDecryption(emsg);
System.out.println("\n\n
main Decrypted Text : " + nmsg);
}
}
OUTPUT
Program No.
3
Write a Java
Program to perform
encryption and decryption using Polyalphabetic Cipher.
import java.util.*;
import java.net.*;
import java.io.*;
public class PolyAlphabeticSubstitutionCipher1
{
public 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
int pv[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
public
char ch[] =
{'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
int key=12;
public
int keys[]=new int[30];
public
String doEncryption(String s)
{
int
track=0;
int
tempk=key;
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))
{
int
temp=j + tempk;
if(temp
> 25)
temp
= temp -26;
c[i]=ch[temp];
keys[track]=tempk;
track++;
tempk=j;
break;
}
}
}
return(new
String(c));
}
public
String doDecryption(String s)
{
char
p1[]=new char[(s.length())];
int
l=s.length();
int
c=0;
for(int
i=0;i<l;i++)
{
for(int
j=0;j<26;j++)
{
if(ch[j]==s.charAt(i))
{
//System.out.println(ch[j]
+ " " + keys[c] + j);
if(keys[c]>j)
{
p1[i]=p[26+j
- keys[c]];
//System.out.println(" " + p1[i]);
}
else
p1[i]=p[j
- keys[c]];
c++;
break;
}
}
}
return(new
String(p1));
}
public
static void main(String args[])
{
String
msg="attackistoday";
String
emsg,nmsg;
PolyAlphabeticSubstitutionCipher1
c1=new PolyAlphabeticSubstitutionCipher1();
emsg=
c1.doEncryption(msg);
System.out.println("\n\n
Normal Text : " + msg);
System.out.println("\n\n
Encrypted Text : " + emsg);
nmsg=
c1.doDecryption(emsg);
System.out.println("\n\n
main Decrypted Text : " + nmsg);
}
}
OUTPUT
Normal Text : attackistoday
Encrypted Text : MTMTCMSALHRDY
main Decrypted Text : attackistoday
Program No. 4
Program No. 4
Write a Java
Program to perform
encryption and decryption using Transposition Mirror Cipher.
public class TranspositionMirrorCipher
{
int
keys[]={0,0,0,0,0,0,0,0,0,0};
int
count=0;
public
String doEncryption(String s)
{
String
s3=new String();
for(int
i=0;i<s.length();i++)
{
if(s.charAt(i)=='
')
keys[count++]=i;
}
int
j=0;
for(int
i=s.length()-1;i>=0;i--)
{
if(s.charAt(i)!='
')
{
s3=s3
+ s.charAt(i);
j++;
}
if(j==5)
{
s3=s3
+ " ";
j=0;
}
}
return(s3);
}
public
String doDecryption(String s)
{
String
s3=new String();
String
s2=new String();
int
c=0;
for(int
i=s.length()-1;i>=0;i--)
{
if(s.charAt(i)!='
')
{
s3=s3
+ s.charAt(i);
}
if(s3.length()==keys[c])
{
s3=s3+
" ";
c++;
}
}
return(s3);
}
public
static void main(String args[])
{
String
msg="I CAME I SAW I CONQUERED";
String
emsg,nmsg;
TranspositionMirrorCipher
c1=new TranspositionMirrorCipher();
emsg
= c1.doEncryption(msg);
System.out.println("\n\n
Normal Text : " + msg);
System.out.println("\n\n
Encrypted Text : " + emsg);
nmsg=
c1.doDecryption(emsg);
System.out.println("\n\n
main Decrypted Text : " + nmsg);
}
}
OUTPUT
Normal Text : I CAME I SAW I CONQUERED
Encrypted Text : DEREU QNOCI WASIE MACI
main Decrypted Text : I CAME I SAW I CONQUERED
Program No. 5
Program No. 5
Write a Java
Program to perform
encryption and decryption using Transposition Railfence Cipher.
public class TranspositionRailfenceCipher
{
int
keys[]={0,0,0,0,0,0,0,0,0,0};
int
count1=0;
int
count=0;
public
String doEncryption(String s)
{
String
s1=new String();
String
s2=new String();
String
s3=new String();
int
j=0;
for(int
i=0;i<s.length();i++)
{
if(s.charAt(i)=='
')
keys[count1++]=i;
if(s.charAt(i)!='
')
{
if(j==0)
{
s1=s1+
s.charAt(i);
j=1;
}
else
{
s2=s2
+ s.charAt(i);
j=0;
}
}
}
count=0;
for(int
i=0;i<s1.length();i++)
{
s3=s3
+ s1.charAt(i);
count++;
if(count
== 5)
{
s3
= s3 + " ";
count=0;
}
}
count=0;
for(int
i=0;i<s2.length();i++)
{
s3=s3
+ s2.charAt(i);
count++;
if(count
== 5)
{
s3
= s3 + " ";
count=0;
}
}
return(s3);
}
public
String doDecryption(String s)
{
String
s1=new String();
String
s2=new String();
String
s3=new String();
for(int
i=0;i<s.length();i++)
{
if(i<11)
{
if(s.charAt(i)!=
' ' )
s1=s1
+ s.charAt(i);
}
else
{
if(s.charAt(i)!=
' ' )
s2=s2
+ s.charAt(i);
}
}
int
j=0;
int
i1=0;
int
i2=0;
int
i=0;
int
l=s1.length() + s2.length();
while(i<l)
{
if(j==0)
{
s3=s3
+ s1.charAt(i1);
j=1;
i1++;
}
else
{
s3=s3
+ s2.charAt(i2);
j=0;
i2++;
}
i++;
}
String
s4=new String();
count=0;
j=0;
for(i=0;i<s3.length();i++)
{
s4=s4
+ s3.charAt(i);
count++;
if(count==keys[j])
{
s4=s4
+ " ";
count++;
j++;
}
}
return(s4);
}
public
static void main(String args[])
{
String
msg="I CAME I SAW I CONQUERED";
String
emsg,nmsg;
TranspositionRailfenceCipher
c1=new TranspositionRailfenceCipher();
emsg
= c1.doEncryption(msg);
System.out.println("\n\n
Normal Text : " + msg);
System.out.println("\n\n
Encrypted Text : " + emsg);
nmsg=
c1.doDecryption(emsg);
System.out.println("\n\n
main Decrypted Text : " + nmsg);
}
}
OUTPUT
Normal
Text : I CAME I SAW I CONQUERED
Encrypted
Text : IAESW CNURD CMIAI OQEE
main
Decrypted Text : I CAME I SAW I CONQUERED
Program No. 6
Program No. 6
Write a Java
Program to perform
encryption and decryption using Row Transposition ciphers.
import java.util.*;
import java.net.*;
import java.io.*;
public class TranspositionCipher
{
int
keyR[]={2,5,4,1,3};
int
keyW[]={4,1,5,3,2};
public
String doEncryption(String s)
{
char
s1[][]=new char[7][5];
char
s2[][]=new char[7][5];
int
r=0;
int
c=0;
char
cipher[]=new char[(s.length()) + 10];
for(int
i=0;i<s.length();i++)
{
s1[r][c]=s.charAt(i);
c++;
if(c==5)
{
c=0;
r++;
}
}
for(;c<5
&& c!=0;c++)
s1[r][c]='1';
System.out.println("First
Matrix");
for(int
i=0;i<7;i++)
{
for(int
j=0;j<5;j++)
System.out.print(s1[i][j]
+ " ");
System.out.println("");
}
c=0;
for(int
i=0;i<7;i++)
{
for(int
j=0;j<5;j++)
{
s2[i][j]=s1[i][keyW[j]-1];
cipher[c]=s2[i][j];
c++;
}
cipher[c]='
';
c++;
}
System.out.println("\n\nSecond
Matrix");
for(int
i=0;i<7;i++)
{
for(int
j=0;j<5;j++)
System.out.print(s2[i][j]
+ " ");
System.out.println("");
}
//
System.out.println("Plain Text : "+new String(p1));
return(new
String(cipher));
}
public
String doDecryption(String s)
{
char
s1[][]=new char[9][5];
char
s2[][]=new char[9][5];
int
r=0;
int
c=0;
char
cipher[]=new char[(s.length()) + 10];
for(int
i=0;i<s.length();i++)
{
if(s.charAt(i)=='
')
i++;
s1[r][c]=s.charAt(i);
c++;
if(c==5)
{
c=0;
r++;
}
}
System.out.println("First
Matrix ==========");
for(int
i=0;i<7;i++)
{
for(int
j=0;j<5;j++)
System.out.print(s1[i][j]
+ " ");
System.out.println("");
}
c=0;
for(int
i=0;i<7;i++)
{
for(int
j=0;j<5;j++)
{
s2[i][j]=s1[i][keyR[j]-1];
if(s2[i][j]!='1')
{
cipher[c]=s2[i][j];
c++;
}
}
}
System.out.println("\n\nSecond
Matrix =============");
for(int
i=0;i<7;i++)
{
for(int
j=0;j<5;j++)
System.out.print(s2[i][j]
+ " ");
System.out.println("");
}
//
System.out.println("Plain Text : "+new String(p1));
return(new
String(cipher));
}
public
static void main(String args[])
{
String
msg="thesimplestpossibletranspositions";
String
emsg,nmsg;
TranspositionCipher
c1=new TranspositionCipher();
emsg
= c1.doEncryption(msg);
System.out.println("\n\n
Normal Text : " + msg);
System.out.println("\n\n
Encrypted Text : " + emsg);
nmsg=
c1.doDecryption(emsg);
System.out.println("\n\n
Decrypted Text : " + nmsg);
}
}
OUTPUT
First Matrix
t h e s i
m p l e s
t p o s s
i b l e t
r a n s p
o s i t i
o n s 1 1
Second Matrix
s t i e h
e m s l p
s t s o p
e i t l b
s r p n a
t o i i s
1 o 1 s n
Normal Text :
thesimplestpossibletranspositions
Encrypted Text :
stieh emslp stsop eitlb srpna toiis 1o1
First Matrix
==========
s t i e h
e m s l p
s t s o p
e i t l b
s r p n a
t o i i s
1 o 1 s n
Second Matrix =============
t h e s i
m p l e s
t p o s s
i b l e t
r a n s p
o s i t i
o n s 1 1
Decrypted Text :
thesimplestpossibletranspositions
Program No. 7
Write a Java
Program to perform
encryption and decryption using Hill ciphers.
import java.util.*;
import java.net.*;
import java.io.*;
public class
HillCipher
{
public
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'};
int
key[][]={{6,24,1},{13,16,10},{20,17,15}};
int
keyinv[][]={{8,5,10},{21,8,21},{21,12,8}};
public
String doEncryption(String s)
{
int
c1[][]=new int[3][1];
char
c[]=new char[(s.length())];
int
khar[][]=new int[3][1];
for(int
i=0;i<s.length();i++)
{
for(int
j=0;j<26;j++)
{
if(p[j]==s.charAt(i))
{
c1[i][0]=j;
break;
}
}
}
for(int
i=0;i<3;i++)
{
for(int
j=0;j<1;j++)
{
khar[i][j]=0;
for(int
k=0;k<3;k++)
khar[i][j]
= khar[i][j] + ( key[i][k] * c1[k][j]);
khar[i][j]=khar[i][j]%26;
}
}
for(int
i=0;i<3;i++)
{
for(int
j=0;j<1;j++)
{
for(int
k=0;k<26;k++)
{
if(khar[i][j]==k)
{
c[i]=p[k];
}
}
}
}
return(new
String(c));
}
public
String doDecryption(String s)
{
int
c1[][]=new int[3][1];
char
c[]=new char[(s.length())];
int
khar[][]=new int[3][1];
for(int
i=0;i<s.length();i++)
{
for(int
j=0;j<26;j++)
{
if(p[j]==s.charAt(i))
{
c1[i][0]=j;
break;
}
}
}
for(int
i=0;i<3;i++)
{
for(int
j=0;j<1;j++)
{
khar[i][j]=0;
for(int
k=0;k<3;k++)
khar[i][j]
= khar[i][j] + ( keyinv[i][k] * c1[k][j]);
khar[i][j]=khar[i][j]%26;
}
}
for(int
i=0;i<3;i++)
{
for(int
j=0;j<1;j++)
{
for(int
k=0;k<26;k++)
{
if(khar[i][j]==k)
{
c[i]=p[k];
}
}
}
}
return(new
String(c));
}
public
static void main(String args[])
{
String
msg="act";
String
emsg,nmsg;
HillCipher
c1=new HillCipher();
emsg=
c1.doEncryption(msg);
System.out.println("\n\n
Normal Text : " + msg);
System.out.println("\n\n
Encrypted Text : " + emsg);
nmsg=
c1.doDecryption(emsg);
System.out.println("\n\n
main Decrypted Text : " + nmsg);
}
}
OUTPUT
Normal Text : act
Encrypted Text : poh
main Decrypted Text :
act
Program No.
8
Write a Java
Program to perform
encryption and decryption using DES.
import
java.security.*;
import
javax.crypto.*;
import
javax.crypto.spec.*;
class DESECB
{
public KeyGenerator keygenerator;
public SecretKey myDesKey;
Cipher c;
public DESECB() throws Exception
{
// Genrate the Key
keygenerator =
KeyGenerator.getInstance("DES");
myDesKey =
keygenerator.generateKey();
// Create the cipher
c =
Cipher.getInstance("DES/ECB/PKCS5Padding");
}
public byte[] doEncryption(String s)
throws Exception
{
//
Initialize the cipher for encryption
c.init(Cipher.ENCRYPT_MODE,
myDesKey);
//sensitive information
byte[] text =
s.getBytes();
// Encrypt the text
byte[] textEncrypted =
c.doFinal(text);
return(textEncrypted);
}
public String doDecryption(byte[]
s)throws Exception
{
// Initialize the same cipher for decryption
c.init(Cipher.DECRYPT_MODE, myDesKey);
// Decrypt the text
byte[] textDecrypted = c.doFinal(s);
return(new String(textDecrypted));
}
public static void main(String[]
argv) throws Exception
{
DESECB d=new DESECB();
String msg= "I CAME
I SAW I CONQUERED";
byte[]
str=d.doEncryption(msg);
System.out.println("Plain
Text : " + msg);
System.out.println("Encrypted
String : "+str);
System.out.println("Decrypted
String : "+d.doDecryption(str));
}
}
OUTPUT
Plain
Text : I CAME I SAW I CONQUERED
Encrypted
String : [B@337838
Decrypted
String : I CAME I SAW I CONQUERED
Program No. 9
Program No. 9
Write a Java
Program to perform
encryption and decryption using Triple DES.
import
java.security.*;
import
javax.crypto.*;
import
javax.crypto.spec.*;
class D3ESECB
{
public KeyGenerator keygenerator;
public SecretKey myDesKey;
Cipher c;
public D3ESECB() throws Exception
{
// Genrate the Key
keygenerator =
KeyGenerator.getInstance("DESede");
myDesKey =
keygenerator.generateKey();
// Create the cipher
c =
Cipher.getInstance("DESede/ECB/PKCS5Padding");
}
public byte[] doEncryption(String s)
throws Exception
{
// Initialize the cipher for encryption
c.init(Cipher.ENCRYPT_MODE,
myDesKey);
//sensitive information
byte[] text =
s.getBytes();
// Encrypt the text
byte[] textEncrypted =
c.doFinal(text);
return(textEncrypted);
}
public String doDecryption(byte[]
s)throws Exception
{
// Initialize the same
cipher for decryption
c.init(Cipher.DECRYPT_MODE,
myDesKey);
// Decrypt the text
byte[] textDecrypted =
c.doFinal(s);
return(new String(textDecrypted));
}
public static void main(String[]
argv) throws Exception
{
D3ESECB d=new D3ESECB();
String msg= "I CAME
I SAW I CONQUERED";
byte[]
str=d.doEncryption(msg);
System.out.println("Plain
Text : " + msg);
System.out.println("Encrypted
String : "+str);
System.out.println("Decrypted
String : "+d.doDecryption(str));
}
}
OUTPUT
Plain
Text : I CAME I SAW I CONQUERED
Encrypted
String : [B@119cca4
Decrypted
String : I CAME I SAW I CONQUERED
Program No.
10
Write a Java
Program to perform
DiffiHellman key Exchange.
import java.math.*;
import java.security.*;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.DHPublicKeySpec;
public class DiffeHellman
{ public
final static int pValue = 47;
public
final static int gValue = 71;
public
final static int XaValue = 9;
public
final static int XbValue = 14;
public
static void main(String[] args) throws Exception
{ // TODO code application logic
here
BigInteger
p = new BigInteger(Integer.toString(pValue));
BigInteger
g = new BigInteger(Integer.toString(gValue));
BigInteger
Xa = new BigInteger(Integer.toString(XaValue));
BigInteger
Xb = new BigInteger(Integer.toString(XbValue));
createKey();
int
bitLength = 512; // 512 bits
SecureRandom
rnd = new SecureRandom();
p
= BigInteger.probablePrime(bitLength, rnd);
g
= BigInteger.probablePrime(bitLength, rnd);
createSpecificKey(p,
g);
}
public
static void createKey() throws Exception
{ KeyPairGenerator kpg =
KeyPairGenerator.getInstance("DiffieHellman");
kpg.initialize(512);
KeyPair
kp = kpg.generateKeyPair();
KeyFactory
kfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpec
kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
DHPublicKeySpec.class);
System.out.println("Public
key is: (createKey) : " + kspec);
}
public
static void createSpecificKey(BigInteger p, BigInteger g) throws Exception
{ KeyPairGenerator kpg =
KeyPairGenerator.getInstance("DiffieHellman");
DHParameterSpec
param = new DHParameterSpec(p, g);
kpg.initialize(param);
KeyPair
kp = kpg.generateKeyPair();
KeyFactory
kfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpec
kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
DHPublicKeySpec.class);
System.out.println("\nPublic
key is : (createSpecificKey) : " + kspec);
}
}
OUTPUT
Public
key is: (createKey) : javax.crypto.spec.DHPublicKeySpec@863399
Public
key is : (createSpecificKey) : javax.crypto.spec.DHPublicKeySpec@141d683
Program No. 11
Program No. 11
Key generation
(public and private key pair) using
java.
import java.security.*;
public class DSAKeyGen
{
public static void main(String[] argv) throws Exception
{
// TODO code application logic here
// Generate a 1024-bit Digital Signature
Algorithm (DSA) key pair
KeyPairGenerator keygenerator =
KeyPairGenerator.getInstance("DSA");
keygenerator.initialize(1024) ;
KeyPair myKey =
keygenerator.generateKeyPair();
PrivateKey privateKey = myKey.getPrivate();
System.out.println(privateKey);
PublicKey publicKey = myKey.getPublic();
System.out.println(publicKey);
}
}
OUTPUT
Private Key
Sun DSA Private Key
parameters:
p:
fd7f5381
1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669
455d4022
51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7
6b9950a5
a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb
83f6d3c5
1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7
q:
9760508f
15230bcc b292b982 a2eb840b f0581cf5
g:
f7e1a085
d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267
5159578e
bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1
3c167a8b
547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b
cca4f1be
a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a
x: 16fb86dd
b7fac5ac 35d0eec8 9d236886 55a3c1d6
Public Key
Sun DSA Public Key
Parameters:
p:
fd7f5381
1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669
455d4022
51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7
6b9950a5
a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb
83f6d3c5
1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7
q:
9760508f
15230bcc b292b982 a2eb840b f0581cf5
g:
f7e1a085
d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267
5159578e
bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1
3c167a8b
547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b
cca4f1be
a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a
y:
58fa0c5b
7b32fefb ae37d360 e108a9cc f724c068 2c4d4a3a 875b08cd 83a4dcdc
6a37280a
53c86c41 c14c7a9a 2f471c0e 4efa3498 8fcafb4a e4ac2f28 6e4afe44
0de82899
0d49698e a3dc1941 52ee2130 82ed8a2f e6a67aeb dc2e0ca8 64f479d8
6342a9f7
591d5d4f 34e367ea b31feed9 6b410141 423122b3 ba1ade5a 5ecc9530
Program No. 12
Program No. 12
Write a Java
Program to perform RSA
import
java.security.*;
import
javax.crypto.*;
import
javax.crypto.spec.*;
class
RSA
{
public KeyPairGenerator
keygenerator;
public KeyPair myKey;
Cipher c;
public
RSA() throws Exception
{
// Genrate the Key
keygenerator =
KeyPairGenerator.getInstance("RSA");
keygenerator.initialize(1024)
;
myKey =
keygenerator.generateKeyPair();
// Create the
cipher
c =
Cipher.getInstance("RSA");
}
public byte[]
doEncryption(String s) throws Exception
{
// Initialize the cipher for encryption
c.init(Cipher.ENCRYPT_MODE,myKey.getPublic());
//sensitive information
byte[] text =
s.getBytes();
// Encrypt the
text
byte[]
textEncrypted = c.doFinal(text);
return(textEncrypted);
}
public String
doDecryption(byte[] s)throws Exception
{
// Initialize
the same cipher for decryption
c.init(Cipher.DECRYPT_MODE,myKey.getPrivate());
// Decrypt the
text
byte[]
textDecrypted = c.doFinal(s);
return(new
String(textDecrypted));
}
public static void main(String[]
argv) throws Exception
{
RSA
d=new RSA();
String msg=
"I CAME I SAW I CONQUERED";
byte[] str=d.doEncryption(msg);
System.out.println("Plain
Text : " + msg);
System.out.println("Encrypted
String : "+str);
System.out.println("Decrypted
String : "+d.doDecryption(str));
}
}
OUTPUT
Plain Text : I CAME I SAW I CONQUERED
Encrypted String : [B@14ed9ff
Decrypted String : I CAME I SAW I CONQUERED
Program No.
13
Program to
perform Digital Signature using Java
import
java.security.*;
import
sun.misc.BASE64Encoder;
public
class DigSign
{
public static void main(String[]
args) throws Exception
{
// TODO code
application logic here
KeyPairGenerator
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair keyPair = kpg.genKeyPair();
byte[] data =
"Sample Text".getBytes("UTF8");
Signature sig =
Signature.getInstance("MD5WithRSA");
sig.initSign(keyPair.getPrivate());
sig.update(data);
byte[]
signatureBytes = sig.sign();
System.out.println("Signature:
\n" + new BASE64Encoder().encode(signatureBytes));
sig.initVerify(keyPair.getPublic());
sig.update(data);
System.out.println(sig.verify(signatureBytes));
}
}
OUTPUT
Signature:
HGKU3m7p8Alj0j+NmgPk4J1pTbWV4ReAomr12sOjhJE1kE4tbKVAMyOZgWQgroaBKUzrvOhwuauH
hhosNetrkesE1YUwYdy7di0uMttcJrRT17gmn9JIDsh1dJXqJjl6IjlWafRkh8OjaAI59Y36GAdS
afz7Y9MSITXn+Hs2rQE=
true
Program No.
14
Program to Calculate the message digest of a text using the SHA-1
algorithm in JAVA
import java.security.*;
public class SHA1
{
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+"\")
= " +bytesToHex(output));
input
= "abc";
md.update(input.getBytes());
output
= md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\")
= " +bytesToHex(output));
input
= "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output
= md.digest();
System.out.println();
System.out.println("SHA1(\""
+input+"\") = " +bytesToHex(output));
System.out.println("");
}
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 1.5
ToString = SHA1
Message Digest from SUN, <initialized>
SHA1("") =
DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
SHA1("abc") =
A9993E364706816ABA3E25717850C26C9CD0D89D
SHA1("abcdefghijklmnopqrstuvwxyz") = 32D10C7B8CF96570CA04CE37F2A19D84240D3A89
No comments:
Post a Comment