无法查看加密图像 - java

时间:2018-05-22 22:43:25

标签: java cryptography

我正在努力使此代码能够使用AES算法及其模式加密图像,然后查看加密图像。

因此标头的字节未加密且内容为。将图像写入新文件时,首先使用标题,然后使用加密内容,以便我们可以将其作为图像文件打开。

但是我无法打开它。任何帮助,将不胜感激。我是新手编码,我去学习:) 感谢

enter code here

import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.UnsupportedEncodingException;  
import java.security.InvalidAlgorithmParameterException;  
import java.security.InvalidKeyException;  
import java.security.MessageDigest;  
import java.security.NoSuchAlgorithmException;  
import java.util.Arrays;  

import javax.crypto.*;  
import javax.crypto.spec.IvParameterSpec;  
import javax.crypto.spec.SecretKeySpec;  


public class test {
    private static SecretKeySpec secretKey ;  
    private static byte[] key ;  
    static String encodekey = "770A8A65DA156D24EE2A093277530142";  
    static String IV = "AAAAAAAAAAAAAAAA";  

public static void main(String[] args) {

    encrypt(new File("/Users/uni/IdeaProjects/ExtendedExperimentalAPI”/src/image.jpeg"));
}

public static void encrypt(File file){
    setKey(encodekey);
    String fileName = file.getName();
    byte[] fileBytes = fileToBytes(file);
    //save the header from file so it is accessible as image after encryption
    byte[] header = new byte[55];
    for(int i = 0; i<55; i++){
        header[i] = fileBytes[i];
    }
    byte[] toEncrypt = Arrays.copyOfRange(fileBytes, 55, fileBytes.length);


    Cipher cipher;
    try {
        //block to encrypt using ECB and save file
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] cipherDataECB = cipher.doFinal(toEncrypt);
        FileOutputStream fos = new FileOutputStream("ECB" + fileName, true);//append to file
        fos.write(header);
        fos.write(cipherDataECB);
        fos.close();

        //block to encrypt using CBC and save file
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey,new IvParameterSpec(IV.getBytes("UTF-8")));
        byte[] cipherDataCBC = cipher.doFinal(toEncrypt);
        fos = new FileOutputStream("CBC" + fileName, true);
        fos.write(header);
        fos.write(cipherDataCBC);
        fos.close();

        //block to encrypt using CFB and save file
        cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey,new IvParameterSpec(IV.getBytes("UTF-8")));
        byte[] cipherDataCFB = cipher.doFinal(toEncrypt);
        fos = new FileOutputStream("CFB" + fileName, true);//append to file
        fos.write(header);
        fos.write(cipherDataCFB);
        fos.close();

    } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | FileNotFoundException e) {
        System.out.println("Error while encrypting file");
    } catch (IOException e) {
        System.out.println("Error writing to the file");
    }

}

static void setKey(String myKey){


    MessageDigest sha = null;
    try {
        key = myKey.getBytes("UTF-8");
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16); // use only first 128 bit
        secretKey = new SecretKeySpec(key, "AES");


    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        System.out.println("Error setting the key for encryption");
    }


}

static byte[] fileToBytes(File file){

    FileInputStream fileInputStream=null;

    byte[] bFile = new byte[(int) file.length()];
    //convert file into array of bytes


    try{
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();
    } catch(FileNotFoundException e){
        System.out.println("Specified file not found");
    } catch (IOException e) {
        System.out.println("Error reading from the file");
    }
    return bFile;
}
}

0 个答案:

没有答案