Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package labb1säkerhet;
  7.  
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.security.Key;
  14. import java.security.KeyPair;
  15. import java.security.KeyStore;
  16. import java.security.KeyStoreException;
  17. import java.security.NoSuchAlgorithmException;
  18. import java.security.NoSuchProviderException;
  19. import java.security.PrivateKey;
  20. import java.security.PublicKey;
  21. import java.security.UnrecoverableKeyException;
  22. import java.security.cert.Certificate;
  23. import java.security.cert.CertificateException;
  24. import javax.crypto.Cipher;
  25.  
  26. /**
  27.  *
  28.  * @author cong
  29.  */
  30. public class AES {
  31.  
  32.     static byte data[];
  33.  
  34.     static byte[] encKey1;
  35.     static byte[] encIV;
  36.     static byte[] encKey2;
  37.     static byte[] ciphertext;
  38.     static Key privateKey;
  39.     static PublicKey publicKey;
  40.  
  41.     public static void main(String[] args) throws IOException, FileNotFoundException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, NoSuchProviderException, Exception {
  42.         String filePath = "/home/cong/Skola/DT541C HT14 Trådlös kommunikation och säkerhet/DT541C HT14  datasäkerhet/Labb1/Lab _ Java Security Implementation/ciphertext.enc";
  43.  
  44.         // Create file object with the path
  45.         File file = new File(filePath);
  46.         // Assign the data from file to data[]
  47.         data = readBytesFromFile(file);
  48.  
  49.         // Byte arrays
  50.         encKey1 = new byte[128];
  51.         encIV = new byte[128];
  52.         encKey2 = new byte[128];
  53.         ciphertext = new byte[1425];
  54.  
  55.         for (int i = 0; i < data.length; i++) {
  56.             if (i < 128) {
  57.                 encKey1[i] = data[i];
  58.             } else if (i >= 128 && i < 256) {
  59.                 encIV[i - 128] = data[i];
  60.             } else if (i >= 255 && i < 383) {
  61.                 encKey2[i - 255] = data[i];
  62.             } else if (i >= 383 && i < 1808) {
  63.                 ciphertext[i - 383] = data[i];
  64.             }
  65.         }
  66.  
  67.         for (int i = 0; i < encKey1.length; i++) {
  68.             System.out.println(i + "=" + encKey1[i]);
  69.         }
  70.         System.out.println("");
  71.         for (int i = 0; i < encIV.length; i++) {
  72.             System.out.println(i + "=" + encIV[i]);
  73.         }
  74.         System.out.println("");
  75.         for (int i = 0; i < encKey2.length; i++) {
  76.             System.out.println(i + "=" + encKey2[i]);
  77.         }
  78.  
  79.         getKeys();
  80.         decryptKey();
  81.     }
  82.  
  83.     public static byte[] readBytesFromFile(File file) throws IOException {
  84.         InputStream is = new FileInputStream(file);
  85.  
  86.         // Get the size of the file
  87.         long length = file.length();
  88.         // You cannot create an array using a long type.
  89.         // It needs to be an int type.
  90.         // Before converting to an int type, check
  91.         // to ensure that file is not larger than Integer.MAX_VALUE.
  92.         if (length > Integer.MAX_VALUE) {
  93.             throw new IOException("Could not completely read file " + file.getName() + " as it is too long (" + length + " bytes, max supported " + Integer.MAX_VALUE + ")");
  94.         }
  95.  
  96.         // Create the byte array to hold the data
  97.         byte[] bytes = new byte[(int) length];
  98.  
  99.         // Read in the bytes
  100.         int offset = 0;
  101.         int numRead = 0;
  102.         while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
  103.             offset += numRead;
  104.         }
  105.  
  106.         // Ensure all the bytes have been read in
  107.         if (offset < bytes.length) {
  108.             throw new IOException("Could not completely read file " + file.getName());
  109.         }
  110.  
  111.         // Close the input stream and return bytes
  112.         is.close();
  113.         return bytes;
  114.     }
  115.  
  116.     private static void getKeys() throws FileNotFoundException, KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
  117.         String labstorePass = "lab1StorePass";
  118.         String password = "lab1KeyPass";
  119.         String alias = "lab1EncKeys";
  120.         String keystoreFilename = "/home/cong/Skola/DT541C HT14 Trådlös kommunikation och säkerhet/DT541C HT14  datasäkerhet/Labb1/Lab _ Java Security Implementation/lab1Store";
  121.         FileInputStream is = new FileInputStream(keystoreFilename);
  122.  
  123.         KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
  124.         keystore.load(is, labstorePass.toCharArray());
  125.  
  126.         Key key = keystore.getKey(alias, password.toCharArray());
  127.         if (key instanceof PrivateKey) {
  128.             // get certifcate of public key
  129.             Certificate cert = keystore.getCertificate(alias);
  130.  
  131.             // get public key
  132.             publicKey = cert.getPublicKey();
  133.  
  134.             // Return a key pair
  135.             KeyPair keyPair = new KeyPair(publicKey, (PrivateKey) key);
  136.             // Get private Key
  137.             privateKey = keyPair.getPrivate();
  138.  
  139.         }
  140.  
  141.     }
  142.  
  143.     public static void decryptKey() {
  144.         try {
  145.  
  146.             byte key1[] = new byte[128];
  147.             byte key2[] = new byte[128];
  148.             byte keyIV[] = new byte[128];
  149.             Cipher rsaDec = Cipher.getInstance("RSA");
  150.             rsaDec.init(Cipher.DECRYPT_MODE, privateKey);
  151.  
  152.             key1 = rsaDec.doFinal(encKey1);
  153.             keyIV = rsaDec.doFinal(encIV);
  154.             key2 = rsaDec.doFinal(encKey2);
  155.  
  156.             System.out.println("Key1 = " + new String(key1));
  157.             System.out.println("KeyIV = " + new String(keyIV));
  158.             System.out.println("key2 = " + new String(key2));
  159.  
  160.         } catch (Exception e) {
  161.             e.printStackTrace();
  162.         }
  163.  
  164.     }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement