2

バウンシーキャッスルを使用して J2ME で暗号化されたファイルを復号化する際に問題が発生しています。私がやろうとしているのは、暗号化するファイルを選択し、暗号化されたファイルを書き込み、元の形式に復号化して戻すことです (検証のために別のファイルに書き込みます)。

暗号化されたファイルを読み取るときにこのエラーが発生します。

Stack Trace : 
s: pad block corrupted
        at j.a(+219)
        at e.c(+38)
        at e.b(+30)
        at com.aaron.midlets.BluetoothServerMidlet.c(+134)
        at com.aaron.midlets.BluetoothServerMidlet.b(+161)
        at com.aaron.midlets.BluetoothServerMidlet.a(+67)
        at com.aaron.midlets.BluetoothServerMidlet.startApp(+105)
        at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:43)
        at com.sun.midp.midlet.Scheduler.schedule(Scheduler.java:374)
        at com.sun.midp.main.Main.runLocalClass(Main.java:466)
        at com.sun.midp.main.Main.main(Main.java:120)

ここに私のコードの一部があります:

private void createEncryptFile() {
    FileConnection fc = FileListingUtil.getFile("root1/", "test.encrypt");
    try {
        fc.create();
        readAndEncrypt();
    } catch (Exception e) {
    }
}

private void readAndEncrypt() {
    FileConnection fc = FileListingUtil.getFile("root1/", "test.original");
    FileConnection fc2 = FileListingUtil.getFile("root1/", "test.encrypt");

    try {
        InputStream test = fc.openDataInputStream();
        OutputStreamWriter output = new OutputStreamWriter(fc2.openOutputStream());

        int fileSize = (int) fc.fileSize();
        byte[] imgData = new byte[fileSize];

        int bytesRead = 0;
        while (bytesRead < fileSize) {
            bytesRead += test.read(imgData, bytesRead, fileSize - bytesRead);
        }

        EncryptorUtil util = new EncryptorUtil("12345678");
        try {
            byte[] dataE = util.encrypt(imgData);
            for (int y = 0; y < dataE.length; ++y) {
                output.write((int) dataE[y]);
            }
        } catch (CryptoException ex) {
            ex.printStackTrace();
        }
        test.close();
        output.close();

        createDecryptFile();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

private void createDecryptFile() {
    FileConnection fc = FileListingUtil.getFile("root1/", "test.decrypt");
    try {
        fc.create();
        readAndDecrypt();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void readAndDecrypt() {
    FileConnection fc = FileListingUtil.getFile("root1/", "test.encrypt");
    FileConnection fc2 = FileListingUtil.getFile("root1/", "test.decrypt");

    try {
        InputStream test = fc.openDataInputStream();
        OutputStreamWriter output = new OutputStreamWriter(fc2.openOutputStream());

        int fileSize = (int) fc.fileSize();
        byte[] imgData = new byte[fileSize];

        int bytesRead = 0;
        while (bytesRead < fileSize) {
            bytesRead += test.read(imgData, bytesRead, fileSize - bytesRead);
        }

        EncryptorUtil util = new EncryptorUtil("12345678");

        try {
            byte[] dataE = util.decrypt(imgData);
            for (int y = 0; y < dataE.length; ++y) {
                output.write((int) dataE[y]);                   
            }
        } catch (CryptoException ex) {
            ex.printStackTrace();
        }
        test.close();
        output.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

最後の関数は例外をスローします。

4

1 に答える 1

6

問題が 1 つあります。ファイルをライターとして書き込みtest.encryptます(各バイトをcharに変換して2倍にします)。InputStreamバイトを読み取る として読み戻します。したがって、暗号化されたデータは破損しています。

于 2009-09-13T00:26:14.447 に答える