Facebook
From Jittery Tern, 1 Year ago, written in Java.
Embed
  1. // Java program to create a
  2. // asymmetric key
  3.  
  4.  
  5. import java.security.KeyPair;
  6. import java.security.KeyPairGenerator;
  7. import java.security.PrivateKey;
  8. import java.security.PublicKey;
  9. import java.security.SecureRandom;
  10. import java.util.Scanner;
  11.  
  12. import javax.crypto.Cipher;
  13. // Class to create an asymmetric key
  14.  
  15. public class Asymmetric {
  16.      private static final String RSA = "RSA";//algoritmo rsa
  17.  
  18.     // Generating public and private key
  19.     // using RSA algorithm.
  20.  
  21.     public static KeyPair generateRSAKkeyPair()throws Exception{
  22.         SecureRandom secureRandom = new SecureRandom(); //random  ma più sicuro
  23.         KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA);//crea una chiave da usare per l'rsa
  24.        
  25.         keyPairGenerator.initialize(2048, secureRandom);//keysize
  26.         return keyPairGenerator.generateKeyPair();//crea la coppia d chiavi
  27.  
  28.     }
  29.    
  30.     // Encryption function which converts
  31.     // the plainText into a cipherText
  32.     // using private Key.
  33.     public static byte[] do_RSAEncryption(String plainText,PrivateKey privateKey) throws Exception
  34.     {
  35.         Cipher cipher = Cipher.getInstance(RSA);
  36.  
  37.         cipher.init(Cipher.ENCRYPT_MODE, privateKey);
  38.  
  39.         return cipher.doFinal(plainText.getBytes());
  40.     }
  41.  
  42.     // Decryption function which converts
  43.     // the ciphertext back to the
  44.     // original plaintext.
  45.     public static String do_RSADecryption(byte[] cipherText,PublicKey publicKey)throws Exception{
  46.         Cipher cipher = Cipher.getInstance(RSA);
  47.         cipher.init(Cipher.DECRYPT_MODE,publicKey);
  48.         byte[] result = cipher.doFinal(cipherText);
  49.         return new String(result);
  50.     }
  51.  
  52.     // Driver code
  53.  
  54.     public static void main(String args[]) throws Exception {
  55.         KeyPair keypair = generateRSAKkeyPair();
  56.         //RSA private keys are encoded in PKCS#8 format, and public keys are encoded in X.509 format
  57.         //DatatypeConverter.printHexBinary
  58.         //chiavi
  59.         System.out.println("Public Key is: " + keypair.getPublic().getEncoded());
  60.  
  61.         System.out.println("Private Key is: " + keypair.getPrivate().getEncoded());
  62.  
  63.         String plainText = "This is the PlainText I want to Encrypt using RSA.";
  64.        
  65.         byte[] cipherText= do_RSAEncryption(plainText,keypair.getPrivate());
  66.        
  67.         System.out.print("The Encrypted Text is: ");
  68.  
  69.         System.out.println(cipherText);
  70.  
  71.         String decryptedText = do_RSADecryption(cipherText,keypair.getPublic());
  72.  
  73.         System.out.println("The decrypted text is: "+ decryptedText);
  74.     }
  75. }
  76.        
  77.        
  78.