NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import java.math.BigInteger;
import java.io.*;

public class RSA
{
// BigInteger is used to store and manipulate very large numbers
BigInteger p, q, d, e, n, z;
// p and q -> Large Prime Numbers
// e -> Public Key
// d -> Private Key
BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
String msg, rmsg, code;
// msg -> String to Encrypt
//code -> Encrypted String
// rmsg -> Decrypted String
int size;
//Size of the Message
BigInteger m, c;
// m and c -> used to store the ASCII values of individual characters in BigInteger format to encrypt or decrypt

void read()throws IOException
{
System.out.println("Enter the large prime numbers(p and q: Such that p*q > 127):");
p = new BigInteger(keyin.readLine()); // Read p
q = new BigInteger(keyin.readLine()); // Read q
n = p.multiply(q); // n = p*q
z = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); // z = (p-1)(q-1)
System.out.println("Enter the public exponent (e):");
e = new BigInteger(keyin.readLine()); // Read Public Key (e)
d = new BigInteger("0"); // Set Private Key (d) to 0
BigInteger temp;
// do-while loop to generate private key (d) such that (e*d) mod z = 1
do
{
d = d.add(BigInteger.ONE); // d++
temp = (d.multiply(e)).mod(z); // computing (e*d) mod z
}while(!temp.equals(BigInteger.ONE)); // the value of d is accepted if temp==1
System.out.println("Enter Message to Encrypt:");
msg = keyin.readLine();
size = msg.length();
code = "";
rmsg = "";
}
void encrypt()
{
for(int i = 0; i < size; i++)
{
m = BigInteger.valueOf((int)msg.charAt(i)); //Copy each character of msg into m
c = m.modPow(e, n); // Calculate c = (m^e) mod n
code += (char)c.intValue(); // Add encrypted character into code
}
}
void decrypt()
{
for(int i = 0; i < size; i++)
{
c = BigInteger.valueOf((int)code.charAt(i)); //Copy each character of code into c
m = c.modPow(d, n); // Calculate m = (c^d) mod n
rmsg += (char) m.intValue(); // Add decrypted character into message
}
}
void show()
{
System.out.println("nThe Message Entered at Sender's end is "" + msg + """);
System.out.println("The Encrypted Message sent to the Receiver is "" + code + """);
System.out.println("The Decrypted Message at Receiver's end is "" + rmsg + """);
}
public static void main(String args[])throws IOException
{
RSA obj = new RSA();
obj.read();
obj.encrypt();
obj.decrypt();
obj.show();
}
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.