Weitere ähnliche Inhalte Ähnlich wie Network security (20) Kürzlich hochgeladen (20) Network security1. 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();
}
}
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);