Skip to content

Commit

Permalink
Add vi param
Browse files Browse the repository at this point in the history
  • Loading branch information
cloverdolphin committed May 18, 2020
1 parent 1aecd9b commit 20877b2
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/penguin/aes/ImageEncryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
Expand All @@ -12,11 +13,13 @@
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class ImageEncryption {
private final static String imageName = "Image-Assignment3.bmp";
private final static String keyStr = "770A8A65DA156D24EE2A093277530142";
private final static String viStr = "1010101010101010";
private final static int HEADER_LENGTH = 138; // examine header length by GHex on Ubuntu
private static byte[] header; // store the header of original image
private static byte[] originContent; // store the content of image
Expand Down Expand Up @@ -46,7 +49,11 @@ public static void readImage(String imgName) {
/*
* Encrypt .bmp image to .jpg image with AES
*/
public static byte [] encrypt(byte[] inputByte, String transformation) throws IllegalBlockSizeException, BadPaddingException{
public static byte [] encrypt(byte[] srcByte, String transformation) throws IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException{
if(keyStr == null) {
System.out.println("***** The key is null");
return null;
}
Cipher cipher = null;
try {
// create instance of cipher
Expand All @@ -55,8 +62,13 @@ public static void readImage(String imgName) {
Key key = new SecretKeySpec(keyStr.getBytes(), "AES");
// update to unlimited
System.out.println( "***** max allowed key length is: " + Cipher.getMaxAllowedKeyLength("AES"));
// initialize the ciper and set mode
cipher.init(Cipher.ENCRYPT_MODE, key);
IvParameterSpec iv = new IvParameterSpec(viStr.getBytes());
if(transformation.equals("AES")) {
// initialize the ciper and set mode
cipher.init(Cipher.ENCRYPT_MODE, key);
}else {
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
}

}catch(NoSuchAlgorithmException e) {
e.printStackTrace();
Expand All @@ -65,7 +77,7 @@ public static void readImage(String imgName) {
}catch(InvalidKeyException e) {
e.printStackTrace();
}
return cipher.doFinal(inputByte);
return cipher.doFinal(srcByte);
}

/*
Expand Down

0 comments on commit 20877b2

Please sign in to comment.