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

css2

This Java program implements the RSA encryption algorithm, generating public and private keys using two prime numbers. It encrypts a message by converting each character to a BigInteger and applying the encryption function, then decrypts it back to the original message. The program demonstrates key generation, encryption, and decryption processes with output for each step.

Uploaded by

aayushgunjal52
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)
7 views3 pages

css2

This Java program implements the RSA encryption algorithm, generating public and private keys using two prime numbers. It encrypts a message by converting each character to a BigInteger and applying the encryption function, then decrypts it back to the original message. The program demonstrates key generation, encryption, and decryption processes with output for each step.

Uploaded by

aayushgunjal52
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

import java.math.

BigInteger;

class RSA {

static BigInteger power(BigInteger base, BigInteger expo, BigInteger m) {

return base.modPow(expo, m);

static BigInteger modInverse(BigInteger e, BigInteger phi) {

return e.modInverse(phi);

static void generateKeys(BigInteger[] keys) {

BigInteger p = new BigInteger("7919");

BigInteger q = new BigInteger("1009");

BigInteger n = p.multiply(q);

BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));

BigInteger e = BigInteger.ZERO;

for (e = new BigInteger("2"); e.compareTo(phi) < 0; e = e.add(BigInteger.ONE)) {

if (e.gcd(phi).equals(BigInteger.ONE)) {

break;

} }

BigInteger d = modInverse(e, phi);

keys[0] = e;

keys[1] = d;

keys[2] = n;

static BigInteger encrypt(BigInteger m, BigInteger e, BigInteger n) {

return power(m, e, n);


}

static BigInteger decrypt(BigInteger c, BigInteger d, BigInteger n) {

return power(c, d, n);

public static void main(String[] args) {

BigInteger[] keys = new BigInteger[3];

generateKeys(keys);

System.out.println("Public Key (e, n): (" + keys[0] + ", " + keys[2] + ")");

System.out.println("Private Key (d, n): (" + keys[1] + ", " + keys[2] + ")");

String message = "HELLO";

System.out.println("Original Message: " + message);

StringBuilder encryptedMessage = new StringBuilder();

for (char c : message.toCharArray()) {

BigInteger M = BigInteger.valueOf(c);

BigInteger C = encrypt(M, keys[0], keys[2]);

encryptedMessage.append(C.toString()).append(" ");

System.out.println("Encrypted Message: " + encryptedMessage.toString().trim());

StringBuilder decryptedMessage = new StringBuilder();

String[] encryptedChunks = encryptedMessage.toString().split(" ");

for (String encryptedChunk : encryptedChunks) {

BigInteger C = new BigInteger(encryptedChunk);


BigInteger decrypted = decrypt(C, keys[1], keys[2]);

decryptedMessage.append((char) decrypted.intValue());

System.out.println("Decrypted Message: " + decryptedMessage.toString());

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy