解密"SunJCE". Android上的AES加密数据 [英] Decrypting "SunJCE" AES encrypted data on Android

查看:310
本文介绍了解密"SunJCE". Android上的AES加密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要编写一些Android代码来解密从服务器发送的某些数据.我们的服务器团队为我们提供了一些使用"SunJCE"提供程序的示例解密代码,很遗憾,该提供程序在Android上不存在.

We need to write some Android code to decrypt some data sent from our server. Our server team gave us some sample decryption code which uses the "SunJCE" provider, which unfortunately doesn't exist on Android.

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");

有人知道在Android上实现此功能的最干净的方法吗?如果我们在Android上尝试

Does anybody know the cleanest way to implement this on Android? If we try this on Android

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

然后,在解密的文本的末尾似乎出现了一些不需要的垃圾,例如:

then it looks like some unwanted garbage appears at the end of the decrypted text, e.g:

ComparisonFailure: expected:<...lAAAAABJRU5ErkJggg==[]> but was:<...lAAAAABJRU5ErkJggg==[��������]>    

我们在Android Cipher类中尝试了许多不同转换的组合(例如"AES/CBC/PKCS5Padding"),但一直遇到BadPaddingExceptions之类的问题.

We tried lots of combinations of different transformations in the Android Cipher class (e.g. "AES/CBC/PKCS5Padding"), but kept hitting things like BadPaddingExceptions.

我们还能够使用纯Java模块解密该数据,该模块似乎没有显示相同的垃圾字符.有没有办法只使用Android类来做到这一点?

We were also able to decrypt this data using a Java-only module, which didn't appear to show the same garbage characters. Is there a way to do this using just Android classes?

推荐答案

Java代码中也存在相同的垃圾.只是您可能在使用默认拉丁(ISO_8859_1)字符集的Windows上运行此程序,而Android默认使用UTF-8.它还取决于用于打印字符的控制台和字体.在这种情况下,使用的填充可能不会在Windows控制台上打印,但会在Android代码上打印.

The same garbage is also present in the Java code. It's just that you probably run this on Windows that uses the default Latin (ISO_8859_1) character set and that Android uses UTF-8 by default. It also depends on the console and font used to print out the characters. In this case it is likely that the padding that is used doesn't print on the Windows console but does on the Android code.

您需要查看字节数组(例如以十六进制表示),以找出要使用的填充,然后将其剥离,然后再将纯文本转换为字符串.

You need to see the byte array (e.g. in hexadecimals) to find out which padding is used and then strip it off before turning the plaintext into a string.

这篇关于解密"SunJCE". Android上的AES加密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