我对使用 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;
}
}