0

私はいくつかの大きなファイルに取り組んでおり、いつ完全なファイルの暗号化/復号化を行う予定ですか。時間がかかりすぎています。ここで、ファイルの最初の 1024 バイトのみを暗号化し、残りのバイトは同じままにします。

これが私のコードです:

      static void encrypt(String inputPath, String outputPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException 
      {
     // Here you read the cleartext.
    FileInputStream fis = new FileInputStream(inputPath);
    // This stream write the encrypted text. This stream will be wrapped by another stream.
    FileOutputStream fos = new FileOutputStream(outputPath);

    // Length is 16 byte
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes
    int b;
    int count = 0;
    byte[] d = new byte[1024];

    while((b = fis.read(d)) != -1) {
        if(count <= 1024){
            count += b;
            cos.write(d, 0, b);
        }else{

            cos.write(d, 0, b);

        }
       // cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
}

static byte[] decrypt(String inputPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(inputPath);

   // FileOutputStream fos = new FileOutputStream(outputPath);
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    CipherInputStream cis = new CipherInputStream(fis, cipher);

    int b;
    byte[] d = new byte[1024];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int count =0;
    while((b = cis.read(d)) != -1) {
        if(count <= 1024){
            count += b;
            bos.write(d, 0, b);
        }else{

            bos.write(d, 0, b);

        }

    }

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

何か提案してください..

4

1 に答える 1

0

ファイルから最初の 1024 バイトを読み込み、暗号化してから書き戻します。最初の 1024 バイトを読み取るために、この C# コードを Java に変換します。

byte[] chunkData =  new byte[];
int chunkSize = 1024;
using(FileStream fsInput = FileStream(PATH TO FileMode.Read) 
fsInput.Read(chunkData, 0, chunkSize);
于 2014-08-25T14:34:27.780 に答える