有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java AES加密,在解密文件中获得额外的垃圾字符

我正在安卓应用程序中制作调试日志功能。 我有一个简单的类,登录到。txt文件使用128位AES加密

日志记录完成后,我用一个简单的JAVA程序解密记录的文件

问题是当我解密加密的日志时,我得到了一些奇怪的内容,我也得到了加密的内容,但是还有一些额外的字符,请参见下文

Android应用程序日志部分:

public class FileLogger {

//file and folder name
public static String LOG_FILE_NAME = "my_log.txt";
public static String LOG_FOLDER_NAME = "my_log_folder";

static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS");

//My secret key, 16 bytes = 128 bit
static byte[] key = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};

//Appends to a log file, using encryption
public static void appendToLog(Context context, Object msg) {

    String msgStr;
    String timestamp = "t:" + formatter.format(new java.util.Date());

    msgStr = msg + "|" + timestamp + "\n";

    File sdcard = Environment.getExternalStorageDirectory();
    File dir = new File(sdcard.getAbsolutePath() + "/" + LOG_FOLDER_NAME);
    if (!dir.exists()) {
        dir.mkdir();
    }

    File encryptedFile = new File(dir, LOG_FILE_NAME);

    try {
        
        //Encryption using my key above defined
        Key secretKey = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        byte[] outputBytes = cipher.doFinal(msgStr.getBytes());

        //Writing to the file using append mode
        FileOutputStream outputStream = new FileOutputStream(encryptedFile, true);
        outputStream.write(outputBytes);
        outputStream.close();
        
        
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

}

}

这是解密程序JAVA程序:

public class Main {

    

//output file name after decryption
private static String decryptedFileName;
//input encrypted file
private static String fileSource;
//a prefix tag for output file name
private static String outputFilePrefix = "decrypted_";
//My key for decryption, its the same as in the encrypter program.
static byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };

//Decrypting function
public static void decrypt(byte[] key, File inputFile, File outputFile) throws Exception {
    try {

        Key secretKey = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        FileInputStream inputStream = new FileInputStream(inputFile);
        byte[] inputBytes = new byte[(int) inputFile.length()];
        inputStream.read(inputBytes);

        byte[] outputBytes = cipher.doFinal(inputBytes);

        FileOutputStream outputStream = new FileOutputStream(outputFile, true);
        outputStream.write(outputBytes);

        inputStream.close();
        outputStream.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

//first argument is the intput file source
public static void main(String[] args) {

    if (args.length != 1) {
        System.out.println("Add log file name as a parameter.");

    } else {
        fileSource = args[0];

        try {
            File sourceFile = new File(fileSource);
            if (sourceFile.exists()) {
                
                //Decrption
                decryptedFileName = outputFilePrefix + sourceFile.getName();
                File decryptedFile = new File(decryptedFileName);
                decrypt(key, sourceFile, decryptedFile);
            } else {
                System.out.println("Log file not found: " + fileSource);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Decryption done, output file: " + decryptedFileName);
    }

}

}

输出解密日志(用记事本++打开):

enter image description here

T这里是有效的内容,但您也可以看到额外的thrash字符如果使用默认的windows文本编辑器打开,也会得到鞭打字符,但不同的字符

这是我第一次尝试加密-解密,我做错了什么? 有什么想法吗


共 (2) 个答案

  1. # 1 楼答案

    AES是一种仅适用于块的分组密码。要加密的明文可以是任意长度,因此密码必须始终填充明文,以将其填充到块大小的倍数(或在已是块大小的倍数时添加完整块)。在这个PKCS#5/PKCS#7填充中,每个填充字节表示填充字节的数量

    简单的解决方法是在解密过程中迭代outputBytes,并删除总是在下一行的填充字节。当您使用多行日志消息或使用语义安全模式(稍后将详细介绍)时,这将立即中断

    更好的解决方法是在消息之前写入每个日志消息的字节数,读取该字节数并仅解密该字节数。这也可能更容易用文件流实现

    您当前使用的Cipher.getInstance("AES");Cipher.getInstance("AES/ECB/PKCS5Padding");的非完全限定版本。ECB模式在语义上不安全。它只是用AES和密钥对每个块(16字节)进行加密。所以相同的块在密文中也是相同的。这尤其糟糕,因为某些日志消息的开头相同,攻击者可能能够区分它们。这也是为什么尽管分块加密,但整个文件的解密仍然有效的原因。你应该使用随机静脉注射的CBC模式

    以下是在CBC模式下正确使用AES的一些示例代码,其中使用了随机IV流:

    private static SecretKey key = generateAESkey();
    private static String cipherString = "AES/CBC/PKCS5Padding";
    
    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream log = new ByteArrayOutputStream();
        appendToLog("Test1", log);
        appendToLog("Test2 is longer", log);
        appendToLog("Test3 is multiple of block size!", log);
        appendToLog("Test4 is shorter.", log);
    
        byte[] encLog = log.toByteArray();
    
        List<String> logs = decryptLog(new ByteArrayInputStream(encLog));
    
        for(String logLine : logs) {
            System.out.println(logLine);
        }
    }
    
    private static SecretKey generateAESkey() {
        try {
            return KeyGenerator.getInstance("AES").generateKey();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    private static byte[] generateIV() {
        SecureRandom random = new SecureRandom();
        byte[] iv = new byte[16];
        random.nextBytes(iv);
        return iv;
    }
    
    public static void appendToLog(String s, OutputStream os) throws Exception {
        Cipher cipher = Cipher.getInstance(cipherString);
        byte[] iv = generateIV();
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
        byte[] data = cipher.doFinal(s.getBytes("UTF-8"));
        os.write(data.length);
        os.write(iv);
        os.write(data);
    }
    
    public static List<String> decryptLog(InputStream is) throws Exception{
        ArrayList<String> logs = new ArrayList<String>();
        while(is.available() > 0) {
            int len = is.read();
            byte[] encLogLine = new byte[len];
            byte[] iv = new byte[16];
            is.read(iv);
            is.read(encLogLine);
    
            Cipher cipher = Cipher.getInstance(cipherString);
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
            byte[] data = cipher.doFinal(encLogLine);
            logs.add(new String(data, "UTF-8"));
        }
        return logs;
    }
    
  2. # 2 楼答案

    您已经使用不同的加密上下文对每个日志消息进行了加密。当您在cipher对象上调用doFinal方法时,明文被填充为16的倍数。实际上,您的日志文件是由许多小的加密消息组成的序列。但是,在解密时,您将忽略这些消息边界,并将文件视为单个加密消息。结果是填充字符没有被正确剥离。您看到的“垃圾”字符很可能是这些填充字节。您需要重新设计日志文件格式,或者保留消息边界,以便解密程序能够发现它们,或者完全消除它们

    另外,不要在Java加密中使用默认值:它们是不可移植的。例如,Cipher.getInstance()采用形式为alg/mode/padding的字符串。始终指定所有三个。我注意到您还使用了默认的no argsString.getBytes()方法。总是指定一个字符集,而且几乎总是“UTF8”是最好的选择