我對使用Bouncy Castle的Blowfish加密的Android加密過程的速度非常慢感到驚訝。一個3 MB的文件花了3分鐘。有一些其他算法非常快嗎?我可以忍受不太可靠的安全。這是代碼。一切都在記憶中完成。沒有文件。Blowfish加密對於3 MB文件的移動極其緩慢
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;
}
}
您沒有發佈您的代碼。正確使用河豚時,速度應該不會太慢。無論是BC的實現都很糟糕,還是以低效的方式使用它。爲什麼選擇河豚而不是AES? AES強大且快速。 – CodesInChaos
好的,我添加了上面的代碼。我選擇Blowfish只是因爲它在安全方面有很好的聲譽,並且規格說明速度很快。也許你是對的,BC的實施很糟糕。 – AndroidDev
我嘗試了這個移動應用程序,它使用AES。它在幾秒鐘內加密了一個3 MB的文件:http://goo.gl/fcOjy所以很明顯,有什麼不對勁。 – AndroidDev