SlideShare a Scribd company logo
A. Caesar Cipher:
Code:
import java.io.*;
importjava.math.*;
classCaesarCipher {
private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public String encrypt(String plainText, intshiftKey) {
plainText = plainText.toLowerCase();
String cipherText="";
for(inti=0;i<plainText.length();i++)
{
intcharPosition = ALPHABET.indexOf(plainText.charAt(i));
intkeyVal = (shiftKey+charPosition)%26;
charreplaceVal = this.ALPHABET.charAt(keyVal);
cipherText += replaceVal;
}
returncipherText;
}
public String decrypt(String cipherText, intshiftKey) {
cipherText = cipherText.toLowerCase();
String plainText="";
for(inti=0;i<cipherText.length();i++)
{
intcharPosition = this.ALPHABET.indexOf(cipherText.charAt(i));
intkeyVal = (charPosition-shiftKey)%26;
if(keyVal<0) {
keyVal = this.ALPHABET.length()+ keyVal;
}
charreplaceVal = this.ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
returnplainText;
}
}
classCaesarDemo {
public static void main(String args[]){
String plainText = "ankitayyer";
intshiftKey=3;
CaesarCipher cc = new CaesarCipher();
String cipherText = cc.encrypt(plainText,shiftKey);
System.out.println("Your Plain Text :" + plainText);
System.out.println("Your Cipher Text :" + cipherText);
String cPlainText = cc.decrypt (cipherText,shiftKey);
System.out.println("Your Plain Text :" + cPlainText);
}
}
Output:
B. Modified Caesar Cipher:
Code:
import java.io.*;
classcaesarcipher
{
public String encrypt(int shift, String line)
{
String result=" ";
int offset;
for(inti=0;i<line.length();i++)
{
offset=((int)line.charAt(i)+shift)%256;
result+=(char)(offset);
}
return result;
}
public String decrypt(int shift, String line)
{ String result=" ";
int offset;
for(inti=0;i<line.length();i++)
{
offset=((int)line.charAt(i)-shift)%256;
if(offset<0)
offset+=256;
result+=(char)(offset);
}
return result;
}
public static void main(String args[])throws IOException
{
caesarcipherobj=new caesarcipher();
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in));
int choice;
System.out.println("Menu:n1: Encryptionn2: Decryption");
choice=Integer.parseInt(in.readLine());
System.out.println("Enter the shift: ");
int shift=Integer.parseInt(in.readLine());
System.out.println("Enter the line: ");
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!");
break;
} } }
Output:
C. Mono Alphabetic Cipher:
Code:
Import java.util.*;
public class Monoalpha {
public void Stringencode() {
System.out.println("t"+"MonoAlphabatic ENCODING");
String inp,code=" ";
char c = 0;
System.out.println("Enter a string: ");
Scanner sc=new Scanner(System.in);
inp=sc.next();
System.out.println("Ener the key:");
int k=sc.nextInt();
intlen=inp.length();
for(inti=0;i<len;i++) {
int x=inp.charAt(i);
if(x>=97 && x<=122) {
x=x+k;
k++;
if(x>122) {
int temp=x-122;
x=97+temp-1; }
c=(char)x; }
code=code+c; }
System.out.println("Cipher is :"+code); }
public static void main(String[] args) {
Monoalphaobj=new Monoalpha ();
obj.Stringencode(); } }
Output:
A. Rail Fence Techniques:
Code:
import java.io.*;
public class railfence
{
public static void main(String args[])
{
String input="MohitAmichand";
String output=" ";
intlen=input.length();
System.out.println("Input Text : "+input);
for(inti=0;i<len;i+=2)
{
output+=input.charAt(i);
}
for (inti=1;i<len;i+=2)
{
output+=input.charAt(i);
}
System.out.println("Ciphered Text :"+output);
}
}
Output:
B. Vernam Cipher:
Code:
Import java.lang.Math;
public class VernamCipher
{
public static void main(String args[])
{
String text = new String("Mohit");
char[] arText = text.toCharArray();
String cipher = new String("XYZHG");
char[] arCipher = cipher.toCharArray();
char[] encoded = new char[5];
System.out.println("Encoded " + text + " to be... ");
for (inti = 0; i<arText.length; i++)
{
encoded[i] = (char) (arText[i] ^ arCipher[i]);
System.out.print(encoded[i]);
}
System.out.println("nDecoded to be... ");
for (inti = 0; i<encoded.length; i++)
{
char temp = (char) (encoded[i] ^ arCipher[i]);
System.out.print(temp);
}
} }
Output:
Code:
Import java.util.*;
Import java.math.BigInteger;
public class DiffieHellmanBigInt {
final static BigInteger one = new BigInteger("1");
public static void main(String args[]) {
Scanner stdin = new Scanner(System.in);
BigInteger p;
System.out.println("Enter the approximate value of p you want.");
String ans = stdin.next();
p = getNextPrime(ans);
System.out.println("Your prime is "+p+".");
System.out.println("Now, enter a number in between 2 and p-1");
BigInteger g = new BigInteger(stdin.next());
System.out.println("Person A: enter your secret number now.");
BigInteger a = new BigInteger(stdin.next());
BigIntegerresulta = g.modPow(a,p);
System.out.println("Person A sends to person B "+resulta+".");
System.out.println("Person B: enter your secret number now.");
BigInteger b = new BigInteger(stdin.next());
BigIntegerresultb = g.modPow(b,p);
System.out.println("Person B sends to person A "+resultb+".");
BigIntegerKeyACalculates = resultb.modPow(a,p);
BigIntegerKeyBCalculates = resulta.modPow(b,p);
System.out.println("A takes "+resultb+" raises it to the power "+a+" mod "+p);
System.out.println("The Key A calculates is "+KeyACalculates+".");
System.out.println("B takes "+resulta+" raises it to the power "+b+" mod "+p);
System.out.println("The Key B calculates is "+KeyBCalculates+".");
}
public static BigIntegergetNextPrime(String ans) {
BigInteger test = new BigInteger(ans);
while (!test.isProbablePrime(99))
test = test.add(one);
return test;
} }
Output:
Code:
Import java.security.*;
Import javax.crypto.*;
Import java.security.spec.*;
Import javax.crypto.spec.*;
public class StringEncrypter_SecretKey_DES {
Cipher ecipher;
Cipher dcipher;
String Encrypter_SecretKey_DES(SecretKey key, String algorithm) {
try {
ecipher = Cipher.getInstance(algorithm);
dcipher = Cipher.getInstance(algorithm);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
}
catch (Exception e) {
e.printStackTrace();
}
}
public String encrypt(String str) {
try {
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
return new sun.misc.BASE64Encoder().encode(enc);
}
catch (Exception e) {
e.printStackTrace();
}
return null; }
public String decrypt(String str) {
try {
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
}
catch (Exception e) {
e.printStackTrace();
}
return null; }
public static void testUsingSecretKey(){
try {
System.out.println();
System.out.println("+----------------------------------------+");
System.out.println("| -- Test Using Secret Key Method -- |");
System.out.println("+----------------------------------------+");
System.out.println();
String secretString = "MohitAmichand";
SecretKeydesKey = KeyGenerator.getInstance("DES").generateKey();
StringEncrypter_SecretKey_DESdesEncrypter = new StringEncrypter_SecretKey_DES(desKey,
desKey.getAlgorithm());
String desEncrypted = desEncrypter.encrypt(secretString);
String desDecrypted = desEncrypter.decrypt(desEncrypted);
System.out.println(desKey.getAlgorithm() + " Encryption algorithm");
System.out.println(" Original String : " + secretString);
System.out.println(" Encrypted String : " + desEncrypted);
System.out.println(" Decrypted String : " + desDecrypted);
System.out.println();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
testUsingSecretKey(); }
}
Output:
Code:
Import java.security.*;
Import javax.crypto.*;
import java.io.*;
public class AES_StringEncrypter{
Cipher ecipher;
Cipher dcipher;
publicAES_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 { byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
return new sun.misc.BASE64Encoder().encode(enc);
}
catch(Exception e) {}
return null;
}
public String decrypt(String str) {
try { byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
}
catch(Exception e) {}
return null;
}
public static void main(String args[]){
SecretKey key=null;
try {
KeyGeneratorkeyGen = KeyGenerator.getInstance("AES");
key = keyGen.generateKey();
}
catch (Exception e)
{
e.printStackTrace();}
AES_StringEncrypter dese = new AES_StringEncrypter(key);
String o = "MohitAmichand";
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:
Code:
Import java.math.*;
classRSADemo {
RSADemo() {
BigInteger one = new BigInteger("1");
BigInteger zero = new BigInteger("0");
BigInteger p = new BigInteger("7");
BigInteger q = new BigInteger("19");
BigInteger N = p.multiply(q);
BigInteger temp1 = p.subtract(new BigInteger("1")); //(p-1)
BigInteger temp2 = q.subtract(new BigInteger("1")); //(q-1)
BigInteger m = temp1.multiply(temp2);
BigInteger e;
e = new BigInteger("2");
while (true) {
BigInteger res = m.gcd(e);
if (res.toString().equals("1")) {
break;
}
e = e.add(one);
}
BigInteger d=null;
BigInteger n1 = new BigInteger("0");
while (true) {
if
((one.add(n1.multiply(m))).remainder(e).toString().equals(zero.toString())) {
d = (one.add(n1.multiply(m))).divide(e);
break;
}
n1 = n1.add(one);
}
System.out.println("p = "+p+",q = "+q+"n");
System.out.println("N = p * q = "+N+"n");
System.out.println("m = (p-1) * (q-1) = "+m+"n");
System.out.println("e = "+e+"n");
System.out.println("d = (1 + (n1 * m))/e = "+d+"n");
System.out.println("Public Key --> ("+N+","+e+")"+"n");
System.out.println("Secret Key --> ("+N+","+d+")"+"n");
BigInteger P = new BigInteger("116");
BigInteger C = P.modPow(e,N);
System.out.println("Given plain text is 116");
System.out.println("Encryption:"+C+"n");
P = C.modPow(d,N);
System.out.println("Decryption:"+P+"n");
}
public static void main(String args[]) {
newRSADemo();
}}
Output:
Code:
Import java.security.*;
Import javax.crypto.*;
Import java.security.spec.*;
Import javax.crypto.spec.*;
public class RC4Algo {
Cipher ecipher;
Cipher dcipher;
RC4Algo(SecretKey key, String algorithm) {
try {
ecipher = Cipher.getInstance(algorithm);
dcipher = Cipher.getInstance(algorithm);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
}
catch (Exception e) {
e.printStackTrace();
}
}
public String encrypt(String str) {
try {
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
return new sun.misc.BASE64Encoder().encode(enc);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String decrypt(String str) {
try {
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void testUsingSecretKey() {
try {
System.out.println();
System.out.println("+----------------------------------------+");
System.out.println("| -- Test Using Secret Key Method -- |");
System.out.println("+----------------------------------------+");
System.out.println();
String secretString = "MohitAmichand";
SecretKeydesedeKey =KeyGenerator.getInstance("RC4").generateKey();
RC4Algo desedeEncrypter = new
RC4Algo(desedeKey, desedeKey.getAlgorithm());
String desedeEncrypted = desedeEncrypter.encrypt(secretString);
String desedeDecrypted = desedeEncrypter.decrypt(desedeEncrypted);
System.out.println(desedeKey.getAlgorithm() + " Encryption algorithm");
System.out.println(" Original String : " + secretString);
System.out.println(" Encrypted String : " + desedeEncrypted);
System.out.println(" Decrypted String : " + desedeDecrypted);
System.out.println();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
testUsingSecretKey();
}
}
Output:
Program:
importjava.security.*;
importjavax.crypto.*;
importjava.security.spec.*;
importjavax.crypto.spec.*;
public class StringEncrypter_SecretKey_BlowFish{
Cipher ecipher;
Cipher dcipher;
StringEncrypter_SecretKey_BlowFish(SecretKey key, String algorithm) {
try{
ecipher = Cipher.getInstance(algorithm);
dcipher = Cipher.getInstance(algorithm);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
}
catch (Exception e){
e.printStackTrace();}
}
public String encrypt(String str) {
try{
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
return new sun.misc.BASE64Encoder().encode(enc);
}
catch (Exception e) {
e.printStackTrace();}
return null; }
public String decrypt(String str) {
try{
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
}
catch (Exception e) {
e.printStackTrace();}
return null;
}
public static void testUsingSecretKey() {
try {
System.out.println();
System.out.println("+----------------------------------------+");
System.out.println("| -- Test Using Secret Key Method -- |");
System.out.println("+----------------------------------------+");
System.out.println();
String secretString = "MohitAmichand";
SecretKeyblowfishKey = KeyGenerator.getInstance("Blowfish").generateKey();
StringEncrypter_SecretKey_BlowFishblowfishEncrypter = new
StringEncrypter_SecretKey_BlowFish(blowfishKey, blowfishKey.getAlgorithm());
String blowfishEncrypted =blowfishEncrypter.encrypt(secretString);
String blowfishDecrypted =blowfishEncrypter.decrypt(blowfishEncrypted);
System.out.println(blowfishKey.getAlgorithm() + " Encryption algorithm");
System.out.println(" Original String : " + secretString);
System.out.println(" Encrypted String : " + blowfishEncrypted);
System.out.println(" Decrypted String : " + blowfishDecrypted);
System.out.println();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();}
}
public static void main(String[] args) {
testUsingSecretKey();
}
}
Output:

More Related Content

What's hot

Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance PuzzlersDoug Hawkins
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical filePranav Ghildiyal
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 
The Ring programming language version 1.6 book - Part 32 of 189
The Ring programming language version 1.6 book - Part 32 of 189The Ring programming language version 1.6 book - Part 32 of 189
The Ring programming language version 1.6 book - Part 32 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.5.4 book - Part 10 of 185The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.5.4 book - Part 10 of 185Mahmoud Samir Fayed
 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03Nasir Mehmood
 
Lisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligenceLisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligenceArtiSolanki5
 
The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184Mahmoud Samir Fayed
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoThe Software House
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objectsHusain Dalal
 
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBTDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBtdc-globalcode
 
Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17LogeekNightUkraine
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013aleks-f
 

What's hot (20)

Cpds lab
Cpds labCpds lab
Cpds lab
 
C programs
C programsC programs
C programs
 
Networking Core Concept
Networking Core ConceptNetworking Core Concept
Networking Core Concept
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
 
CBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical fileCBSE Class XII Comp sc practical file
CBSE Class XII Comp sc practical file
 
Introduzione a C#
Introduzione a C#Introduzione a C#
Introduzione a C#
 
Collection v3
Collection v3Collection v3
Collection v3
 
C# - What's next
C# - What's nextC# - What's next
C# - What's next
 
The Ring programming language version 1.6 book - Part 32 of 189
The Ring programming language version 1.6 book - Part 32 of 189The Ring programming language version 1.6 book - Part 32 of 189
The Ring programming language version 1.6 book - Part 32 of 189
 
PathOfMostResistance
PathOfMostResistancePathOfMostResistance
PathOfMostResistance
 
The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.5.4 book - Part 10 of 185The Ring programming language version 1.5.4 book - Part 10 of 185
The Ring programming language version 1.5.4 book - Part 10 of 185
 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03
 
Lisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligenceLisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligence
 
The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184The Ring programming language version 1.5.3 book - Part 10 of 184
The Ring programming language version 1.5.3 book - Part 10 of 184
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objects
 
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDBTDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
TDC2016POA | Trilha .NET - CQRS e ES na prática com RavenDB
 
Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17Alexey Tsoy Meta Programming in C++ 16.11.17
Alexey Tsoy Meta Programming in C++ 16.11.17
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013
 

Viewers also liked

Practical Encryption Tips and Tools
Practical Encryption Tips and ToolsPractical Encryption Tips and Tools
Practical Encryption Tips and ToolsHeidi Alexander
 
Watermarking & Encryption
Watermarking & EncryptionWatermarking & Encryption
Watermarking & EncryptionHossam Halapi
 
Teaching With Thinking Graphics
Teaching With Thinking GraphicsTeaching With Thinking Graphics
Teaching With Thinking Graphicsjgentile
 
A Brief History of Cryptography
A Brief History of CryptographyA Brief History of Cryptography
A Brief History of Cryptographyguest9006ab
 
Cryptography - A Brief History
Cryptography - A Brief HistoryCryptography - A Brief History
Cryptography - A Brief Historyprasenjeetd
 
Project Method of Teaching
 Project Method of Teaching Project Method of Teaching
Project Method of TeachingMandeep Gill
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedSlideShare
 
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011photomatt
 
The Great State of Design with CSS Grid Layout and Friends
The Great State of Design with CSS Grid Layout and FriendsThe Great State of Design with CSS Grid Layout and Friends
The Great State of Design with CSS Grid Layout and FriendsStacy Kvernmo
 

Viewers also liked (11)

Teaching F#
Teaching F#Teaching F#
Teaching F#
 
Practical Encryption Tips and Tools
Practical Encryption Tips and ToolsPractical Encryption Tips and Tools
Practical Encryption Tips and Tools
 
Watermarking & Encryption
Watermarking & EncryptionWatermarking & Encryption
Watermarking & Encryption
 
Teaching With Thinking Graphics
Teaching With Thinking GraphicsTeaching With Thinking Graphics
Teaching With Thinking Graphics
 
A Brief History of Cryptography
A Brief History of CryptographyA Brief History of Cryptography
A Brief History of Cryptography
 
Cryptography - A Brief History
Cryptography - A Brief HistoryCryptography - A Brief History
Cryptography - A Brief History
 
Cryptography
Cryptography Cryptography
Cryptography
 
Project Method of Teaching
 Project Method of Teaching Project Method of Teaching
Project Method of Teaching
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-Presented
 
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011
 
The Great State of Design with CSS Grid Layout and Friends
The Great State of Design with CSS Grid Layout and FriendsThe Great State of Design with CSS Grid Layout and Friends
The Great State of Design with CSS Grid Layout and Friends
 

Similar to Network security

Similar to Network security (20)

Java binary subtraction
Java binary subtractionJava binary subtraction
Java binary subtraction
 
Network security
Network securityNetwork security
Network security
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Cifrado cesar
Cifrado cesarCifrado cesar
Cifrado cesar
 
Java programs
Java programsJava programs
Java programs
 
Ac2
Ac2Ac2
Ac2
 
IT6712 lab manual
IT6712 lab manualIT6712 lab manual
IT6712 lab manual
 
The Art of Clean Code
The Art of Clean CodeThe Art of Clean Code
The Art of Clean Code
 
Dotnet 18
Dotnet 18Dotnet 18
Dotnet 18
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Oot practical
Oot practicalOot practical
Oot practical
 
Anti patterns
Anti patternsAnti patterns
Anti patterns
 
Pnno
PnnoPnno
Pnno
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 
Backdoor coding
Backdoor codingBackdoor coding
Backdoor coding
 
IT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdf
IT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdfIT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdf
IT8761-SECURITY LABORATORY-590519304-IT8761 security labmanual.pdf
 
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While TestingQA Fest 2019. Saar Rachamim. Developing Tools, While Testing
QA Fest 2019. Saar Rachamim. Developing Tools, While Testing
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 

Recently uploaded

Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resourcesdimpy50
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxShajedul Islam Pavel
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfTamralipta Mahavidyalaya
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxEduSkills OECD
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...Denish Jangid
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFVivekanand Anglo Vedic Academy
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxssuserbdd3e8
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptxmansk2
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxRaedMohamed3
 

Recently uploaded (20)

Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 

Network security

  • 1. A. Caesar Cipher: Code: import java.io.*; importjava.math.*; classCaesarCipher { private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; public String encrypt(String plainText, intshiftKey) { plainText = plainText.toLowerCase(); String cipherText=""; for(inti=0;i<plainText.length();i++) { intcharPosition = ALPHABET.indexOf(plainText.charAt(i)); intkeyVal = (shiftKey+charPosition)%26; charreplaceVal = this.ALPHABET.charAt(keyVal); cipherText += replaceVal; } returncipherText; } public String decrypt(String cipherText, intshiftKey) { cipherText = cipherText.toLowerCase(); String plainText=""; for(inti=0;i<cipherText.length();i++) { intcharPosition = this.ALPHABET.indexOf(cipherText.charAt(i)); intkeyVal = (charPosition-shiftKey)%26; if(keyVal<0) {
  • 2. keyVal = this.ALPHABET.length()+ keyVal; } charreplaceVal = this.ALPHABET.charAt(keyVal); plainText += replaceVal; } returnplainText; } } classCaesarDemo { public static void main(String args[]){ String plainText = "ankitayyer"; intshiftKey=3; CaesarCipher cc = new CaesarCipher(); String cipherText = cc.encrypt(plainText,shiftKey); System.out.println("Your Plain Text :" + plainText); System.out.println("Your Cipher Text :" + cipherText); String cPlainText = cc.decrypt (cipherText,shiftKey); System.out.println("Your Plain Text :" + cPlainText); } }
  • 4. B. Modified Caesar Cipher: Code: import java.io.*; classcaesarcipher { public String encrypt(int shift, String line) { String result=" "; int offset; for(inti=0;i<line.length();i++) { offset=((int)line.charAt(i)+shift)%256; result+=(char)(offset); } return result; } public String decrypt(int shift, String line) { String result=" "; int offset; for(inti=0;i<line.length();i++) { offset=((int)line.charAt(i)-shift)%256; if(offset<0) offset+=256; result+=(char)(offset);
  • 5. } return result; } public static void main(String args[])throws IOException { caesarcipherobj=new caesarcipher(); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int choice; System.out.println("Menu:n1: Encryptionn2: Decryption"); choice=Integer.parseInt(in.readLine()); System.out.println("Enter the shift: "); int shift=Integer.parseInt(in.readLine()); System.out.println("Enter the line: "); 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!"); break; } } }
  • 7. C. Mono Alphabetic Cipher: Code: Import java.util.*; public class Monoalpha { public void Stringencode() { System.out.println("t"+"MonoAlphabatic ENCODING"); String inp,code=" "; char c = 0; System.out.println("Enter a string: "); Scanner sc=new Scanner(System.in); inp=sc.next(); System.out.println("Ener the key:"); int k=sc.nextInt(); intlen=inp.length(); for(inti=0;i<len;i++) { int x=inp.charAt(i); if(x>=97 && x<=122) { x=x+k; k++; if(x>122) { int temp=x-122; x=97+temp-1; } c=(char)x; } code=code+c; } System.out.println("Cipher is :"+code); }
  • 8. public static void main(String[] args) { Monoalphaobj=new Monoalpha (); obj.Stringencode(); } } Output: A. Rail Fence Techniques:
  • 9. Code: import java.io.*; public class railfence { public static void main(String args[]) { String input="MohitAmichand"; String output=" "; intlen=input.length(); System.out.println("Input Text : "+input); for(inti=0;i<len;i+=2) { output+=input.charAt(i); } for (inti=1;i<len;i+=2) { output+=input.charAt(i); } System.out.println("Ciphered Text :"+output); } }
  • 11. Code: Import java.lang.Math; public class VernamCipher { public static void main(String args[]) { String text = new String("Mohit"); char[] arText = text.toCharArray(); String cipher = new String("XYZHG"); char[] arCipher = cipher.toCharArray(); char[] encoded = new char[5]; System.out.println("Encoded " + text + " to be... "); for (inti = 0; i<arText.length; i++) { encoded[i] = (char) (arText[i] ^ arCipher[i]); System.out.print(encoded[i]); } System.out.println("nDecoded to be... "); for (inti = 0; i<encoded.length; i++) { char temp = (char) (encoded[i] ^ arCipher[i]); System.out.print(temp); } } }
  • 13. Import java.util.*; Import java.math.BigInteger; public class DiffieHellmanBigInt { final static BigInteger one = new BigInteger("1"); public static void main(String args[]) { Scanner stdin = new Scanner(System.in); BigInteger p; System.out.println("Enter the approximate value of p you want."); String ans = stdin.next(); p = getNextPrime(ans); System.out.println("Your prime is "+p+"."); System.out.println("Now, enter a number in between 2 and p-1"); BigInteger g = new BigInteger(stdin.next()); System.out.println("Person A: enter your secret number now."); BigInteger a = new BigInteger(stdin.next()); BigIntegerresulta = g.modPow(a,p); System.out.println("Person A sends to person B "+resulta+"."); System.out.println("Person B: enter your secret number now."); BigInteger b = new BigInteger(stdin.next()); BigIntegerresultb = g.modPow(b,p); System.out.println("Person B sends to person A "+resultb+"."); BigIntegerKeyACalculates = resultb.modPow(a,p); BigIntegerKeyBCalculates = resulta.modPow(b,p); System.out.println("A takes "+resultb+" raises it to the power "+a+" mod "+p);
  • 14. System.out.println("The Key A calculates is "+KeyACalculates+"."); System.out.println("B takes "+resulta+" raises it to the power "+b+" mod "+p); System.out.println("The Key B calculates is "+KeyBCalculates+"."); } public static BigIntegergetNextPrime(String ans) { BigInteger test = new BigInteger(ans); while (!test.isProbablePrime(99)) test = test.add(one); return test; } } Output: Code:
  • 15. Import java.security.*; Import javax.crypto.*; Import java.security.spec.*; Import javax.crypto.spec.*; public class StringEncrypter_SecretKey_DES { Cipher ecipher; Cipher dcipher; String Encrypter_SecretKey_DES(SecretKey key, String algorithm) { try { ecipher = Cipher.getInstance(algorithm); dcipher = Cipher.getInstance(algorithm); ecipher.init(Cipher.ENCRYPT_MODE, key); dcipher.init(Cipher.DECRYPT_MODE, key); } catch (Exception e) { e.printStackTrace(); } } public String encrypt(String str) { try { byte[] utf8 = str.getBytes("UTF8"); byte[] enc = ecipher.doFinal(utf8); return new sun.misc.BASE64Encoder().encode(enc); } catch (Exception e) {
  • 16. e.printStackTrace(); } return null; } public String decrypt(String str) { try { byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); byte[] utf8 = dcipher.doFinal(dec); return new String(utf8, "UTF8"); } catch (Exception e) { e.printStackTrace(); } return null; } public static void testUsingSecretKey(){ try { System.out.println(); System.out.println("+----------------------------------------+"); System.out.println("| -- Test Using Secret Key Method -- |"); System.out.println("+----------------------------------------+"); System.out.println(); String secretString = "MohitAmichand"; SecretKeydesKey = KeyGenerator.getInstance("DES").generateKey(); StringEncrypter_SecretKey_DESdesEncrypter = new StringEncrypter_SecretKey_DES(desKey, desKey.getAlgorithm());
  • 17. String desEncrypted = desEncrypter.encrypt(secretString); String desDecrypted = desEncrypter.decrypt(desEncrypted); System.out.println(desKey.getAlgorithm() + " Encryption algorithm"); System.out.println(" Original String : " + secretString); System.out.println(" Encrypted String : " + desEncrypted); System.out.println(" Decrypted String : " + desDecrypted); System.out.println(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } public static void main(String[] args) { testUsingSecretKey(); } }
  • 19. Import java.security.*; Import javax.crypto.*; import java.io.*; public class AES_StringEncrypter{ Cipher ecipher; Cipher dcipher; publicAES_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 { byte[] utf8 = str.getBytes("UTF8"); byte[] enc = ecipher.doFinal(utf8); return new sun.misc.BASE64Encoder().encode(enc); } catch(Exception e) {} return null; } public String decrypt(String str) { try { byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
  • 20. byte[] utf8 = dcipher.doFinal(dec); return new String(utf8, "UTF8"); } catch(Exception e) {} return null; } public static void main(String args[]){ SecretKey key=null; try { KeyGeneratorkeyGen = KeyGenerator.getInstance("AES"); key = keyGen.generateKey(); } catch (Exception e) { e.printStackTrace();} AES_StringEncrypter dese = new AES_StringEncrypter(key); String o = "MohitAmichand"; 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); } }
  • 22. Import java.math.*; classRSADemo { RSADemo() { BigInteger one = new BigInteger("1"); BigInteger zero = new BigInteger("0"); BigInteger p = new BigInteger("7"); BigInteger q = new BigInteger("19"); BigInteger N = p.multiply(q); BigInteger temp1 = p.subtract(new BigInteger("1")); //(p-1) BigInteger temp2 = q.subtract(new BigInteger("1")); //(q-1) BigInteger m = temp1.multiply(temp2); BigInteger e; e = new BigInteger("2"); while (true) { BigInteger res = m.gcd(e); if (res.toString().equals("1")) { break; } e = e.add(one); } BigInteger d=null; BigInteger n1 = new BigInteger("0"); while (true) { if ((one.add(n1.multiply(m))).remainder(e).toString().equals(zero.toString())) {
  • 23. d = (one.add(n1.multiply(m))).divide(e); break; } n1 = n1.add(one); } System.out.println("p = "+p+",q = "+q+"n"); System.out.println("N = p * q = "+N+"n"); System.out.println("m = (p-1) * (q-1) = "+m+"n"); System.out.println("e = "+e+"n"); System.out.println("d = (1 + (n1 * m))/e = "+d+"n"); System.out.println("Public Key --> ("+N+","+e+")"+"n"); System.out.println("Secret Key --> ("+N+","+d+")"+"n"); BigInteger P = new BigInteger("116"); BigInteger C = P.modPow(e,N); System.out.println("Given plain text is 116"); System.out.println("Encryption:"+C+"n"); P = C.modPow(d,N); System.out.println("Decryption:"+P+"n"); } public static void main(String args[]) { newRSADemo(); }}
  • 25. Import java.security.*; Import javax.crypto.*; Import java.security.spec.*; Import javax.crypto.spec.*; public class RC4Algo { Cipher ecipher; Cipher dcipher; RC4Algo(SecretKey key, String algorithm) { try { ecipher = Cipher.getInstance(algorithm); dcipher = Cipher.getInstance(algorithm); ecipher.init(Cipher.ENCRYPT_MODE, key); dcipher.init(Cipher.DECRYPT_MODE, key); } catch (Exception e) { e.printStackTrace(); } } public String encrypt(String str) { try { byte[] utf8 = str.getBytes("UTF8"); byte[] enc = ecipher.doFinal(utf8); return new sun.misc.BASE64Encoder().encode(enc);
  • 26. } catch (Exception e) { e.printStackTrace(); } return null; } public String decrypt(String str) { try { byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); byte[] utf8 = dcipher.doFinal(dec); return new String(utf8, "UTF8"); } catch (Exception e) { e.printStackTrace(); } return null; } public static void testUsingSecretKey() { try { System.out.println(); System.out.println("+----------------------------------------+"); System.out.println("| -- Test Using Secret Key Method -- |"); System.out.println("+----------------------------------------+"); System.out.println();
  • 27. String secretString = "MohitAmichand"; SecretKeydesedeKey =KeyGenerator.getInstance("RC4").generateKey(); RC4Algo desedeEncrypter = new RC4Algo(desedeKey, desedeKey.getAlgorithm()); String desedeEncrypted = desedeEncrypter.encrypt(secretString); String desedeDecrypted = desedeEncrypter.decrypt(desedeEncrypted); System.out.println(desedeKey.getAlgorithm() + " Encryption algorithm"); System.out.println(" Original String : " + secretString); System.out.println(" Encrypted String : " + desedeEncrypted); System.out.println(" Decrypted String : " + desedeDecrypted); System.out.println(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } public static void main(String[] args) { testUsingSecretKey(); } }
  • 29. importjava.security.*; importjavax.crypto.*; importjava.security.spec.*; importjavax.crypto.spec.*; public class StringEncrypter_SecretKey_BlowFish{ Cipher ecipher; Cipher dcipher; StringEncrypter_SecretKey_BlowFish(SecretKey key, String algorithm) { try{ ecipher = Cipher.getInstance(algorithm); dcipher = Cipher.getInstance(algorithm); ecipher.init(Cipher.ENCRYPT_MODE, key); dcipher.init(Cipher.DECRYPT_MODE, key); } catch (Exception e){ e.printStackTrace();} } public String encrypt(String str) { try{ byte[] utf8 = str.getBytes("UTF8"); byte[] enc = ecipher.doFinal(utf8); return new sun.misc.BASE64Encoder().encode(enc); } catch (Exception e) { e.printStackTrace();}
  • 30. return null; } public String decrypt(String str) { try{ byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); byte[] utf8 = dcipher.doFinal(dec); return new String(utf8, "UTF8"); } catch (Exception e) { e.printStackTrace();} return null; } public static void testUsingSecretKey() { try { System.out.println(); System.out.println("+----------------------------------------+"); System.out.println("| -- Test Using Secret Key Method -- |"); System.out.println("+----------------------------------------+"); System.out.println(); String secretString = "MohitAmichand"; SecretKeyblowfishKey = KeyGenerator.getInstance("Blowfish").generateKey(); StringEncrypter_SecretKey_BlowFishblowfishEncrypter = new StringEncrypter_SecretKey_BlowFish(blowfishKey, blowfishKey.getAlgorithm()); String blowfishEncrypted =blowfishEncrypter.encrypt(secretString);
  • 31. String blowfishDecrypted =blowfishEncrypter.decrypt(blowfishEncrypted); System.out.println(blowfishKey.getAlgorithm() + " Encryption algorithm"); System.out.println(" Original String : " + secretString); System.out.println(" Encrypted String : " + blowfishEncrypted); System.out.println(" Decrypted String : " + blowfishDecrypted); System.out.println(); } catch (NoSuchAlgorithmException e) { e.printStackTrace();} } public static void main(String[] args) { testUsingSecretKey(); } }