0% found this document useful (0 votes)
20 views8 pages

CSS 1 and 2

The document contains three experiments related to encryption and decryption techniques in Java. Experiment 01 demonstrates substitution and transposition encryption methods, while Experiment 02 focuses on RSA encryption and decryption using prime numbers. Experiment 02 B showcases the generation and verification of digital signatures using RSA algorithm.

Uploaded by

oopmbcoe
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)
20 views8 pages

CSS 1 and 2

The document contains three experiments related to encryption and decryption techniques in Java. Experiment 01 demonstrates substitution and transposition encryption methods, while Experiment 02 focuses on RSA encryption and decryption using prime numbers. Experiment 02 B showcases the generation and verification of digital signatures using RSA algorithm.

Uploaded by

oopmbcoe
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/ 8

Experiment No:- 01

Source code:-

Import java.util.*;

Class ProductCipher {

Public static void main(String args[]) {

// Input to be encrypted

System.out.println(“Enter the input to be encrypted:”);

String substitutionInput = new Scanner(System.in).nextLine();

// Enter a number

System.out.println(“Enter a number:”);

Int n = new Scanner(System.in).nextInt();

// Substitution encryption

StringBuffer substitutionOutput = new StringBuffer();

For (int I = 0; I < substitutionInput.length(); i++) {

Char c = substitutionInput.charAt(i);

substitutionOutput.append((char) (c + 5));

System.out.println(“\nSubstituted text:”);

System.out.println(substitutionOutput);

// Transposition encryption

String transpositionInput = substitutionOutput.toString();

Int modulus = transpositionInput.length() % n;

If (modulus != 0) {

Modulus = n – modulus; // padding with ‘/’

For (int I = 0; I < modulus; i++) {

transpositionInput += “/”;

}
StringBuffer transpositionOutput = new StringBuffer();

System.out.println(“\nTransposition Matrix:”);

For (int I = 0; I < n; i++) {

For (int j = 0; j < transpositionInput.length() / n; j++) {

Char c = transpositionInput.charAt(I + (j * n));

System.out.print©;

transpositionOutput.append©;

System.out.println();

System.out.println(“\nFinal encrypted text:”);

System.out.println(transpositionOutput);

// Transposition decryption

Int transpositionN = transpositionOutput.length() / n;

StringBuffer transpositionPlaintext = new StringBuffer();

For (int I = 0; I < transpositionN; i++) {

For (int j = 0; j < transpositionOutput.length() / n; j++) {

Char c = transpositionOutput.charAt(I + (j * transpositionN));

transpositionPlaintext.append©;

// Substitution decryption

StringBuffer plaintext = new StringBuffer();

For (int I = 0; I < transpositionPlaintext.length(); i++) {

Char c = transpositionPlaintext.charAt(i);

Plaintext.append((char) (c – 5));

System.out.println(“\nPlaintext:”);

System.out.println(plaintext);

}
Output:-
Experiment No:- 02 A

Source Code:-

Import java.util.*;

Class Exp1 {

Public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

// Initializing d

Int d = 0;

System.out.println(“Enter two prime numbers”);

// Taking two prime numbers as input

Int p = sc.nextInt();

Int q = sc.nextInt();

// Calculate n

Int n = p * q;

System.out.println(“n = “ + n);

// Calculate phi(n)

Int pn = (p – 1) * (q – 1);

// Finding e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1

Int e = 0;

Search:

For (int I = 2; I <= pn; i++) {

Int r;

Int j = I;

Int k = pn;

// Euclidean algorithm to find gcd

While (k != j) {
If (k > j) {

K = k – j;

} else {

J = j – k;

If (k == 1) {

E = I;

Break search;

System.out.println(“e = “ + e);

// Finding d such that (d * e) % phi(n) = 1

Go:

For (int I = 1; I <= pn; i++) {

Int x = (e * i) % pn;

If (x == 1) {

System.out.println(“d = “ + i);

System.out.println(“The private key is (d) “ + i);

D = I;

Break go;

// Public key

System.out.println(“The public key is (n, e) “ + n + “, “ + e);

// Encryption and Decryption process

System.out.println(“Enter plaintext”);

String t = sc.next();

Int m = 0;
// Summing the ASCII values of the characters in the plaintext

For (int I = 0; I < t.length(); i++) {

M += (int) t.charAt(i);

// Encryption: c = (m^e) % n

Int c = (int) (Math.pow(m, e) % n);

System.out.println(“The encrypted message is “ + c);

// Decryption: m = (c^d) % n

M = (int) (Math.pow(c, d) % n);

System.out.println(“The decrypted message is “ + (char) m);

Output:-
Experiment No:- 02 B

Source Code:-

Import java.security.KeyPair;

Import java.security.KeyPairGenerator;

Import java.security.Signature;

Import sun.misc.BASE64Encoder;

Public class MainClass {

Public static void main(String[] args) throws Exception {

// Initialize KeyPairGenerator with RSA algorithm

KeyPairGenerator kpg = KeyPairGenerator.getInstance(“RSA”);

Kpg.initialize(1024);

// Generate key pair

KeyPair keyPair = kpg.genKeyPair();

// Data to be signed

Byte[] data = “test”.getBytes(“UTF8”);

// Initialize Signature instance with MD5WithRSA

Signature sig = Signature.getInstance(“MD5WithRSA”);

// Initialize the signature for signing with private key

Sig.initSign(keyPair.getPrivate());

Sig.update(data);

// Generate the signature

Byte[] signatureBytes = sig.sign();

// Print the signature in BASE64 encoding

System.out.println(“Signature: “ + new BASE64Encoder().encode(signatureBytes));


// Initialize the signature for verification with the public key

Sig.initVerify(keyPair.getPublic());

Sig.update(data);

// Verify the signature

Boolean isVerified = sig.verify(signatureBytes);

// Print verification result

System.out.println(“Signature verified: “ + isVerified);

Output:-

Signature:
ecmcCxZyBjl4ueSJF3mULwVM9YnYHq8137X1DOxlq9KRCIUpdfQjAn75t9Gm72U
h/jzPp/wr0vSSjGINdgACyqrQc0fbAXLCBwibstPO80jEerLjJzaGfly9VZA+dCOZNpCh
ELBDD/EHMJ+RZSf0H3SUEH8/48p5EbQGDNutgAk/c

Signature verified: true

You might also like