answersLogoWhite

0


Best Answer

This is a perfectly running code.....

package javaapplication1;

import java.security.*;

import java.io.*;

import javax.crypto.Cipher;

public class NewClass {

public static void main(String args[])

{

String srci="";

try{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter any string you want to encrypt");

srci=br.readLine();

}

catch(IOException ioe)

{

System.out.println(ioe.getMessage());

}

try

{

KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");

kpg.initialize(512);//initialize key pairs to 512 bits ,you can also take 1024 or 2048 bits

KeyPair kp=kpg.genKeyPair();

PublicKey publi=kp.getPublic();

Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.ENCRYPT_MODE, publi);

byte[]src=srci.getBytes();//converting source data into byte array

byte[] cipherData = cipher.doFinal(src);//use this method to finally encrypt data

String srco=new String(cipherData);//converting byte array into string

System.out.println();

System.out.println("Encrypted data is:-"+srco);

PrivateKey privatei=kp.getPrivate();//Generating private key

Cipher cipheri=Cipher.getInstance("RSA");//Intializing 2nd instance of Cipher class

cipheri.init(Cipher.DECRYPT_MODE, privatei);//Setting to decrypt_mode

byte[] cipherDat = cipheri.doFinal(cipherData);//Finally decrypting data

String decryptdata=new String(cipherDat);

System.out.println("Decrypted data:-"+decryptdata);

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

}

Incase of errors mail me on the below given address....

fs.fabianski@gmail.com

-Fabianski Benjamin

India

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you implement RSA algorithm in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you provide a solution to the diamond-square algorithm using Java and recursion?

Yes. It is possible to provide a solution to the diamond-square algorithm using Java and recursion.


Write a program in C language to implement the apriori algorithm?

JavaScript is one program that has been written in C to implement the Apriori algorithm. There are also several other known programs available on the Internet that implement it as well.


How do you RSA algorithm c?

Perform encryption on the following PT using RSA and find the CT p = 3; q = 11; M = 5


What is fullform of RSA token?

There are number of encryption techniques one such technique is RSA. RSA stands for rivest shamir algorithm.


What are the difference between DES and RSA algorithm?

DES is a symmetric cryptographic algorithm, while RSA is an asymmetric (or public key) cryptographic algorithm. Encryption and decryption is done with a single key in DES, while you use separate keys (public and private keys) in RSA. DES uses 56-bit keys for encryption while RSA uses 2600-bits of KEY


What is the best public-key cryptography algorithm and why?

RSA (Rivest, Shamir, and Adelman) is the best public key algorithm.


What is Difference between DES and RSA algorithm?

1. RSA comes under Asymmetric and DES comes under Symmetric 2. RSA is more secure than DES.


How to write a java program that determines the number of prime numbers less than N which is given by the user?

where to start? do you have an algorithm and just want to implement it in java? depends on how big N is, as that will determine which method is most efficient


Which algorithm was the first to be found suitable for both digital signing and encryption?

RSA


What is the difference between AES and RSA?

AES is a symmetric cryptographic algorithm, while RSA is an asymmetric (or public key) cryptographic algorithm. Encryption and decryption is done with a single key in AES, while you use separate keys (public and private keys) in RSA. The strength of a 128-bit AES key is roughly equivalent to 2600-bits RSA key.


How can you implement election algorithm in java?

To implement an election algorithm in Java, you can start by creating a class that represents the process or node participating in the election. This class should have fields to store the node's ID and status (e.g., active or inactive). The algorithm itself can be implemented using methods within this class, such as sending and receiving election messages, comparing IDs to determine the leader, and updating the status accordingly. Additionally, you can create another class to simulate the network and manage the communication between the nodes.


Which encryption algorithm is commonly used as a public key cipher?

Type your answer here... RSA