0% found this document useful (0 votes)
116 views3 pages

Klick Micro

The document is an information security laboratory record for a Master of Technology program. It lists 14 experiments conducted, including the implementation of symmetric cipher algorithms like AES and RC4. It also details experiments involving random number generation, RSA signature systems, Diffie-Hellman key exchange, and encryption techniques like El Gamal and Rabin cryptosystems. The record is being submitted by a student to fulfill requirements for an MTech in Information Technology.

Uploaded by

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

Klick Micro

The document is an information security laboratory record for a Master of Technology program. It lists 14 experiments conducted, including the implementation of symmetric cipher algorithms like AES and RC4. It also details experiments involving random number generation, RSA signature systems, Diffie-Hellman key exchange, and encryption techniques like El Gamal and Rabin cryptosystems. The record is being submitted by a student to fulfill requirements for an MTech in Information Technology.

Uploaded by

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

INFORMATION SECURITY LABORATORY RECORD 1. Implementation of symmetric cipher algorithm (AES & RC4).

UNIVERSITY COLLEGE OF ENGINEERING, SCIENCE AND TECHNOLOGY HYDERABAD


INFORMATION SECURITY LABORATORY RECORD JAWAHARLAL NEHRU TECHNOLGICAL UNIVERSITY HYDERABAD.
AES Code:
LIST OF EXPERIMENTS import javax.crypto.Cipher;
FOR Kukatpally, Hyderabad-500085. import javax.crypto.SecretKey;
S. NO NAME OF THE EXPERIMENT PAGE NO.
import javax.crypto.SecretKeyFactory;
M. TECH (CNIS) 1 Implementation of symmetric cipher algorithm (AES & RC4). 4-8
import javax.crypto.spec.IvParameterSpec;
1 YEAR, 1 SEMESTER 2 Random number generation using subset and digits and 9-10 import javax.crypto.spec.PBEKeySpec;
alphabets. import javax.crypto.spec.SecretKeySpec;
3 Implementation of RSA-based signature system. 11-12 import java.nio.charset.StandardCharsets;
4 Implementation of subset-sum. 13-14 import java.security.InvalidAlgorithmParameterException;
5 Authenticating the given signature using the MD5 hash 15-16
import java.security.InvalidKeyException;
algorithm.
6 Implementation of Diffie-Hellman algorithm. 17-18 import java.security.NoSuchAlgorithmException;
7 Implementation of the EL Gamal cryptosystem (Option) 19-21 import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
8 Implementation of Gold Wasser - Micali probabilistic public 22-26 import java.util.Base64;
key system import javax.crypto.BadPaddingException;
9 Implementation of Rabin Cryptosystem (Option) 27-31
import javax.crypto.IllegalBlockSizeException;
10 Implementation of Kerberos cryptosystem 32
import javax.crypto.NoSuchPaddingException;
CERTIFICATE 11 Firewall implementation and testing 33-35 public class AESExample
Submitted By 12 Implementation of a trusted secure web transaction 36-43 {
This is to certify that the record entitled “INFORMATION SECURITY LAB” is being submitted by Mr. 13 Cryptographic libraries sun JCE/Open SSL/Bouncy Castle JCE 44-45 private static final String SECRET_KEY = "123456789";
M. KLICK M. KLICK, Reg. No. 22031D6412 in partial fulfillment of the requirements for the award of the degree of
private static final String SALTVALUE = "abcdefg";
MASTER OF TECHNOLOGY in the DEPARTMENT OF INFORMATION TECHNOLOGY 14 Digital Certificate and Hybrid (ASSY/SY) encryption PKI 46-47
22031D6412 (FORMELY SCHOOL OF INFORMATION TECHNOLOGY), UNIVERSITY COLLEGE OF ENGINEERING,
public static String encrypt(String strToEncrypt)
SCIENCE AND TECHNOLOGY HYDERABAD, JAWAHARLAL NEHRU TECHNOLOGICAL 15 Message authentication codes 48 {
UNIVERSITY HYDERABAD is a record of bonafide work carried out by him under my guidance and 16 Elliptic curve cryptosystems (Option) 49-50 try
supervision. 17 PKCS standards (PKCS 1,5,11,12) 51-56 {
The results embodied in this record have been submitted to any other university or institution for award of byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
DEPARTMENT OF INFORMATION TECHNOLOGY any degree or diploma. IvParameterSpec ivspec = new IvParameterSpec(iv);
(FORMERLY SCHOOL OF INFORMATION TECHNOLOGY)
SecretKeyFactory factory =
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
UNIVERSITY COLLEGE OF ENGINEERING, SCIENCE AND TECHNOLOGY HYDERABAD. KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(),
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD. SALTVALUE.getBytes(), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
TELANGANA
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
(2022-2024) Internal Examiner External Examiner Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);

1 2 3 4

return String decryptedval = decrypt(encryptedval); for(int i=0;i<key.length();i++) Output:


Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes(Standard System.out.println("Original value: " + originalval); {
Charsets.UTF_8))); System.out.println("Encrypted value: " + encryptedval); keyi[i]=(int)keyc[i];
System.out.println("Decrypted value: " + decryptedval); }
}
for(int i=0;i<255;i++)
catch (InvalidAlgorithmParameterException | InvalidKeyException | } {
NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | } s[i]=i; k[i]=keyi[i%key.length()];
IllegalBlockSizeException | NoSuchPaddingException e) }
{ Output: int j=0;
System.out.println("Error occured during encryption: " + e.toString()); for(int i=0;i<255;i++)
} {
j=(j+s[i]+k[i])%256;
return null;
temp=s[i];
} s[i]=s[j];
public static String decrypt(String strToDecrypt) s[j]=temp;
{ }
try int i=0;
{ j=0;
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; RC4 Code: int z=0;
for(int l=0;l<ptext.length();l++)
IvParameterSpec ivspec = new IvParameterSpec(iv); import java.io. *; {
SecretKeyFactory factory = class rc4 i=(l+1)%256;
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); { j=(j+s[i])%256;
KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), temp=s[i];
SALTVALUE.getBytes(), 65536, 256); public static void main (String args[])throws IOException s[i]=s[j];
{ s[j]=temp;
SecretKey tmp = factory.generateSecret(spec);
int temp=0; z=s[(s[i]+s[j])%256];
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES"); String ptext; cipher[l]=z^ptexti[l];
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); String key; decrypt[l]=z^cipher[l];
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec); int s[]=new int[256]; }
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); int k[]=new int[256]; System.out.print("\n\nENCRYPTED:\t\t");
} BufferedReader in= new BufferedReader(new InputStreamReader(System.in)); display(cipher);
catch (InvalidAlgorithmParameterException | InvalidKeyException | System.out.print("\nENTER PLAIN TEXT\t"); System.out.print("\n\nDECRYPTED:\t\t");
ptext=in.readLine(); display(decrypt);
NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException |
System.out.print("\n\nENTER KEY TEXT\t\t"); }
IllegalBlockSizeException | NoSuchPaddingException e) key=in.readLine(); static void display(int disp[])
{ char ptextc[]=ptext.toCharArray(); {
System.out.println("Error occured during decryption: " + e.toString()); char keyc[]=key.toCharArray(); char convert[]=new char[disp.length];
} int cipher[]=new int[ptext.length()]; for(int l=0;l<disp.length;l++)
return null; int decrypt[]=new int[ptext.length()]; {
} int ptexti[]=new int[ptext.length()]; convert[l]=(char)disp[l];
int keyi[]=new int[key.length()]; System.out.print(convert[l]);
public static void main(String[] args)
for(int i=0;i<ptext.length();i++) }
{ { }
String originalval = "AES Encryption"; ptexti[i]=(int)ptextc[i]; }
String encryptedval = encrypt(originalval); }
5 6 7 8

