0

我正在写两个程序;一个加密第一个参数中给出的文件,第二个参数中给出一个 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);
    }
 }
}

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

4

0 回答 0