Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. /***************************************************************
  2.  *
  3.  * A Simple Program on Asymmetric Encryption
  4.  *
  5.  * Written by Samson Yeow
  6.  *
  7.  * Singapore Polytechnic
  8.  *
  9.  * (c) Mar 2007. Singapore Polytechnic
  10.  *
  11.  * Program Description:
  12.  * ====================
  13.  * Create a cipher using the generated public key to initialize it.
  14.  * Then we use the Electronic Code Book (ECB) mode and PKCS1Padding.
  15.  * ECB is suitable for encrypting small blocks of random data.
  16.  * For example, like a key.
  17.  *
  18.  * PKCS1Padding is standard for most implementations of RSA
  19.  *
  20.  * Another is to use OAEP (Optimal Asymmetric Encryption Padding)
  21.  * OAEP is an improvement on PKCS#1.
  22.  *
  23.  ***************************************************************/
  24.  
  25.  import javax.crypto.*;
  26.  import javax.crypto.spec.*;
  27.  import java.security.*;
  28.  import sun.misc.*;
  29.  import java.io.*;
  30.  
  31.  public class myRsaStud
  32.  {
  33.    public static void main (String[] args) throws Exception
  34.    {
  35.     // Print Header
  36.     System.out.println("A Simple Program using RSA to encrypt a single symmetric key using");
  37.     System.out.println("Advanced Encryption Standard (AES).\n");
  38.  
  39.     // Create an AES key to be encrypted
  40.     System.out.println("Generating a symmetric (AES) key...");
  41.     KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
  42.     keyGenerator.init(128);
  43.     Key AESKey = keyGenerator.generateKey();
  44.     System.out.println("Format: "+AESKey.getFormat());
  45.  
  46.     // Create an RSA key pair
  47.     System.out.println("Generating an RSA key...");
  48.     KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
  49.     keyPairGenerator.initialize(1024);
  50.     // Insert your statement here
  51.     KeyPair kp = keyPairGenerator.genKeyPair();
  52.    
  53.     String publicKeyFilename = "public";
  54.  
  55.     byte[] publicKeyBytes = kp.getPublic().getEncoded();
  56.  
  57.     FileOutputStream fos = new FileOutputStream(publicKeyFilename);
  58.     fos.write(publicKeyBytes);
  59.     fos.close();
  60.  
  61.     String privateKeyFilename = "privateKeyFilename";
  62.  
  63.     byte[] privateKeyBytes = kp.getPrivate().getEncoded();
  64.  
  65.     //byte[] encryptedPrivateKeyBytes = passwordEncrypt(password.toCharArray(), privateKeyBytes);
  66.  
  67.     fos = new FileOutputStream(privateKeyFilename);
  68.     //fos.write(encryptedPrivateKeyBytes);
  69.     fos.write(privateKeyBytes);
  70.     fos.close();
  71.  
  72.     System.out.println("Done generating the key.\n");
  73.  
  74.     // Initialise the RSA cipher with PUBLIC key
  75.     Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  76.     // Insert your statement here
  77.     cipher.init(Cipher.ENCRYPT_MODE,kp.getPublic());
  78.  
  79.  
  80.     // Get the bytes of the AES key
  81.     byte[] encryptedKey = AESKey.getEncoded();
  82.     System.out.println("AES key:\n" + asHex(encryptedKey) + "\n");
  83.  
  84.     // Perform the actual encryption on those bytes
  85.     byte[] cipherText = cipher.doFinal(encryptedKey);
  86.     System.out.println("Encrypted key:\n" + asHex(cipherText) + "\n");
  87.  
  88.     // Re-initialize the RSA cipher with PRIVATE key
  89.     // Insert your statement here
  90.     cipher.init(Cipher.DECRYPT_MODE, kp.getPrivate());
  91.  
  92.  
  93.     // Perform the decryption
  94.     byte[] decryptedKey = cipher.doFinal(cipherText);
  95.     System.out.println("Decrypted key:\n" + asHex(decryptedKey));
  96.  
  97.     // Create a new key from the decrypted bytes using SecretKeySpec
  98.     SecretKey newAESKey = new SecretKeySpec(decryptedKey, "AES");
  99.     // And your program continues from here
  100.     // ....blah blah
  101.  
  102.     System.out.println("\nEnd of Program");
  103.  
  104.   }
  105.  
  106.   public static String asHex (byte buf[]) {
  107.  
  108.   //Obtain a StringBuffer object
  109.       StringBuffer strbuf = new StringBuffer(buf.length * 2);
  110.       int i;
  111.  
  112.       for (i = 0; i < buf.length; i++) {
  113.           if (((int) buf[i] & 0xff) < 0x10)
  114.              strbuf.append("0");
  115.              strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
  116.        }
  117.        // Return result string in Hexadecimal format
  118.        return strbuf.toString();
  119.   }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement