postheadericon RSA Encryption Example in Java

RSA.java

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class RSA
{
public KeyPairGenerator keygenerator;
public KeyPair myKey;
Cipher c;
public RSA() throws Exception
{
// Genrate the Key
keygenerator = KeyPairGenerator.getInstance("RSA");
keygenerator.initialize(1024) ;
myKey = keygenerator.generateKeyPair();



// Create the cipher
c = Cipher.getInstance("RSA");


}
public byte[] doEncryption(String s) throws Exception
{

   // Initialize the cipher for encryption
   c.init(Cipher.ENCRYPT_MODE,myKey.getPublic());
 
//sensitive information
   byte[] text = s.getBytes();

// Encrypt the text
   byte[] textEncrypted = c.doFinal(text);

return(textEncrypted);
}
public String doDecryption(byte[] s)throws Exception
{

   // Initialize the same cipher for decryption
c.init(Cipher.DECRYPT_MODE,myKey.getPrivate());

   // Decrypt the text
   byte[] textDecrypted = c.doFinal(s);

return(new String(textDecrypted));
}
}



DoRSA.java

import bsr.*;
public class DoRSA
{  

public static void main(String[] argv) throws Exception
{
  RSA d=new RSA();
byte[] str=d.doEncryption("BipinRupadiya");
System.out.println("Encrypted String : "+str);
System.out.println("Encrypted String : "+d.doDecryption(str));
 
}
}


To execute this code


  • open two command prompt
  • compile bsr package
  • compile DoRSA.java
  • execute DoRSA.java


3 comments:

Akanshya said...

thank you for the valuable information giving on blockchain it is very helpful.

blockchain training in hyderabad
blockchain course in hyderabad
blockchain coaching in hyderabad
blockchain training institute in hyderabad
blockchain institute in hyderabad

Sowmya said...

very informative article post. much thanks again

blockchain training in hyderabad
blockchain course in hyderabad
blockchain coaching in hyderabad
blockchain training institute in hyderabad
blockchain institute in hyderabad

meritstep Technology said...

Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
blockchain online training
best blockchain online training
top blockchain online training

Follow by Email

Blog archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.