admin管理员组

文章数量:1130349

import javax.crypto.Cipher;
import com.googlemon.primitives.Bytes;

public void encrypt18(InputStream input, OutputStream output) throws Exception {
    byte[] algorithm = "Salted__".getBytes("ascii");
    byte[] salt = get_random_bytes(BS - algorithm.length);
    Cipher cipher = getEncryptCipher(this.password, salt);

    output.write(algorithm);
    output.write(salt);
    boolean finished = false;
    ChunkReader reader = new ChunkReader(1024 * BS);
    while (!finished) {
        byte[] chunk = reader.readChunk(input);
        if (chunk.length <= 0 || (chunk.length % BS) != 0) {
            int padding_length = chunk.length <= 0 ? BS : (BS - (chunk.length % BS));
            chunk = Bytes.concat(chunk, padding(padding_legnth));
            finished = true;
        }
        if (chunk.length > 0) {
            output.write(cipher.update(chunk));
        }
    }
}
这段代码是一个用于加密数据流的Java方法。它使用了Java加密架构(JCA)中的`Cipher`类来对数据进行加密。以下是代码的逐行解释:

1. `import javax.crypto.Cipher;`:导入`Cipher`类,这是Java加密架构(JCA)的一部分,用于加密和解密数据。

2. `import com.googlemon.primitives.Bytes;`:导入`Bytes`类,这是一个来自Google Guava库的实用程序类,用于操作字节。

3. `public void encrypt18(InputStream input, OutputStream output) throws Exception {`:定义了一个名为`encrypt18`的方法,它接受一个`InputStream`和一个`OutputStream`作为参数,并可能抛出异常。这个方法用于读取输入流中的数据,加密它,然后将加密后的数据写入输出流。

4. `byte[] algorithm = "Salted__".getBytes("ascii");`:将字符串`"Salted__"`转换为ASCII编码的字节数组。这通常是指定加密算法的字符串,例如`"AES/CBC/PKCS5Padding"`。

5. `byte[] salt = get_random_bytes(BS - algorithm.length);`:调用`get_random_bytes`方法生成一个随机的盐值,长度为`BS - algorithm.length`。`BS`可能是一个定义块大小的常量,例如128位(16字节)。

6. `Cipher cipher = getEncryptCipher(this.password, salt);`:调用`getEncryptCipher`方法创建一个`Cipher`对象,使用提供的密码和盐值初始化它进行加密操作。

7. `output.write(algorithm);`:将加密算法的字节数组写入输出流。

8. `output.write(salt);`:将生成的盐值写入输出流。

9. `boolean finished = false;`:初始化一个布尔变量`finished`,用于控制加密循环。

10. `ChunkReader reader = new ChunkReader(1024 * BS);`:创建一个`ChunkReader`对象,用于以块的形式读取输入流。块的大小是1024倍的`BS`。

11. `while (!finished) {`:开始一个循环,直到所有数据都被处理完毕。

12. `byte[] chunk = reader.readChunk(input);`:从输入流中读取一个数据块。

13. `if (chunk.length <= 0 || (chunk.length % BS) != 0) {`:检查读取的数据块是否是最后一个块或者块的大小不是BS的倍数。

14. `int padding_length = chunk.length <= 0 ? BS : (BS - (chunk.length % BS));`:计算需要填充的字节数。如果`chunk`的长度小于等于0,填充整个块大小`BS`;否则填充到下一个`BS`的倍数。

15. `chunk = Bytes.concat(chunk, padding(padding_length));`:将原始数据块与填充数据合并。`padding`方法应该返回指定长度的填充字节。

16. `finished = true;`:设置`finished`为`true`,表示这是最后一个数据块。

17. `if (chunk.length > 0) {`:如果数据块不为空,则进行加密。

18. `output.write(cipher.update(chunk));`:使用`Cipher`对象的`update`方法对数据块进行加密,并将加密后的数据写入输出流。

19. `}`:循环结束。

20. `}`:方法结束。

**举例说明**:

假设我们有一个文件,我们想要加密这个文件的内容,并且我们有一个密码`"mypassword"`。我们首先需要定义块大小`BS`,比如16字节。然后我们可以调用`encrypt18`方法,传入文件的输入流和加密数据的输出流(比如另一个文件或网络流)。
FileInputStream fileInputStream = new FileInputStream("path/to/input/file");
FileOutputStream fileOutputStream = new FileOutputStream("path/to/output/file");
encrypt18(fileInputStream, fileOutputStream);
在这个方法中,我们首先生成一个盐值,然后使用密码和盐值创建一个`Cipher`对象。我们读取输入文件的每个块,如果块的大小不是16字节的倍数,我们会添加填充。然后我们加密每个块,并将加密后的数据写入输出文件。

请注意,这段代码是不完整的,因为它依赖于一些未定义的方法和变量,比如`get_random_bytes`、`getEncryptCipher`、`padding`和`ChunkReader`。此外,代码中还有一些错误,比如`padding_legnth`应该是`padding_length`。
import javax.crypto.Cipher;
import com.googlemon.primitives.Bytes;

