Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package labb1säkerhet;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.security.Key;
- import java.security.KeyPair;
- import java.security.KeyStore;
- import java.security.KeyStoreException;
- import java.security.NoSuchAlgorithmException;
- import java.security.NoSuchProviderException;
- import java.security.PrivateKey;
- import java.security.PublicKey;
- import java.security.UnrecoverableKeyException;
- import java.security.cert.Certificate;
- import java.security.cert.CertificateException;
- import javax.crypto.Cipher;
- /**
- *
- * @author cong
- */
- public class AES {
- static byte data[];
- static byte[] encKey1;
- static byte[] encIV;
- static byte[] encKey2;
- static byte[] ciphertext;
- static Key privateKey;
- static PublicKey publicKey;
- public static void main(String[] args) throws IOException, FileNotFoundException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, NoSuchProviderException, Exception {
- 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";
- // Create file object with the path
- File file = new File(filePath);
- // Assign the data from file to data[]
- data = readBytesFromFile(file);
- // Byte arrays
- encKey1 = new byte[128];
- encIV = new byte[128];
- encKey2 = new byte[128];
- ciphertext = new byte[1425];
- for (int i = 0; i < data.length; i++) {
- if (i < 128) {
- encKey1[i] = data[i];
- } else if (i >= 128 && i < 256) {
- encIV[i - 128] = data[i];
- } else if (i >= 255 && i < 383) {
- encKey2[i - 255] = data[i];
- } else if (i >= 383 && i < 1808) {
- ciphertext[i - 383] = data[i];
- }
- }
- for (int i = 0; i < encKey1.length; i++) {
- System.out.println(i + "=" + encKey1[i]);
- }
- System.out.println("");
- for (int i = 0; i < encIV.length; i++) {
- System.out.println(i + "=" + encIV[i]);
- }
- System.out.println("");
- for (int i = 0; i < encKey2.length; i++) {
- System.out.println(i + "=" + encKey2[i]);
- }
- getKeys();
- decryptKey();
- }
- public static byte[] readBytesFromFile(File file) throws IOException {
- InputStream is = new FileInputStream(file);
- // Get the size of the file
- long length = file.length();
- // You cannot create an array using a long type.
- // It needs to be an int type.
- // Before converting to an int type, check
- // to ensure that file is not larger than Integer.MAX_VALUE.
- if (length > Integer.MAX_VALUE) {
- throw new IOException("Could not completely read file " + file.getName() + " as it is too long (" + length + " bytes, max supported " + Integer.MAX_VALUE + ")");
- }
- // Create the byte array to hold the data
- byte[] bytes = new byte[(int) length];
- // Read in the bytes
- int offset = 0;
- int numRead = 0;
- while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
- offset += numRead;
- }
- // Ensure all the bytes have been read in
- if (offset < bytes.length) {
- throw new IOException("Could not completely read file " + file.getName());
- }
- // Close the input stream and return bytes
- is.close();
- return bytes;
- }
- private static void getKeys() throws FileNotFoundException, KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
- String labstorePass = "lab1StorePass";
- String password = "lab1KeyPass";
- String alias = "lab1EncKeys";
- String keystoreFilename = "/home/cong/Skola/DT541C HT14 Trådlös kommunikation och säkerhet/DT541C HT14 datasäkerhet/Labb1/Lab _ Java Security Implementation/lab1Store";
- FileInputStream is = new FileInputStream(keystoreFilename);
- KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
- keystore.load(is, labstorePass.toCharArray());
- Key key = keystore.getKey(alias, password.toCharArray());
- if (key instanceof PrivateKey) {
- // get certifcate of public key
- Certificate cert = keystore.getCertificate(alias);
- // get public key
- publicKey = cert.getPublicKey();
- // Return a key pair
- KeyPair keyPair = new KeyPair(publicKey, (PrivateKey) key);
- // Get private Key
- privateKey = keyPair.getPrivate();
- }
- }
- public static void decryptKey() {
- try {
- byte key1[] = new byte[128];
- byte key2[] = new byte[128];
- byte keyIV[] = new byte[128];
- Cipher rsaDec = Cipher.getInstance("RSA");
- rsaDec.init(Cipher.DECRYPT_MODE, privateKey);
- key1 = rsaDec.doFinal(encKey1);
- keyIV = rsaDec.doFinal(encIV);
- key2 = rsaDec.doFinal(encKey2);
- System.out.println("Key1 = " + new String(key1));
- System.out.println("KeyIV = " + new String(keyIV));
- System.out.println("key2 = " + new String(key2));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement