栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP Java AES CBC加密不同的结果

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP Java AES CBC加密不同的结果

如果您不只是简单地

Exception
encrypt()
例程中的可能内容吞噬掉,那么您将对发生的事情有了更好的了解。如果函数正在返回,
null
那么显然发生了异常,您需要知道它是什么。

实际上,例外是:

javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes    at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:854)    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:828)    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)    at javax.crypto.Cipher.doFinal(Cipher.java:2087)    at Encryption.encrypt(Encryption.java:20)    at Encryption.main(Encryption.java:6)

可以肯定的是,您的纯文本长度只有11个Java字符,按照您的默认编码,它将为11个字节。

您需要检查PHP

mcrypt_encrypt
函数的实际作用。由于它有效,因此显然使用了一些填充方案。您需要找出它是哪一个,并在Java代码中使用它。

好的-我查找了手册页

mcrypt_encrypt
。它说:

将使用给定的密码和模式加密的数据。如果数据大小

n * blocksize
不足,数据将被填充

因此,您需要使用Java复制该代码。这是一种方法:

import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;public class Encryption{    public static void main(String args[]) throws Exception {        System.out.println(encrypt());    }    public static String encrypt() throws Exception {        try { String data = "Test string"; String key = "1234567812345678"; String iv = "1234567812345678"; Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); int blockSize = cipher.getBlockSize(); // We need to pad with zeros to a multiple of the cipher block size, // so first figure out what the size of the plaintext needs to be. byte[] dataBytes = data.getBytes(); int plaintextLength = dataBytes.length; int remainder = plaintextLength % blockSize; if (remainder != 0) {     plaintextLength += (blockSize - remainder); } // In java, primitive arrays of integer types have all elements // initialized to zero, so no need to explicitly zero any part of // the array. byte[] plaintext = new byte[plaintextLength]; // Copy our actual data into the beginning of the array.  The // rest of the array is implicitly zero-filled, as desired. System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); return new sun.misc.base64Enprer().enpre(encrypted);        } catch (Exception e) { e.printStackTrace(); return null;        }    }}

当我跑步时,我得到:

iz1qFlQJfs6Ycp+gcc2z4w==

这就是您的PHP程序所得到的。


更新(2016年6月12日) :从Java 8开始,JavaSE最终随附了已记录的base64编解码器。所以代替

return new sun.misc.base64Enprer().enpre(encrypted);

你应该做类似的事情

return base64.Enprer.enpreToString(encrypted);

或者,使用第三方库(例如

commons-prec
)进行base64编码/解码,而不要使用未公开的内部方法。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419824.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号