System.out.println("Decrypted message is : "+ msgback);


Code: Random Alphabet generation. 3. Implementation of RSA-based signature system. }
2. Random number generation using subset and digits and alphabets. import java.util.*; static int gcd(int e, int z)
class GFG {
Code:
Code: Random number generation. { import java.math.*;
if (e == 0)
import java.util.*; static int MAX = 26; return z;
import java.util.*;
public class RandomNumberGeneration{ static String printRandomString(int n) else
public static void main(String args[]){ { return gcd(z % e, e);
class RSA {
// create random object char []alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', }
public static void main(String args[])
Random randomno=new Random(); 'h', 'i', 'j', 'k', 'l', 'm', 'n', }
{
Scanner sc=new Scanner(System.in); 'o', 'p', 'q', 'r', 's', 't', 'u', int p, q, n, z, d = 0, e, i;
System.out.println("How many random numbers do you need :"); 'v', 'w', 'x', 'y', 'z' }; int msg = 12; Output:
int n=sc.nextInt(); String res = ""; double c;
for(int i=0;i<n;i++){ for (int i = 0; i< n; i++) BigInteger msgback;
System.out.println(randomno.nextInt()); res = res + alphabet[(int) (Math.random() * 100 % MAX)]; p = 3;
} return res; q = 11;
} } n = p * q;
public static void main(String[] args) z = (p - 1) * (q - 1);
{ System.out.println("the value of z = " + z);
int n = 10; for (e = 2; e < z; e++) {
System.out.print(printRandomString(n)); if (gcd(e, z) == 1) {
} break;
} }
}
Output: System.out.println("the value of e = " + e);
for (i = 0; i <= 9; i++) {
int x = 1 + (i * z);
if (x % e == 0)
{
d = x / e;
break;
}
}
System.out.println("the value of d = " + d);
c = (Math.pow(msg, e)) % n;
System.out.println("Encrypted message is : " + c);
BigInteger N = BigInteger.valueOf(n);
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);

9 10 11 12

4. Implementation of subset-sum. 5. Authenticating the given signature using the MD5 hash algorithm. Output:
Output:
Code: Code:
class SUBSETSUM { import java.math.BigInteger;
static boolean isSubsetSum(int set[], import java.security.MessageDigest;
int n, int sum) import java.security.NoSuchAlgorithmException;
{ public class MD5Example
// Base Cases {
if (sum == 0) //hash function to get the md5 hash
return true; public static String getMd5Hash(String input)
if (n == 0 && sum != 0) {
return false; try
{
// If last element is greater than //static getInstance() method is called with hashing MD5
// sum, then ignore it MessageDigest md = MessageDigest.getInstance("MD5");
if (set[n - 1] > sum) //calculating message digest of an input that return array of byte
return isSubsetSum(set, n - 1, sum); byte[] messageDigest = md.digest(input.getBytes());
//converting byte array into signum representation
/* else, check if sum can be obtained BigInteger no = new BigInteger(1, messageDigest);
by any of the following //converting message digest into hex value
(a) including the last element String hashtext = no.toString(16);
(b) excluding the last element */ while (hashtext.length() < 32)
return isSubsetSum(set, n - 1, sum) || isSubsetSum(set, n - 1, sum - set[n - 1]); {
} hashtext = "0" + hashtext;
}
public static void main(String args[]) return hashtext;
{ }
int set[] = { 3, 34, 4, 12, 5, 2 }; //for specifying wrong message digest algorithms
int sum = 9; catch (NoSuchAlgorithmException e)
int n = set.length; {
if (isSubsetSum(set, n, sum) == true) throw new RuntimeException(e);
System.out.println("Found a subset" }}
+ " with given sum"); //driver code
else public static void main(String args[]) throws NoSuchAlgorithmException
System.out.println("No subset with" {
+ " given sum"); String s = "javatpoint";
} System.out.println("HashCode Generated for the string is: " + getMd5Hash(s));
} }}

