SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
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:

Weitere ähnliche Inhalte

Was ist angesagt?

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
Nasir Mehmood
 

Was ist angesagt? (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
 

Andere mochten auch

Andere mochten auch (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
 

Ähnlich wie Network security

Java programs
Java programsJava programs
Java programs
jojeph
 
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
DhanuskarSankar1
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Kürzlich hochgeladen (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

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(); } }