Task 1a
import [Link].*;
class T1{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter Plain Text:");
String txt=[Link]();
[Link]("Enter key:");
int key=[Link]();
txt=encrypt(txt,key);
[Link]("Encrypted Text: "+txt);
txt=decrypt(txt,key);
[Link]("Decrypted Text: "+txt);
}
static String encrypt(String txt,int key){
String etxt="";
for(int i=0;i<[Link]();i++){
char j=[Link](i);
if([Link](j)){
etxt+=(char)(((j-'A')+key)%26 +'A');
}
else etxt+=(char)(((j-'a')+key)%26 +'a');
}
return etxt;
}
static String decrypt(String txt,int key){
String etxt="";
for(int i=0;i<[Link]();i++){
char j=[Link](i);
if([Link](j)){
etxt+=(char)(((j-'A')-key+26)%26 +'A');
}
else etxt+=(char)(((j-'a')-key+26)%26 +'a');
}
return etxt;
}
}
Task 1b
import [Link].*;
class T1b{
static String abc="abcdefghijklmnopqrstuvwxyz";
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter Plain Text:");
String txt=[Link]();
txt=encrypt(txt);
[Link]("Encrypted Text: "+txt);
txt=decrypt(txt);
[Link]("Decrypted Text: "+txt);
}
static String encrypt(String txt){
String etxt="";
for (int i=0;i<[Link]();i++){
int a=[Link]([Link](i));
etxt+=[Link](25-a);
}
return etxt;
}
static String decrypt(String txt){
String etxt="";
for (int i=0;i<[Link]();i++){
int a=[Link]([Link](i));
etxt+=[Link](25-a);
}
return etxt;
}
}
Task 2,3,4,5
import [Link].*;
public class T2 {
private static Cipher encryptCipher;
private static Cipher decryptCipher;
public static void main(String[] args) throws Exception{
KeyGenerator keygenerator = [Link]("RC4");
SecretKey secretKey = [Link]();
encryptCipher = [Link]("RC4");
[Link](Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = encryptData("Classified Information!");
decryptCipher = [Link]("RC4");
[Link](Cipher.DECRYPT_MODE, secretKey);
decryptData(encryptedData);
}
private static byte[] encryptData(String data) throws Exception {
[Link]("Data Before Encryption :" + data);
byte[] dataToEncrypt = [Link]();
byte[] encryptedData = [Link](dataToEncrypt);
[Link]("Encryted Data: " + encryptedData);
return encryptedData;
}
private static void decryptData(byte[] data) throws Exception {
byte[] textDecrypted = [Link](data);
[Link]("Decryted Data: " + new String(textDecrypted));
}
}
Task 7
import [Link].*;
import [Link];
import [Link];
public class T7 {
private BigInteger p;
private BigInteger q;
private BigInteger N;
private BigInteger phi;
private BigInteger e;
private BigInteger d;
private int bitlength = 1024;
private Random r;
public T7() {
r = new Random();
p = [Link](bitlength, r);
q = [Link](bitlength, r);
N = [Link](q);
phi = [Link]([Link]).multiply([Link]([Link]));
e = [Link](bitlength / 2, r);
while ([Link](e).compareTo([Link]) > 0 && [Link](phi) < 0) {
e = [Link]([Link]);
}
d = [Link](phi);
}
public static void main(String[] args) throws IOException {
T7 rsa = new T7();
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
String testString;
[Link]("Enter the plain text:");
testString = [Link]();
byte[] encrypted = [Link]([Link]());
[Link]("Encrypted String in Bytes: " + encrypted);
byte[] decrypted = [Link](encrypted);
[Link]("Decrypted String: " + new String(decrypted));
}
public byte[] encrypt(byte[] message) {
return (new BigInteger(message)).modPow(e, N).toByteArray();
}
public byte[] decrypt(byte[] message) {
return (new BigInteger(message)).modPow(d, N).toByteArray();
}
Task 8
import [Link].*;
import [Link];
public class T8
{
public static void main(String[]args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter prime number:");
BigInteger p=new BigInteger([Link]());
[Link]("Enter primitive root of "+p+":");
BigInteger g=new BigInteger([Link]());
[Link]("Enter value for x less than " +p+":");
BigInteger x=new BigInteger([Link]());
BigInteger R1=[Link](x,p);
[Link]("R1="+R1);
[Link]("Enter value for y less than "+p+":");
BigInteger y=new BigInteger([Link]());
BigInteger R2=[Link](y,p);
[Link]("R2="+R2);
BigInteger k1=[Link](x,p);
[Link]("Key1 calculated :"+k1);
BigInteger k2=[Link](y,p);
[Link]("Key2 calculated :"+k2);
[Link]("deffie hellman secret key Encryption has Taken");
}
}
Task 9,10
import [Link].*;
public class T9 {
public static void main(String[] args) {
try {
MessageDigest md = [Link]("SHA1");
[Link]("Message digest object info:");
[Link]("Algorithm = " + [Link]());
[Link]("ToString = " + [Link]());
String input;
input = "";
[Link]([Link]());
byte[] output = [Link]();
[Link]("\nSHA1(\"" + input + "\") = " + bytesToHex(output));
input = "abc";
[Link]([Link]());
output = [Link]();
[Link]("\nSHA1(\"" + input + "\") = " + bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
[Link]([Link]());
output = [Link]();
[Link]("\nSHA1(\"" + input + "\") = " + bytesToHex(output));
} catch (Exception e) {
[Link]("Exception: " + e);
}
}
public static String bytesToHex(byte[] b) {
char hexDigit[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
StringBuffer buf = new StringBuffer();
for (int j = 0; j < [Link]; j++) {
[Link](hexDigit[(b[j] >> 4) & 0x0f]);
[Link](hexDigit[b[j] & 0x0f]);
}
return [Link]();
}
Task 12
import [Link].*;
import [Link].Base64;
public class T12 {
public static void main(String[] args) throws Exception {
KeyPairGenerator kpg = [Link]("RSA");
[Link](1024);
KeyPair keyPair = [Link]();
byte[] data = "Sample Text".getBytes();
Signature sig = [Link]("MD5WithRSA");
[Link]([Link]());
[Link](data);
byte[] signatureBytes = [Link]();
[Link]("Signature: \n" + [Link]().encodeToString(signatureBytes));
[Link]([Link]());
[Link](data);
[Link]("Signature verification result: " + [Link](signatureBytes));
}
}