【问题标题】:How to encrypt an InputStream with JNCryptor如何使用 JNCryptor 加密 InputStream
【发布时间】:2013-04-01 02:43:37
【问题描述】:

我正在开发一个 iPad 应用程序并使用 RNCryptor 在设备上进行加密和解密。这种加密格式有一个 Java 版本,格式为 JNCryptor

我现在要从 InputStream 中读取数据,但我想在读取数据之前对其进行加密。我找到了一个名为CipherInputStream 的类,它似乎完全符合我的要求。唯一的问题是,我需要一个Cipher(和Provider)来指定加密方法,我不知道该怎么做。甚至可以定义一个自定义的 Provider 吗?

有人对使用 JNCryptor 加密 InputStream 的替代方法有建议吗?

【问题讨论】:

  • 我会的,谢谢 :) 现在我已经编写了自己的代码,但是如果我可以调用类似“更新”的方法,而不必做所有事情,那就太好了。我认为我现在可以使用的库中唯一的方法是生成密钥,哈哈。我已经在这个问题的答案中发布了我的解决方案,您也许可以使用它:)

标签: java encryption inputstream jncryptor


【解决方案1】:

您可以使用默认的 Java 提供程序。要实例化您将使用的密码

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")

这使用AESCipher Block Chaining mode。 CBC 仅适用于 16 字节的倍数,因此您还指定了一种将输入填充为 16 字节的倍数的方法。

Here is some more sample AES code 让您开始

【讨论】:

  • 那是和 RNCryptor 一样的加密方法吗?因为我需要用那个库解密 iPad 上的数据。
  • 我研究了一下,我想我应该能够用 Cipher 模仿 RNCryptor 的加密方法,但我需要 RNCryptor 的特定格式来解密它的客户端。我开始认为我可能需要将数据读入内存并将加密数据输出到管道输出流或其他东西......
  • 或者我应该只使用 SSL,doh
  • 我最终编写了基于 JNCryptor 的流加密代码,因为 SSL 根本不是解决方案。
【解决方案2】:

最后,我编写了一个类来读取 InputStream,一次加密数据部分,然后写入 PipedOutputStream。然后我将这个 PipedOutputStream 连接到一个 PipedInputStream,我最终返回了它。加密和写入 PipedOutputStream 发生在单独的线程上以避免死锁。

PipedInputStream pin = new PipedInputStream();
PipedOutputStream pout = new PipedOutputStream(pin);
EncryptionPipe pipe = new EncryptionPipe(5, pout, in, cipher, mac, metaData);
//EncryptionPipe(int interval, OutputStream out, InputStream in
//              ,Cipher cipher, Mac mac, byte[] metaData)
pipe.start();
return pin;

在 EncryptionPipe 中:

public class EncryptionPipe extends Thread {
    ...
    @Override
    public void run() {
        try {
            mac.update(metaData);
            out.write(metaData);

            byte[] buf = new byte[1024];
            int bytesRead = 0;
            byte[] crypted;
            byte[] hmac;
            while ((bytesRead = in.read(buf)) != -1) {
                if (bytesRead < buf.length) {
                    //the doFinal methods add padding if necessary, important detail!
                    crypted = cipher.doFinal(buf, 0, bytesRead);
                    hmac = mac.doFinal(crypted);

                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    bytes.write(crypted);
                    bytes.write(hmac);
                    crypted = bytes.toByteArray();
                    bytesRead = crypted.length;
                    bytes.close();
                } else {
                    crypted = cipher.update(buf, 0, bytesRead);
                    mac.update(crypted, 0, bytesRead);
                }
                out.write(crypted, 0, bytesRead);
                synchronized (this) {
                    this.wait(interval);
                }
            }
            out.close();
            ...
        }
    }
}

【讨论】:

    【解决方案3】:

    JNCryptor v1.1.0 昨天发布,支持流式加解密。

    使用AES256JNCryptorInputStream进行解密,使用AES256JNCryptorOutputStream进行加密。

    【讨论】:

      猜你喜欢
      • 2015-01-10
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多