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 lab9;
- import java.security.Key;
- import java.security.KeyPair;
- import java.security.KeyPairGenerator;
- import java.security.Security;
- import java.security.SecureRandom;
- import javax.crypto.Cipher;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- import javax.crypto.BadPaddingException;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.ShortBufferException;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import javax.rmi.CORBA.Util;
- /**
- *
- * @author student
- */
- public class Lab9 {
- public static void main(String[] args) throws Exception {
- byte[] keyBytes = new byte[16];
- byte[] keyBytes2 = new byte[16];
- // declare secure PRNG
- IvParameterSpec ob = new IvParameterSpec(keyBytes2);
- SecureRandom myPRNG = new SecureRandom();
- // seed the key
- myPRNG.nextBytes(keyBytes);
- // build the key from random key bytes
- SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
- // instantiate AES object for ECB with no padding
- Cipher myAES = Cipher.getInstance("AES/ECB/NoPadding");
- // initialize AES objecy to encrypt mode
- myAES.init(Cipher.ENCRYPT_MODE, myKey,ob);
- // initialize plaintext
- byte[] plaintext = new byte[16];
- //initialize ciphertext
- byte[] ciphertext = new byte[16];
- // update cipher with the plaintext
- int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext, 0);
- // process remaining blocks of plaintext
- cLength += myAES.doFinal(ciphertext, cLength);
- // print plaintext and ciphertext
- System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
- System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
- // initialize AES for decryption
- myAES.init(Cipher.DECRYPT_MODE, myKey,ob);
- // initialize a new array of bytes to place the decryption
- byte[] dec_plaintext = new byte[16];
- cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext, 0);
- // process remaining blocks of ciphertext
- cLength += myAES.doFinal(dec_plaintext, cLength);
- // print the new plaintext (hopefully identical to the initial one)
- System.out.println("decrypted: " + javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
- // get a Cipher instance for RSA with PKCS1 padding
- Cipher myRSA = Cipher.getInstance("RSA/ECB/PKCS1Padding");
- // get an instance for the Key Generator
- KeyPairGenerator myRSAKeyGen = KeyPairGenerator.getInstance("RSA");
- // generate an 1024 bit key
- myRSAKeyGen.initialize(1024, myPRNG);
- KeyPair myRSAKeyPair= myRSAKeyGen.generateKeyPair();
- // store the public and private key individually
- Key pbKey = myRSAKeyPair.getPublic();
- Key pvKey = myRSAKeyPair.getPrivate();
- // init cipher for encryption
- myRSA.init(Cipher.ENCRYPT_MODE, pbKey, myPRNG);
- // encrypt, as expected we encrypt a symmetric key with RSA rather than a file or some longer stream which should be encrypted with AES
- ciphertext = myRSA.doFinal(keyBytes);
- // init cipher for decryption
- myRSA.init(Cipher.DECRYPT_MODE, pvKey);
- // decrypt
- plaintext = myRSA.doFinal(ciphertext);
- System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
- System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
- System.out.println("keybytes: " + javax.xml.bind.DatatypeConverter.printHexBinary(keyBytes));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement