0

第一次发布这个所以不要对我太苛刻(虽然我已经阅读这个论坛有一段时间了)

我在 Java 中使用 AES 遇到的问题是这样的。

首先,我需要加密一个字符串并将其写入一个文本文件然后压缩它,这没问题。我正在使用 AES 加密,并且我定义了自己的密钥,例如“123”

其次,我需要解压缩文件(或提取它?)并使用我在第一步中使用的相同密钥对其进行解密。

这里发生的是:第一步很好,但第二步解密文件失败,即使字符串结果相同,总字符,单词等

这是编写文件的代码

private static void inputKeFile(String input) throws IOException
{
    FileWriter fstream = new FileWriter("C:/Users/Sactio/Desktop/tyo/txtToZip.txt",false);
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(input);
    //Close the output stream
    out.close();
}

压缩文件

private static void doZip() {
        try {

            String filename ="C:/Users/Sactio/Desktop/tyo/txtToZip.txt";
            String zipfilename="C:/Users/Sactio/Desktop/OutputZipWrite";
            File file = new File(filename);
            FileInputStream fis = new FileInputStream(file);
            long length = file.length();
            byte[] buf = new byte[(int)length];
            fis.read(buf,0,buf.length);

            CRC32 crc = new CRC32();
            ZipOutputStream s = new ZipOutputStream(new FileOutputStream(zipfilename));

            s.setLevel(9);

            ZipEntry entry = new ZipEntry(filename);
            entry.setSize((long)buf.length);
            crc.reset();
            crc.update(buf);
            entry.setCrc( crc.getValue());
            s.putNextEntry(entry);
            s.write(buf, 0, buf.length);
            s.finish();
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这就是加密

import javax.crypto.Cipher;  
import javax.crypto.SecretKey;  
import javax.crypto.spec.SecretKeySpec;  

public class JcaTest {  
private Cipher ecipher;  
private Cipher dcipher;  

JcaTest(SecretKey key) {  
    try {  
        ecipher = Cipher.getInstance("AES");  
        dcipher = Cipher.getInstance("AES");  
        ecipher.init(Cipher.ENCRYPT_MODE, key);  
        dcipher.init(Cipher.DECRYPT_MODE, key);  
    } catch (Exception e) {  
        System.out.println("Failed in initialization");  
    }  
}  

public String encrypt(String str) {  
    try {  
        byte[] utf8 = str.getBytes("UTF-8");  
        byte[] enc = ecipher.doFinal(utf8);  

        return new sun.misc.BASE64Encoder().encode(enc);  
    } catch (Exception e) {  
        System.out.println("Failed in Encryption");  
    }  
    return null;  
}  

public String decrypt(String str) {  
    try {  
        byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);  

        byte[] utf8 = dcipher.doFinal(dec);  

        return new String(utf8, "UTF-8");  
    } catch (Exception e) {  
        System.out.println("Failed in Decryption");  
    }  
    return null;  
}  

最后是 zip 的提取器

private static void bacaZip(String zipfilename) throws IOException
{

    ZipInputStream zinstream = new ZipInputStream(
     new FileInputStream(zipfilename));


    File file = new File(zipfilename);
    FileInputStream fis = new FileInputStream(file);
    long length = file.length();
    byte[] buf = new byte[(int)length];

    ZipEntry zentry = zinstream.getNextEntry();
    System.out.println("Name of current Zip Entry : " + zentry + "\n");
    while (zentry != null) {
      String entryName = zentry.getName();
      System.out.println("Name of  Zip Entry : " + entryName);
      FileOutputStream outstream = new FileOutputStream("C:/Users/Sactio/Desktop/OutputZipWrite.txt");
      int n;

      while ((n = zinstream.read(buf, 0, buf.length)) > -1) {
        outstream.write(buf, 0, n);

      }
      System.out.println("Successfully Extracted File Name : "
          + entryName);
      outstream.close();

      zinstream.closeEntry();
      zentry = zinstream.getNextEntry();

    }
}

private static void extractZip(String jsonString) throws FileNotFoundException
{

    try {
        bacaZip(jsonString);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        System.err.println("Exception: "+e1);
    }
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader("C:/Users/Sactio/Desktop/OutputZipWrite.txt"));
        String text = null;

        // repeat until all lines is read
        while ((text = reader.readLine()) != null) {
            contents.append(text)
                    .append(System.getProperty(
                            "line.separator"));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // show file contents here
    System.out.println("HASIL: "+contents.toString());
}

如果我跳过压缩和文件步骤,AES 工作得很好,但如果我将字符串发送到文件中并压缩它,AES 加密会由于某种原因失败。有人对这个问题有任何想法吗?

4

1 回答 1

1

您解密的文件必须逐字节地与加密过程的输出相同。您说“即使字符串结果相同,总字符、单词等”对我来说表明您将加密文件视为文本“字符”。它不是文本,它是字节。将其视为文本会导致灾难,因为字符可以通过多种不同的方式表示为字节。您需要逐字节检查身份,并始终将密文视为字节,而不是字符。

正如@Thilo 指出的那样,压缩加密数据是没有用的。使用顺序压缩 -> 加密 -> 解密 -> 展开。

于 2012-06-20T12:09:39.627 回答