用Java加密JPG / Zip文件

时间:2013-10-06 00:38:04

标签: java encryption jpeg aes

我正在写两个节目;一个加密第一个参数中给出的文件,第二个参数中给出的ASCII种子,另一个解密加密文件。该算法在CBC流密码模式下使用AES-128。将文件读入缓冲区,生成SHA-1哈希并附加到明文,然后加密。种子也由SHA-1生产。解密(使用相同种子)的程序将消息与摘要分开,并将摘要与本地生成的散列进行比较。 到目前为止,这两个程序完全适用于.txt文件,但是如果我尝试加密/解密.jpg或.png文件,原始图像的剩余部分只是顶部的一小部分 - 其余部分只是随机颜色。 .zip文件损坏了。

我的代码很大,我很抱歉。

加密:

public class secureFile 
{
private static SecretKeySpec sec_key_spec;
private static Cipher sec_cipher;
private static final int SIZE_LIMIT = 1024;
private static final int SHA1_SIZE = 20;


// encryption function
public static byte[] encrypt(byte[] plaintext) throws Exception
{
    byte[] encrypted = null;
    byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    IvParameterSpec ivspec = new IvParameterSpec(iv);
    try {
        //set cipher object to encrypt mode
        sec_cipher.init(Cipher.ENCRYPT_MODE, sec_key_spec, ivspec);

        //create ciphertext
        encrypted = sec_cipher.doFinal(plaintext);
    }
    catch(Exception e) {
        System.out.println(e);
    }
    return encrypted;
}


//creates SHA-1 message digest
public static byte[] createDigest(byte[] message) throws Exception
{
    byte[] hash = null;
    try{
        //create message digest object
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");

        //make message digest
        hash = sha1.digest(message);    
    }
    catch(NoSuchAlgorithmException nsae) {
        System.out.println(nsae);
    }
    return hash;
}


public static void main (String args[]) throws Exception 
{   
    byte[] key;
    byte[] key128;
    FileInputStream mFile;
    FileOutputStream cFile;
    byte[] ciphertext;
    byte[] plaintext;
    byte[] sha1_hash;   
    byte[] message;
    String cFilename;
    int file_size;

    try {
        //gets filenames and fileinput
        String[] mFilename = args[0].split("\\.(?=[^\\.]+$)");
        cFilename = mFilename[0] + "EN." + mFilename[1];
        mFile = new FileInputStream(mFilename[0] + "." + mFilename[1]);

        //limits buffer size to 1024
        file_size = mFile.available();
        if (file_size > SIZE_LIMIT) {
            file_size = SIZE_LIMIT;
        }

        //reads message file in buffer
        message = new byte[file_size];
        mFile.read(message);
        mFile.close();

        //creates sha1 digest from message
        sha1_hash = createDigest(message);

        //combines message and digest
        plaintext = new byte[file_size + SHA1_SIZE];
        for (int i = 0; i < file_size; i++) {
            plaintext[i] = message[i];
        }
        for (int j = 0; j < SHA1_SIZE; j++) {
            plaintext[file_size + j] = sha1_hash[j];
        }

        //generates key
        key = createDigest(args[1].getBytes("us-ascii"));
        key128 = Arrays.copyOfRange(key, 0, 16);
        sec_key_spec = new SecretKeySpec(key128, "AES");

        //enciphers the plaintext
        sec_cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        ciphertext = encrypt(plaintext);

        //copies ciphertext to file
        cFile = new FileOutputStream(cFilename);
        cFile.write(ciphertext);
        cFile.close();
    }
    catch (Exception e) {
        System.out.println(e);
    }
  }

}

解密文件:

import java.io.*;
import java.util.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class decryptFile
{
private static SecretKeySpec sec_key_spec = null;
private static Cipher sec_cipher = null;
private static final int SHA1_SIZE = 20;


//decryption function
public static byte[] decrypt(byte[] ciphertext) throws Exception{
    byte[] decrypted = null;
    byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    IvParameterSpec ivspec = new IvParameterSpec(iv);
    try{
        //set cipher to decrypt mode
        sec_cipher.init(Cipher.DECRYPT_MODE, sec_key_spec, ivspec);

        //do decryption
        decrypted = sec_cipher.doFinal(ciphertext);
    }
    catch (BadPaddingException b)
    {
        sec_cipher.init(Cipher.ENCRYPT_MODE, sec_key_spec, ivspec);
        decrypted = sec_cipher.doFinal(ciphertext);
    }
    catch(Exception e){
        System.out.println(e);
    }

    return decrypted;
}


//creates digest
public static byte[] createDigest(byte[] message) throws Exception
{
    byte[] hash = null;
    try{
        //create message digest object
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");

        //make message digest
        hash = sha1.digest(message);    
    }
    catch(NoSuchAlgorithmException nsae) {
        System.out.println(nsae);
    }
    return hash;
}


//compares two digests
public static String compareDigests(byte[] digest, byte[] local_digest)
{
    for (int i = 0; i < SHA1_SIZE; i++)
    {
        if (digest[i] != local_digest[i]) {
            return "Digests don't match; your file may have been tampered with.";
        }
    }
    return "Digests match!";
}


public static void main (String args[]) 
{
    FileInputStream cFile;
    FileOutputStream mFile;
    byte[] key;
    byte[] key128;
    byte[] ciphertext;
    byte[] decrypted;
    byte[] message;
    byte[] digest;
    byte[] local_digest;
    byte[] local_digest2;
    String mFilename;
    int file_size;

    try {
        //obtains filenames
        String[] cFilename = args[0].split("\\.(?=[^\\.]+$)");
        mFilename = cFilename[0] + "DE." + cFilename[1];
        cFile = new FileInputStream(cFilename[0] + "." + cFilename[1]);

        //reads ciphertext file
        cFile = new FileInputStream(cFilename[0] + "." + cFilename[1]);
        ciphertext = new byte[cFile.available()];
        cFile.read(ciphertext);
        cFile.close();

        //generates key
        key = createDigest(args[1].getBytes("us-ascii"));
        key128 = Arrays.copyOfRange(key, 0, 16);
        sec_key_spec = new SecretKeySpec(key128, "AES");

        //deciphers the plaintext
        sec_cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        decrypted = decrypt(ciphertext);

        //splits plaintext between message and digest
        file_size = decrypted.length;
        message = Arrays.copyOfRange(decrypted, 0, (file_size - SHA1_SIZE));
        digest = Arrays.copyOfRange(decrypted, (file_size - SHA1_SIZE), file_size);

        //compares digests
        local_digest = createDigest(message);
        System.out.println(compareDigests(digest, local_digest));

        //copies message to file
        mFile = new FileOutputStream(mFilename);
        mFile.write(message);
        mFile.close();
    }
    catch (Exception e) {
        System.out.println(e);
    }
 }
}

我非常感谢你能给我的任何帮助。谢谢!

0 个答案:

没有答案