JAVA Cipher 加密C 语言无法解密问题总结
来源:zh_harry     阅读:1197
云上智慧
发布于 2018-12-13 23:17
查看主页

问题

AES算法(DES等其余算法一样)。AES算法有四种模式 CBC/ECB/CFB/OFB,这四种Java和C都有实现。AES算法还有末尾的填充(padding),java支持的padding方式有三种NoPadding/PKCS5Padding/,而C却不能显式的设置padding方式,默认的padding就是在末尾加 '\0'。这是一个大坑,多少人都坑在这了。另外,网上很多JAVA AES算法,很多都用SecureRandom,假如你的代码中出现了SecureRandom这个东西,那么你再也不能用C解出来了。

password.jpeg

https://docs.oracle.com/javase/7/docs/technotes/guides/security/crypto/CryptoSpec.html#trans

Creating a Cipher Object

Cipher objects are obtained by using one of the Cipher getInstance() static factory methods. Here, the algorithm name is slightly different than with other engine classes, in that it specifies not just an algorithm name, but a "transformation". A transformation is a string that describes the operation (or set of operations) to be performed on the given input to produce some output. A transformation always includes the name of a cryptographic algorithm (e.g., AES), and may be followed by a mode and padding scheme.

Cipher对象是通过使用Cipher getInstance()静态工厂方法取得的。这里,算法名称与其余引擎类略有不同,由于它不仅指定算法名称,而且指定“转换”。转换是形容要对给定输入执行的操作(或者一组操作)以产生某些输出的字符串。转换总是包含加密算法(例如AES)的名称,而后可能是模式和填充方案。

A transformation is of the form:

"algorithm/mode/padding" or
"algorithm"
For example, the following are valid transformations:

    "AES/CBC/PKCS5Padding"    "AES"

If just a transformation name is specified, the system will determine if there is an implementation of the requested transformation available in the environment, and if there is more than one, returns there is a preferred one.

If both a transformation name and a package provider are specified, the system will determine if there is an implementation of the requested transformation in the package requested, and throw an exception if there is not.

It is recommended to use a transformation that fully specifies the algorithm, mode, and padding. By not doing so, the provider will use a default. For example, the SunJCE and SunPKCS11 providers uses ECB(默认模式) as the default mode, and PKCS5Padding(默认对齐方式) as the default padding for many symmetric ciphers.

This means that in the case of the SunJCE provider:

    Cipher c1 = Cipher.getInstance("AES/ECB/PKCS5Padding");

and

    Cipher c1 = Cipher.getInstance("AES");

are equivalent statements.

由于默认模式和默认的对齐方式,上边的两条语句是等价的.

https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html

Cipher

Cipher The algorithms are specified as transformations. Implementations must support the key sizes in parentheses. AES/CBC/NoPadding (128)

总结:

异构语言加解密在保证密钥一致的前题下,还要保证以上三个参数是一致的,对于密码和加密原文要注意添充模式,假如不没有实现,需要手动添充。

demo见下文

引用

https://my.oschina.net/gesuper/blog/174035

免责声明:本文为用户发表,不代表网站立场,仅供参考,不构成引导等用途。 系统环境 服务器应用
相关推荐
Android Q 要来了,一个影响国内 90% App 的适配项!
HTML5入门
对Android及移动互联网的大局观看法!
看了此文,你还敢说你懂了Javascript运算符吗
对常见的css属性进行浏览器兼容性总结
首页
搜索
订单
购物车
我的