0

ファイルの最後の 64 KB を暗号化/復号化しようとしていますが、ファイル フォルダーの 1 つが暗号化されたファイルにありません。ここでは、zip ファイルに実装しようとしています。現在、zip ファイルには 3 つのフォルダーがあり、結果の zip には 2 つのフォルダーしか表示されていません。フォルダの 1 つがありません。いくつかのバイトが欠落していると思います。これが私のコードです:

static void encryptLast64KB(String inputPath, String outputPath)
throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {


    File myFile = new File(inputPath);
    FileInputStream fis = new FileInputStream(myFile);

    FileOutputStream fos = new FileOutputStream(outputPath);
    BufferedOutputStream bus = new BufferedOutputStream(fos);

    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
            "AES");

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);

    int b = 0;
    byte[] d = new byte[65536];

    int offset = 0;

    byte[] encVal = null;

    while ((b = fis.read(d)) != -1) {

        offset = offset + b;
        Log.d(TAG, "Offset: "+offset);
        Log.d(TAG, "b: "+b);
        if((offset)>=myFile.length())
        {
            Log.d(TAG, "last 64 Kbytes");

            try {
                encVal = cipher.doFinal(d);
                Log.d(TAG, "encVal: "+encVal);
                bus.write(encVal);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        else
        {
            Log.d(TAG, "rest of the bytes");
            bus.write(d);
        }


        bus.flush();
        bus.close();
        fis.close();
    }


}

チェックしてください..

[編集] 最後の 64 KB を復号化するための復号化コードを追加。

 static byte[] decryptLast64KBytes(String inputPath) throws IOException,
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException {

        FileInputStream fis = new FileInputStream(inputPath);

        SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] iv = new byte[] { '3', 'd', '0', 'c', 'd', '7', 'A', '9', '7', 'e', '2', '0', 'b', 'x', 'g', 'y' };
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        try {
            cipher.init(Cipher.DECRYPT_MODE, sks, ivParameterSpec);
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        CipherInputStream cis = new CipherInputStream(fis, cipher);

        int b;
        byte[] d = new byte[1024];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int count =0;

        int offset = 0;
        while((b = fis.read(d)) != -1) {
            offset = offset + b;
            Log.d(TAG, "Offset: "+offset);
            Log.d(TAG, "b: "+b);
            if((offset)>=fis.available())
            {
                Log.d(TAG, "last 64 Kbytes");
                while((b = cis.read(d, offset, offset+b))!=-1)
                {
                    bos.write(d);
                    offset = offset + b;
                }

            }
            else
            {
                Log.d(TAG, "rest of the bytes");
                bos.write(d);
            }

        }

        byte[] completeBytes = bos.toByteArray();
        cis.close();
        return completeBytes;

}
4

2 に答える 2

1

あなたのコードは最後の 64KB を暗号化していません。以前のすべての読み取りの結果、サイズに関係なく、最後のチャンクを暗号化します。最後の 64KB が必要な場合は、その理由はご存じのとおりです。seek() または skip() を適切な値にする必要があります。つまり、ファイルの長さから 64KB を引いた値になります。

于 2013-09-25T08:32:32.557 に答える