RSA Implementation
RSA Implementation
import java.math.BigInteger;
import java.util.Random;
class SimpleRSA {
p = BigInteger.probablePrime(512, myRandom);
q = BigInteger.probablePrime(512, myRandom);
// N = pq
N = p.multiply(q);
// v = (p-1)*(q-1)
v =(p.subtract(BigInteger.valueOf(1))).multiply(q.subtract(BigInteger.valueOf(1)));
k = new BigInteger("3");
d = k.modInverse(v);
// Encryption
// Decryption
}
Output
C:\Program Files\Java\jdk1.6.0_07\bin\java.exe -classpath "C:\Program
Files\Java\jdk1.6.0_07\jre\lib\rt.jar;C:\Program
Files\Java\jdk1.6.0_07\lib\tools.jar;J:\NMS" SimpleRSA
Value of
p:117686837408564885453421841793708809347228146689137677955112976839435485865199857879
50858001297977310173110996763173093591148833987835653326488053279071057
Value of
q:978416053767100196956358749326941175987797406696382576990251389340509263828043164595
8699693034030735729767115300016309578037907172526549932686180897765073
Value of
N:115146691037618399775015206138126645941683210886745085070661259313407657294202145378
90797867948401122300854224215832865062946648083833347474785936345525934024377290292344
29227303336683443297176106130949796163813268210456021393412412100478213769312776842115
92892679558448854882364554039142791087605376859792161
Value of
v:115146691037618399775015206138126645941683210886745085070661259313407657294202145378
90797867948401122300854224215832865062946648083833347474785936345525931869092862439595
24078245619957040370230098243591020228159130094682534981164407926139118192369456761656
90014567495259451713177812878780587828431142682956032
Value of k:5
Value of
d:460586764150473599100060824552506583766732843546980340282645037253630629176808581515
63191471793604489203416896863331460251786592335333389899143745382103727476371449758380
96312982479828161480920392974364080912636520378730139924657631704556472769477827046627
6005826998103780685271125151512235131372457073182413
Encrypted
data:878566493909273651566193128130976485690987993613060869654571672080342559863388160
62993171026906727449286884065773744742817094148101294006849261084472064961571993469872
929822783457
Decrypted data:pravesh bansal
Finished executing
DIGITAL SIGNATURE IMPLEMENTATION
class GenSig {
if (args.length != 1)
{
System.out.println("Usage: GenSig nameOfFileToSign");
}
else try
{
keyGen.initialize(1024, random);
dsa.initSign(priv);
sigfos.close();
keyfos.close();
} catch (Exception e) {
System.err.println("Caught exception " + e.toString());
}
};
}
VerSig.java – verifies the signature
import java.io.*;
import java.security.*;
import java.security.spec.*;
class VerSig
{
if (args.length != 3)
{
System.out.println("Usage: VerSig publickeyfile signaturefile datafile");
}
else try
{
keyfis.close();
sigfis.close();
bufin.close();
} catch (Exception e) {
System.err.println("Caught exception " + e.toString());
};
}}
Output
Diffie–Hellman Algorithm
import java.math.BigInteger;
import java.util.Random;
class DephiHelliman {
p = BigInteger.probablePrime(8, myRandom);
g=new BigInteger("5");
a=new BigInteger("6");
b=new BigInteger("15");
A=g.modPow(a,p);
B=g.modPow(b,p);
k1=B.modPow(a,p);
k2=A.modPow(b,p);
}
Output
C:\Program Files\Java\jdk1.6.0_07\bin\java.exe
-classpath "C:\Program
Files\Java\jdk1.6.0_07\jre\lib\rt.jar;C:\Program
Files\Java\jdk1.6.0_07\lib\tools.jar;D:\DTU\2nd
Sem\NMS\nms_lab" DephiHelliman
Value of p:251
Value of g:5
Value of a:6
Value of g:15
Value of A:63
Value of B:149
Value of k1:149
Value of k2:149
Finished executing