13 14 15 16
{ }
6. Implementation of Diffie-Hellman algorithm. long result = 0; 7. Implementation of the EL Gamal cryptosystem. else
if (y == 1){ System.out.println("Sorry, a generator for your prime couldn't be found.");
return x; }
Code: Code:
} public static BigInteger getNextPrime(String ans) {
import java.util.*; import java.util.*;
else{ BigInteger one = new BigInteger("1");
// create class DiffieHellmanAlgorithmExample to calculate the key for two persons import java.math.BigInteger;
result = ((long)Math.pow(x, y)) % P; BigInteger test = new BigInteger(ans);
class DiffieHellmanAlgorithmExample {
return result; while (!test.isProbablePrime(99))
// main() method start public class ElGamal {
} test = test.add(one);
public static void main(String[] args) public static void main(String[] args) {
} return test;
{ Scanner stdin = new Scanner(System.in);
} }
long P, G, x, a, y, b, ka, kb; Random r = new Random();
public static BigInteger getGenerator(BigInteger p, Random r) {
// create Scanner class object to take input from user System.out.println("Enter the approximate value of the prime number for your El
Output: int numtries = 0;
Scanner sc = new Scanner(System.in); Gamal key.");
System.out.println("Both the users should be agreed upon the public keys G and P"); BigInteger p = getNextPrime(stdin.next());
while (numtries < 1000) {
// take inputs for public keys from the user BigInteger g = getGenerator(p, r);
BigInteger rand = new BigInteger(p.bitLength()+1,r);
System.out.println("Enter value for public key G:"); if (g != null) {
rand = rand.mod(p);
G = sc.nextLong(); BigInteger a = new BigInteger(p.bitLength()+1, r);
System.out.println("Enter value for public key P:"); a = a.mod(p);
BigInteger exp = BigInteger.ONE;
P = sc.nextLong(); if (a.equals(BigInteger.ZERO) || a.equals(BigInteger.ONE))
BigInteger next = rand;
// get input from user for private keys a and b selected by User1 and User2 a = a.add(new BigInteger("2"));
if (next.equals(BigInteger.ZERO) || next.equals(BigInteger.ONE))
System.out.println("Enter value for private key a selected by user1:"); BigInteger b = g.modPow(a, p);
continue;
a = sc.nextLong(); System.out.println("Post p = "+p+" g = "+g+" b = "+b);
while (!next.equals(BigInteger.ONE)) {
System.out.println("Enter value for private key b selected by user2:"); BigInteger k = new BigInteger(p.bitLength()+1, r);
next = (next.multiply(rand)).mod(p);
b = sc.nextLong(); k = k.mod(p);
exp = exp.add(BigInteger.ONE);
// call calculatePower() method to generate x and y keys if (k.equals(BigInteger.ZERO) || k.equals(BigInteger.ONE))
}
x = calculatePower(G, a, P); k = k.add(new BigInteger("2"));
if (exp.equals(p.subtract(BigInteger.ONE)))
y = calculatePower(G, b, P); BigInteger c1 = g.modPow(k, p);
return rand;
// call calculatePower() method to generate ka and kb secret keys after the exchange of BigInteger c2 = b.modPow(k, p);
x and y keys System.out.println("Please enter your message. It should be in between 1 and "+p);
numtries++;
// calculate secret key for User1 BigInteger m = new BigInteger(stdin.next());
}
ka = calculatePower(y, a, P); c2 = c2.multiply(m);
return null;
// calculate secret key for User2 c2 = c2.mod(p);
}
kb = calculatePower(x, b, P); System.out.println("The corresponding cipher texts are c1 = "+c1+" c2 = "+c2);
// print secret keys of user1 and user2 BigInteger temp = c1.modPow(a,p);
}
System.out.println("Secret key for User1 is:" + ka); temp = temp.modInverse(p);
System.out.println("Secret key for User2 is:" + kb); System.out.println("Here is c1^ -a = "+temp);
} BigInteger recover = temp.multiply(c2);
// create calculatePower() method to find the value of x ^ y mod P recover = recover.mod(p);
private static long calculatePower(long x, long y, long P) System.out.println("The original message = "+recover);

