-1

我一直在尝试使用 AES 加密 android 中的一些 XML 文件,使用 FTP 将它们发送到服务器,然后使用 java 在 Linux 终端中对它们进行解密。

我尝试使用 AES、DES、Triple DES 和其他加密方法进行加密,但是通过 FTP 将文件发送到服务器后,其中一些(约 25%)无法解密,总是相同的。我还尝试在 android 模拟器中解密接收到的文件,但它也不起作用

还尝试下载 spongycastle 罐子并使用它们而不是 BouncyCastle 库,我在某个地方读到过时的 android java,但它也不起作用。

我还尝试在加密文件之前删除每个 \r\n 。

我现在使用的库是:

编码解码AES.java

我现在得到的错误是“javax.crypto.IllegalBlockSizeException:解密时最后一个块不完整”

我目前用于加密/解密的代码是:

String fileNotEncrypted=Environment.getExternalStorageDirectory() + "unencrypted.xml";
RandomAccessFile fileNotEncryptedRa = new RandomAccessFile(fileNotEncrypted, "r");
byte[] textNotEncryptedByte = new byte[(int)fileNotEncryptedRa.length()];
fileNotEncryptedRa.read(textNotEncryptedByte);

Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
byte[] textEncryptedByte = EncodeDecodeAES.encryptBytes("1234567890123456", textNotEncryptedByte);      

String fileEncrypted=Environment.getExternalStorageDirectory() + "encrypted.xml";
FileOutputStream fos = new FileOutputStream(fileEncrypted);
fos.write(textEncryptedByte);
fos.close();

这是我用来解密我通过 FTP 收到的文件的代码

Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);

//encryptedFile is a String I get from main(String[] args), so I can use the code in a script easily
RandomAccessFile fileEncrypted = new RandomAccessFile(encryptedFile, "r");
byte[] textEncryptedByte = new byte[(int)fileEncrypted.length()];
fileEncrypted.read(textEncryptedByte);

byte[] textDecryptedByte = EncodeDecodeAES.decryptBytes("Dephi20101234567", textEncryptedByte);

//decriptedFile is another String got from main(String[] args)
FileOutputStream fos = new FileOutputStream(decryptedFile);
fos.write(textDecryptedByte);
fos.close();

解决方案:

该问题是由于尝试通过 FTP 发送密文引起的。我要做的就是将密文编码为base64,然后将base64写入一个文件,然后通过FTP发送该文件。

对于 Base64 编码,我使用的是在这里找到的开源库: Base64Coder

4

1 回答 1

1

在解密在 Linux 上加密的文件(反之亦然)时,我遇到了关于 Base64 类 android 的非常类似的问题。Linux 只是在使用\n(或者\r只是,不记得了)而不是\r\n在进行中。这导致文件具有不同的长度并且解密失败。上面,您说所有内容都已\r\n删除,但我猜您没有检查\n. 尝试检查这两种情况应该是一致的。

最后,http ://examples.oreilly.com/9781565924024/files/oreilly/jonathan/util/Base64.java 是我在 android 和 Linux 上都使用的 Base64 类,以确保一致性。

于 2013-03-26T16:09:48.827 回答