public void encrypt18(InputStream input, OutputStream output) throws Exception {
    byte[] algorithm = "Salted__".getBytes("ascii");
    byte[] salt = get_random_bytes(BS - algorithm.length);
    Cipher cipher = getEncryptCipher(this.password, salt);

    output.write(algorithm);
    output.write(salt);
    boolean finished = false;
    ChunkReader reader = new ChunkReader(1024 * BS);
    while (!finished) {
        byte[] chunk = reader.readChunk(input);
        if (chunk.length <= 0 || (chunk.length % BS) != 0) {
            int padding_length = chunk.length <= 0 ? BS : (BS - (chunk.length % BS));
            chunk = Bytes.concat(chunk, padding(padding_legnth));
            finished = true;
        }
        if (chunk.length > 0) {
            output.write(cipher.update(chunk));
        }
    }
}
这段代码是一个用于加密数据流的Java方法。它使用了Java加密架构(JCA)中的`Cipher`类来对数据进行加密。以下是代码的逐行解释:

1. `import javax.crypto.Cipher;`:导入`Cipher`类,这是Java加密架构(JCA)的一部分,用于加密和解密数据。

2. `import com.googlemon.primitives.Bytes;`:导入`Bytes`类,这是一个来自Google Guava库的实用程序类,用于操作字节。

3. `public void encrypt18(InputStream input, OutputStream output) throws Exception {`:定义了一个名为`encrypt18`的方法,它接受一个`InputStream`和一个`OutputStream`作为参数,并可能抛出异常。这个方法用于读取输入流中的数据,加密它,然后将加密后的数据写入输出流。

4. `byte[] algorithm = "Salted__".getBytes("ascii");`:将字符串`"Salted__"`转换为ASCII编码的字节数组。这通常是指定加密算法的字符串,例如`"AES/CBC/PKCS5Padding"`。

5. `byte[] salt = get_random_bytes(BS - algorithm.length);`:调用`get_random_bytes`方法生成一个随机的盐值,长度为`BS - algorithm.length`。`BS`可能是一个定义块大小的常量,例如128位(16字节)。

6. `Cipher cipher = getEncryptCipher(this.password, salt);`:调用`getEncryptCipher`方法创建一个`Cipher`对象,使用提供的密码和盐值初始化它进行加密操作。

7. `output.write(algorithm);`:将加密算法的字节数组写入输出流。

8. `output.write(salt);`:将生成的盐值写入输出流。

9. `boolean finished = false;`:初始化一个布尔变量`finished`,用于控制加密循环。

10. `ChunkReader reader = new ChunkReader(1024 * BS);`:创建一个`ChunkReader`对象,用于以块的形式读取输入流。块的大小是1024倍的`BS`。

11. `while (!finished) {`:开始一个循环,直到所有数据都被处理完毕。

12. `byte[] chunk = reader.readChunk(input);`:从输入流中读取一个数据块。

13. `if (chunk.length <= 0 || (chunk.length % BS) != 0) {`:检查读取的数据块是否是最后一个块或者块的大小不是BS的倍数。

14. `int padding_length = chunk.length <= 0 ? BS : (BS - (chunk.length % BS));`:计算需要填充的字节数。如果`chunk`的长度小于等于0,填充整个块大小`BS`;否则填充到下一个`BS`的倍数。

15. `chunk = Bytes.concat(chunk, padding(padding_length));`:将原始数据块与填充数据合并。`padding`方法应该返回指定长度的填充字节。

16. `finished = true;`:设置`finished`为`true`,表示这是最后一个数据块。

17. `if (chunk.length > 0) {`:如果数据块不为空,则进行加密。

18. `output.write(cipher.update(chunk));`:使用`Cipher`对象的`update`方法对数据块进行加密,并将加密后的数据写入输出流。

19. `}`:循环结束。

20. `}`:方法结束。

**举例说明**:

假设我们有一个文件,我们想要加密这个文件的内容,并且我们有一个密码`"mypassword"`。我们首先需要定义块大小`BS`,比如16字节。然后我们可以调用`encrypt18`方法,传入文件的输入流和加密数据的输出流(比如另一个文件或网络流)。
FileInputStream fileInputStream = new FileInputStream("path/to/input/file");
FileOutputStream fileOutputStream = new FileOutputStream("path/to/output/file");
encrypt18(fileInputStream, fileOutputStream);
在这个方法中,我们首先生成一个盐值,然后使用密码和盐值创建一个`Cipher`对象。我们读取输入文件的每个块,如果块的大小不是16字节的倍数,我们会添加填充。然后我们加密每个块,并将加密后的数据写入输出文件。

请注意,这段代码是不完整的,因为它依赖于一些未定义的方法和变量,比如`get_random_bytes`、`getEncryptCipher`、`padding`和`ChunkReader`。此外,代码中还有一些错误,比如`padding_legnth`应该是`padding_length`。

本文标签: 详解文件加密方法java