17 18 19 20

Output: for(int PrKP.Pr2=PrimeGenerator();


8. Implementation of Gold Wasser - Micali probabilistic public key i=0;i<Ciphertext.length;i++)System.out.println("c"+i+":"+Ciphertext[i].toString(16)); return PrKP;
system. System.out.println("Time:"+format.format(new Date())); }
byte x; public static boolean LegendreSymbolCalculate(BigInteger integer,BigInteger
x=decrypt(Ciphertext,TestPr); Modulus)throws Exception{
Code:
System.out.println("Decrypted:"+x); int i=1,r=1;
import java.math.BigInteger;
System.out.println("Time:"+format.format(new Date())); BigInteger Num0=new BigInteger("0");
import java.text.DateFormat;
} BigInteger Num1=new BigInteger("1");
import java.text.SimpleDateFormat;
public static boolean[] ByteToBoolean(byte input){ if(integer.gcd(Modulus).compareTo(Num1)!=0)throw new Exception("The integer
import java.util.Date;
boolean[] output=new boolean[8]; and Modulus are not coprime");
import java.util.Random;
for(int i=output.length-1;i>=0;i--){ BigInteger Num2=new BigInteger("2");
if(input%2==0)output[i]=false; BigInteger Num8=new BigInteger("8");
public class GoldwasserMicali_Test1 {
else output[i]=true; BigInteger temp;
public final static int SecurityParameter=200;
input=(byte) (input>>>1); while(true){
public final static DateFormat format=new SimpleDateFormat("MM/dd HH:mm:ss");
} while(integer.mod(Num2).compareTo(Num0)==0){
public static class PublicKeyPair{
return output; integer=integer.divide(Num2);
BigInteger Modulus;
} i=i*(-1);
BigInteger Q_Nonresidue;
public static byte BooleanToByte(boolean[] input){ }
public void Print(){
byte output=0; if(((((Modulus.pow(2)).subtract(Num1)).divide(Num8)).mod(Num2)).compareTo(Nu
System.out.println("PublicKeyPair:\nModulus:"+Modulus.toString(16)+"\nQuadratic_
for(int i=0;i<input.length;i++){ m0)!=0)r=r*i;
Nonresidue:"+Q_Nonresidue.toString(16)+"\nTime:"+format.format(new
if(input[i])output++; //if the condition is false,then [(p^2)-1]/8 is an even number,i^[(p^2)-1]/8=1,no matter
Date())+"\n");
if(i!=input.length-1)output=(byte) (output<<1); i is 1 or -1
}}
} i=1;
public static class PrivateKeyPair{//PrKP.Pr1,PrKP.Pr2:Two PrivateKeys,factors of
return output; if(integer.compareTo(Num1)==0)break;
Modulus
} if(integer.compareTo(Modulus)<0){
BigInteger Pr1;
public static BigInteger PrimeGenerator(){ temp=integer.subtract(Num1).divide(Num2);
BigInteger Pr2;
BigInteger prime; temp=temp.multiply((Modulus.subtract(Num1).divide(Num2)));
public void Print(){
Random rnd=new Random(new Date().getTime()); if(temp.mod(Num2).compareTo(Num0)!=0)r=r*(-1);
System.out.println("PrivateKeyPair:\nPrivate factor1:"+Pr1.toString(16)+"\nPrivate
prime=BigInteger.probablePrime(SecurityParameter, rnd);//bitLength but not Decimal temp=integer;
factor2:"+Pr2.toString(16)+"\nTime:"+format.format(new Date())+"\n");
BigInteger exponent=prime.subtract(new BigInteger("1")); integer=Modulus;
}}
BigInteger a=new BigInteger("1147"); Modulus=temp;
public static void main(String[] args) throws Exception {
BigInteger r=a.modPow(exponent,prime);//Fermat little theorem }
PrivateKeyPair TestPr=PrKGenerator();
if(r.compareTo(new /*System.out.println("q:"+integer);
TestPr.Print();
BigInteger("1"))==0)System.out.println("a:"+a+";exponent:"+exponent.toString(16)+" System.out.println("p:"+Modulus);
PublicKeyPair TestPu=PuKGenerator(TestPr);
;prime:"+prime.toString(16)+";[a^(p-1)]mod p=r:"+r); System.out.println("r:"+r);*/
TestPu.Print();
return prime; integer=integer.mod(Modulus);
byte TestByte=(byte)57;
} }
System.out.println("Test
public static PrivateKeyPair PrKGenerator(){ if(r==1)return true;
byte:"+TestByte+"\nSecurityParameter:"+SecurityParameter+"\nEncrypted:");
PrivateKeyPair PrKP=new PrivateKeyPair(); else return false;
BigInteger[] Ciphertext=encrypt(TestByte,TestPu);
PrKP.Pr1=PrimeGenerator(); }
21 22 23 24

public static BigInteger CoprimeGenerator(BigInteger Modulus,int Length){ if(RawData[i])Ciphertext[i]=integer.pow(2).mod(PuKP.Modulus); public static BigInteger[] decrypt(BigInteger c,
BigInteger CoprimeNum; else 9. Implementation of Rabin Cryptosystem. BigInteger p,
//BigInteger a,b,residue; Ciphertext[i]=integer.pow(2).multiply(PuKP.Q_Nonresidue).mod(PuKP.Modulus);
Random rnd; } BigInteger q)
Code:
do{ return Ciphertext;
rnd=new Random(new Date().getTime()); {
} import java.math.BigInteger;
CoprimeNum=BigInteger.probablePrime(Length, rnd); public static byte decrypt(BigInteger[] Ciphertext,PrivateKeyPair PrKP) throws BigInteger n = p.multiply(q);
/*a=Modulus; Exception{ import java.nio.charset.Charset;
b=CoprimeNum; byte Plaintext; BigInteger p1 = c.modPow(p.add(BigInteger.ONE).divide(fourth),p);
import java.security.SecureRandom;
residue=new BigInteger("0"); boolean a=false,b=false; BigInteger p2 = p.subtract(p1);
do{ boolean[] RawData=new boolean[8]; import java.util.Random;
residue=a.mod(b); for(int i=0;i<Ciphertext.length;i++){ BigInteger q1 = c.modPow(q.add(BigInteger.ONE).divide(fourth),q);
a=b; a=LegendreSymbolCalculate(Ciphertext[i],PrKP.Pr1); BigInteger q2 = q.subtract(q1);
b=residue; b=LegendreSymbolCalculate(Ciphertext[i],PrKP.Pr2); class CryptographyClassic {
}while(residue.compareTo(new BigInteger("0"))==0); if(a&&b)RawData[i]=true; BigInteger[] ext = Gcd(p, q);
private static Random random = new SecureRandom();
}while(a.compareTo(new BigInteger("1"))!=0);*/ else RawData[i]=false;
BigInteger yp = ext[1];
}while(CoprimeNum.gcd(Modulus).compareTo(new BigInteger("1"))!=0);//already } private static BigInteger second = BigInteger.valueOf(2);
had a gcd algorithm Plaintext=BooleanToByte(RawData); BigInteger yq = ext[2];
private static BigInteger third = BigInteger.valueOf(3);
return CoprimeNum; return Plaintext;
BigInteger d1 = yp.multiply(p).multiply(q1).add(yq.multiply(q).multiply(p1)).mod(n);
} } private static BigInteger fourth = BigInteger.valueOf(4);
public static PublicKeyPair PuKGenerator(PrivateKeyPair PrKP) throws Exception{ } BigInteger d2 = yp.multiply(p).multiply(q2).add(yq.multiply(q).multiply(p1)).mod(n);
public static BigInteger[] generateKey(int Bitlength)
PublicKeyPair PuKP=new PublicKeyPair();
BigInteger d3 = yp.multiply(p).multiply(q1).add(yq.multiply(q).multiply(p2)).mod(n);
boolean a,b; Output: {
BigInteger Coprime; BigInteger d4 = yp.multiply(p).multiply(q2).add(yq.multiply(q).multiply(p2)).mod(n);
BigInteger p1 = blumPrime(Bitlength / 2);
PuKP.Modulus=PrKP.Pr1.multiply(PrKP.Pr2);
do{ return new BigInteger[] { d1, d2, d3, d4 };
BigInteger q1 = blumPrime(Bitlength / 2);
Coprime=CoprimeGenerator(PuKP.Modulus,PuKP.Modulus.bitLength()); }
a=LegendreSymbolCalculate(Coprime,PrKP.Pr1); BigInteger n = p1.multiply(q1);
b=LegendreSymbolCalculate(Coprime,PrKP.Pr2); public static BigInteger[] Gcd(BigInteger a, BigInteger b)
return new BigInteger[] { n, p1, q1 };
}while(a||b);//a||b then y may or may not be a pseudo quadratic residue,a&&b then y {
must be a pseudo quadratic residues }
PuKP.Q_Nonresidue=Coprime; BigInteger s = BigInteger.ZERO;
public static BigInteger encrypt(BigInteger m,
return PuKP; BigInteger olds = BigInteger.ONE;
} BigInteger n)
public static BigInteger[] encrypt(byte Plaintext,PublicKeyPair PuKP){ BigInteger t = BigInteger.ONE;
{
BigInteger[] Ciphertext=new BigInteger[8];
BigInteger oldt = BigInteger.ZERO;
BigInteger integer; return m.modPow(second, n);
boolean[] RawData=ByteToBoolean(Plaintext); BigInteger r = b;
}
for(int i=0;i<RawData.length;i++){
BigInteger oldr = a;
integer=CoprimeGenerator(PuKP.Modulus,PuKP.Modulus.bitLength());
25 26 27 28

while (!r.equals(BigInteger.ZERO)) { BigInteger[] form = CryptographyClassic.generateKey(512); Output:


BigInteger q = oldr.divide(r); BigInteger n = form[0]; 10. Implementation of Kerberos cryptosystem.

BigInteger tr = r; BigInteger p = form[1]; Code:


r = oldr.subtract(q.multiply(r)); BigInteger q = form[2]; import javax.crypto.*;
import javax.crypto.spec.*;
oldr = tr; String finalMessage = null; import java.security.*;
public class KerberosCryptosystem {
BigInteger ts = s; int i = 1;
public static void main(String[] args) throws Exception {
s = olds.subtract(q.multiply(s)); String s = "Hi Learners ,Welcome to Codespeedy!"; KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey key = keygenerator.generateKey();
olds = ts; System.out.println("Message sent by sender is : " + s); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
BigInteger tt = t; BigInteger m= new BigInteger(s.getBytes(Charset.forName("ascii")));
String plaintext = "This is a plaintext message.";
t = oldt.subtract(q.multiply(t)); BigInteger c = CryptographyClassic.encrypt(m, n); byte[] ciphertext = cipher.doFinal(plaintext.getBytes());
System.out.println("Encrypted message: " + new String(ciphertext));
oldt = tt; System.out.println("Encrypted Message is : " + c); cipher.init(Cipher.DECRYPT_MODE, key);
} BigInteger[] m2 = CryptographyClassic.decrypt(c, p, q); byte[] decryptedtext = cipher.doFinal(ciphertext);
System.out.println("Decrypted message: " + new String(decryptedtext));
return new BigInteger[] { oldr, olds, oldt }; for (BigInteger b : m2) { }
}
} String dec = new String(

public static BigInteger blumPrime(int bitLength) b.toByteArray(), Output:

{ Charset.forName("ascii"));

BigInteger p; if (dec.equals(s)) {

do { finalMessage = dec;

p = BigInteger.probablePrime(bitLength, random); }

} while (!p.mod(fourth).equals(third)); i++;

return p; }

} System.out.println(

} "Message received by The Receiver is : "+ finalMessage);

class RabinCryptoSystem { }

public static void main(String[] args) }

29 30 31 32
String cleartextFile = "cleartext.txt";
14. Message authentication codes. 15. Elliptic curve cryptosystems String ciphertextFile = "ciphertextECIES.txt";

byte[] block = new byte[64];


Code: Code:
FileInputStream fis = new FileInputStream(cleartextFile);
import java.security.Key; import java.io.FileInputStream;
FileOutputStream fos = new FileOutputStream(ciphertextFile);
import java.security.SecureRandom; import java.io.FileOutputStream;
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
import javax.crypto.KeyGenerator; import java.security.KeyPair;
int i;
import javax.crypto.Mac; import java.security.KeyPairGenerator;
while ((i = fis.read(block)) != -1) {
import java.security.PrivateKey;
cos.write(block, 0, i);
public class MacSample{ import java.security.Provider;
}
public static void main(String args[])throws Exception{ import java.security.PublicKey;
cos.close();
KeyGenerator keyGen=KeyGenerator.getInstance("DES"); import java.security.SecureRandom;
SecureRandom secRandom=new SecureRandom(); import java.security.Security;
// Decrypt
keyGen.init(secRandom); import java.security.Signature;
String cleartextAgainFile = "cleartextAgainECIES.txt";
Key key=keyGen.generateKey(); import java.security.spec.ECGenParameterSpec;
cipher.init(Cipher.DECRYPT_MODE, privKey, ecsp);
Mac mac=Mac.getInstance("HmacSHA256"); import java.security.spec.ECParameterSpec;
fis = new FileInputStream(ciphertextFile);
mac.init(key); import java.security.spec.EllipticCurve;
CipherInputStream cis = new CipherInputStream(fis, cipher);
String msg=new String("Hi how are you"); import javax.crypto.Cipher;
fos = new FileOutputStream(cleartextAgainFile);
byte[] bytes =msg.getBytes(); import javax.crypto.CipherInputStream;
while ((i = cis.read(block)) != -1) {
byte[] macResult=mac.doFinal(bytes); import javax.crypto.CipherOutputStream;
fos.write(block, 0, i);
System.out.println("Mac result:"); import javax.crypto.spec.DESKeySpec;
}
System.out.println(new String(macResult)); class TestECC {
fos.close();
}
} public static void main(String args[]) {
} catch (Exception e) {
try {
System.out.println(e);
Output: Provider p[] = Security.getProviders();
}
Provider p1 = Security.getProvider("well come");
}
System.out.println(p1.getName());
}
KeyPairGenerator kpg = KeyPairGenerator.getInstance("CSE", "Well come");
System.out.println(kpg.getAlgorithm());
Cipher cipher = Cipher.getInstance("DES");
System.out.println("provider=" + cipher.getProvider());
ECGenParameterSpec ecsp = new ECGenParameterSpec("sect163r2");
kpg.initialize(ecsp);
KeyPair kyp = kpg.genKeyPair();
PublicKey pubKey = kyp.getPublic();
PrivateKey privKey = kyp.getPrivate();
System.out.println(cipher.getProvider());
cipher.init(Cipher.ENCRYPT_MODE, pubKey);

43 44 45 50

57

You might also like