0

我对使用 Bouncy Castle 的 Blowfish 加密在 Android 上的加密过程非常慢感到相当惊讶。一个 3 mb 的文件需要 3 多分钟。还有其他一些非常快的算法吗?我可以忍受不太可靠的安全性。这是代码。一切都在记忆中完成。没有文件。

private BufferedBlockCipher cipher;
private KeyParameter key;

public Encryption(byte[] key)
{
  try
  {
    BlowfishEngine blowfishEngine = new BlowfishEngine();
    CBCBlockCipher cbcBlockCipher = new CBCBlockCipher(blowfishEngine);

    cipher = new org.spongycastle.crypto.modes.PaddedBlockCipher(cbcBlockCipher);

    this.key = new KeyParameter(key);
  }
  catch (Exception ex)
  {
  }
}

public Encryption(String key)
{
  this(key.getBytes());
}

public synchronized byte[] Encrypt(byte[] data) throws CryptoException
{
  try
  {
    if (data == null || data.length == 0)
    {
      return new byte[0];
    }

    cipher.init(true, key);
    return CallCipher(data);
  }
  catch (Exception ex)
  {
    return null;
  }
}

private byte[] CallCipher(byte[] data) throws CryptoException
{
  try
  {
    int size = cipher.getOutputSize(data.length);
    byte[] result = new byte[size];
    int olen = cipher.processBytes(data, 0, data.length, result, 0);
    olen += cipher.doFinal(result, olen);

    if (olen < size)
    {
      byte[] tmp = new byte[olen];
      System.arraycopy(result, 0, tmp, 0, olen);
      result = tmp;
    }

    return result;
  }
  catch (Exception ex)
  {
    return null;
  }
}
4

1 回答 1

0

河豚实际上是一种快速密码。即使 Java 速度很慢,它也应该提供一些 Mb/sec。最有可能的问题在于您如何使用它(例如,以 8 字节块写入文件)。或者,BouncyCastle 团队是如何编码的。试试 AES-128,它应该更快。而且,最快的解决方案(而且安全性要低得多)是 RC4。

于 2013-02-06T11:05:44.843 回答