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

需要Similler Java代码进行php加密脚本

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

需要Similler Java代码进行php加密脚本

这应该做。

public static byte[] encrypt(byte[] decrypted, byte[] keyvalue) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{    MessageDigest sha256 = MessageDigest.getInstance("SHA-256");    byte[] key = sha256.digest(keyvalue);    MessageDigest md5 = MessageDigest.getInstance("MD5");    byte[] checksum = md5.digest(decrypted);    //The length of the value to encrypt must be a multiple of 16.    byte[] decryptedAndChecksum = new byte[(decrypted.length + md5.getDigestLength() + 15) / 16 * 16];    System.arraycopy(decrypted, 0, decryptedAndChecksum, 0, decrypted.length);    System.arraycopy(checksum, 0, decryptedAndChecksum, decrypted.length, checksum.length);    //The remaining bytes of decryptedAndChecksum stay as 0 (default byte value) because PHP pads with 0's.    SecureRandom rnd = new SecureRandom();    byte[] iv = new byte[16];    rnd.nextBytes(iv);    IvParameterSpec ivSpec = new IvParameterSpec(iv);    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), ivSpec);    byte[] encrypted = base64.enprebase64(cipher.doFinal(decryptedAndChecksum));    byte[] ivbase64 = base64.enprebase64String(iv).substring(0, 22).getBytes();    byte[] output = new byte[encrypted.length + ivbase64.length];    System.arraycopy(ivbase64, 0, output, 0, ivbase64.length);    System.arraycopy(encrypted, 0, output, ivbase64.length, encrypted.length);    return output;}

Java中的MCRYPT_RIJNDAEL_128和MCRYPT_MODE_CBC等效于AES / CBC /
NoPadding。您还需要一个用于base64编码的实用程序,以上代码

base64
在Apache Codec库中使用。

另外,由于加密密钥是256位,因此您将需要 Java密码学扩展(JCE)无限强度管辖权策略文件 。这些可以从Oracle网站下载。

最后,请注意ntoskrnl的警告。这种加密确实可以更好,不要从PHP手册中复制粘贴。



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

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

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