c 加密 java解密错误_JAVA Cipher 加密C 语言无法解密问题总结

问题

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

4f66cb16511f4c5cc6eefd458d3ba54c.pngpassword.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");

andCipher 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)AES/CBC/PKCS5Padding (128)

AES/ECB/NoPadding (128)

AES/ECB/PKCS5Padding (128)

DES/CBC/NoPadding (56)

DES/CBC/PKCS5Padding (56)

DES/ECB/NoPadding (56)

DES/ECB/PKCS5Padding (56)

DESede/CBC/NoPadding (168)

DESede/CBC/PKCS5Padding (168)

DESede/ECB/NoPadding (168)

DESede/ECB/PKCS5Padding (168)

RSA/ECB/PKCS1Padding (1024, 2048)

RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)

RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)

总结:

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

demo见下文

引用

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值