使用cipher可以很容易的实现3des加密,但是跟其他平台开发的3des加密对接来说,通常会有一些问题。基本的程序如下:
public static byte[] desEncrypt(String message, String key) throws Exception {
Cipher cipher = Cipher.getInstance("DESede");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(message.getBytes("UTF-8"));
』
我们跟其他平台对接发现对同样输入加密以后结果不同,看看jdk的文档,有如下描述:
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 is of the form:
- "algorithm/mode/padding" or
- "algorithm"
(in the latter case, provider-specific default values for the mode and padding scheme are used).
根据前面的代码,我们已经选择了正确的算法,那么加密不同的原因应该就是mode和padding了。
he SunJCE provider uses ECB as the default mode, and PKCS5Padding as the default padding scheme for DES, DES-EDE and Blowfish ciphers. This means that in the case of the SunJCE provider,
Cipher c1 = Cipher.getInstance("DES/ECB/PKCS5Padding");
and
Cipher c1 = Cipher.getInstance("DES");
are equivalent statements.
对于其他语言开发的3des,一定要采用相同的mode和padding才能保证通信。
分享到:
相关推荐
java实现的3DES加密算法,类中支持多种密钥长度
本程序是java语言实现的DES加密算法的实例。
Java实现文件的RSA和DES加密 说明代码
des加密解密java实现des加密解密java实现des加密解密java实现des加密解密java实现des加密解密java实现des加密解密java实现
用java实现了DES的加密算法 有可视化见面 缺点:输入的密钥最后为5为字符
Java 3des加密算法ECB模式,亲测完美通过。目前网上的大部分算法都通不过或者加进Base64之类的,还要不下载其它jar包。而代码使用时直接下载运行,无须配置和下载额外的jar包 ,只需换上自己的密钥和待加密的数据...
主要介绍了使用java自带des加密算法实现文件加密和字符串加密的示例,需要的朋友可以参考下
使用java实现的DES算法加密,带有界面。
JAVA实现DES非对称加密算法,根据秘钥对内容进行加密解密操作。
详细介绍JAVA中3des加密 解密算法
JAVA代码实现DES对称加密,同时加密后的数据为字符串类型,方便数据传输。
用Java实现的DES算法,可对文件进行加密解密
3des加密 java
通过java实现的DES加密算法,加深对加密机制的了解
java+3des加密+和字符串转换
DES加密的Java实现,从算法来实现。
NULL 博文链接:https://ping2010.iteye.com/blog/1571517
java_3des加密源码
java代码-使用java解决3DES加密解密的源代码 ——学习参考资料:仅用于个人学习使用!
基于Java实现了DES正确加解密txt、jpg、docx、MP3等格式的文件,3DES以及AES加解密都实现了,除此之外,还实现了五种分组密码工作模式,内容很多。