0% found this document useful (0 votes)
22 views5 pages

Network Security

The document describes programs implementing symmetric and asymmetric cryptographic algorithms. The first program implements the Caesar cipher symmetric encryption algorithm. The second program implements the RSA asymmetric encryption algorithm to encrypt and decrypt a message. The third program generates a digital signature for a given text string using the DSA algorithm.

Uploaded by

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

Network Security

The document describes programs implementing symmetric and asymmetric cryptographic algorithms. The first program implements the Caesar cipher symmetric encryption algorithm. The second program implements the RSA asymmetric encryption algorithm to encrypt and decrypt a message. The third program generates a digital signature for a given text string using the DSA algorithm.

Uploaded by

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

EX N0-01 IMPLEMENT SYMMETRIC KEY ALGORITHM USING CAESAR CIPHER.

PROGRAM:
class caesarCipher
{
public static String encode(String enc, int offset)
{
offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder();
for (char i : enc.toCharArray())
{
if (Character.isLetter(i))
{
if (Character.isUpperCase(i))
{
encoded.append((char) ('A' + (i - 'A' + offset) % 26));

}
else
{
encoded.append((char) ('a' + (i - 'a' + offset) % 26));
}
}
else
{
encoded.append(i);
}
}
return encoded.toString();
}
public static String decode(String enc, int offset)
{
return encode(enc, 26 - offset);
}
public static void main(String[] args) throws java.lang.Exception
{
String msg = "Anna University";
System.out.println("Simulating Caesar Cipher\n------------------------");
System.out.println("Input : " + msg);
System.out.printf("Encrypted Message : ");
System.out.println(caesarCipher.encode(msg, 3));
System.out.printf("Decrypted Message : ");
System.out.println(caesarCipher.decode(caesarCipher.encode(msg, 3), 3));
}
}

OUTPUT:
Simulating Caesar Cipher
------------------------
Input : Anna University
Encrypted Message : Dqqd Xqlyhuvlwb
Decrypted Message : Anna University
EX NO-02 IMPLEMENT AYMMETRIC KEY ALGORITHMS AND KEY EXCHANGE ALGORITHMS.

PROGRAM:
import java.math.*;
import java.util.*;
class RSA
{
public static void main(String args[])
{
int p, q, n, z, d = 0, e, i;
int msg = 12;
double c;
BigInteger msgback;
p = 3;
q = 11;
n = p * q;
z = (p - 1) * (q - 1);
System.out.println("The value of z = " + z);
for (e = 2; e < z; e++)
{
if (gcd(e, z) == 1)
{
break;
}
}
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);
System.out.println("Decrypted message is : "+ msgback);
}
static int gcd(int e, int z)
{
if (e == 0)
return z;
else
return gcd(z % e, e);
}
}

OUTPUT:
The value of z = 20
The value of e = 3
The value of d = 7
Encrypted message is : 12.0
Decrypted message is : 12
EX NO-03 IMPLEMENT DIGITAL SIGNATURE SCHEMES
PROGRAM:
import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.PrivateKey;

import java.security.Signature;

import java.util.Scanner;

public class CreatingDigitalSignature

public static void main(String args[])throws Exception

Scanner sc=new Scanner(System.in);

System.out.println("Enter some text:");

String msg=sc.nextLine();

KeyPairGenerator KeyPairGen=KeyPairGenerator.getInstance("DSA");

KeyPairGen.initialize(2048);

KeyPair pair=KeyPairGen.generateKeyPair();

PrivateKey privKey=pair.getPrivate();

Signature sign=Signature.getInstance("SHA256withDSA");

sign.initSign(privKey);

byte[] bytes="msg".getBytes();

sign.update(bytes);

byte[] signature=sign.sign();

System.out.println("Digital Signature for given text:"+new String(signature,"UTF8"));

OUTPUT:
Enter some text:
Hii

Digital Signature for given text: 0<0$:h]�)ҵ�!�S�����@!


(<�N*&�l�fu�#�ٍk6�P���9jܺ"

You might also like