简体   繁体   English

将PHP加密和解密方法转换为Java

[英]Convertion of PHP encryption and decryption methods into java

I tried to convert the encrypt and decrypt functions to use in Java for the below PHP. 我试图将加密和解密函数转换为在Java中用于以下PHP。 But received illegal key size error. 但是收到非法的密钥大小错误。 Suggest me to do 256bit AES decryption in java. 建议我在Java中执行256位AES解密。

PHP Code PHP代码

<?php
    function encrypt ($data,$salt) {
        $hash = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($salt), $data, MCRYPT_MODE_CBC, md5(md5($salt))));
        return $hash;
    }
    function decrypt ($encdata,$salt) {
        $string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($salt), base64_decode($encdata), MCRYPT_MODE_CBC, md5(md5($salt)));
        return $string;
    }
?>

Converted Java Code: 转换后的Java代码:

//The below code found in http://www.logikdev.com/2010/11/01/encrypt-with-php-decrypt-with-java/
    public static String md5(String input) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(input.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        return number.toString(16);
    }

    public String decrypt(String encryptedData) {
        String decryptedData = null;
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(md5("5A17K3Y").getBytes(), "AES");           
            String initialVectorString=md5(md5("5A17K3Y"));
            IvParameterSpec initialVector = new IvParameterSpec(initialVectorString.getBytes());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding","SunJCE");            
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, initialVector);          
            encryptedData=encryptedData.replace('-','+').replace('_','/').replace(',','=');
            byte[] encryptedByteArray = (new org.apache.commons.codec.binary.Base64()).decode((encryptedData.getBytes()));          
            byte[] decryptedByteArray = cipher.doFinal(encryptedByteArray);
            decryptedData = new String(decryptedByteArray, "UTF8");
        } catch (Exception e) {         
            System.out.println("Error. Problem decrypting the data: " + e);
        }
    }

Problem decrypting the data: java.security.InvalidKeyException: Illegal key size 解密数据时出现问题:java.security.InvalidKeyException:密钥大小非法

You receive an illegal key size error when you've not installed the unlimited strength jurisdiction files. 如果尚未安装无限强度管辖区文件,则会收到非法密钥大小错误。 These files permit Java to use stronger key lengths (for example 256-bit AES). 这些文件允许Java使用更长的密钥长度(例如256位AES)。

Go to the Oracle download site and hunt for the files that match your Java version. 转到Oracle下载站点并寻找与Java版本匹配的文件。 If it is legal in your country to do so, install these files and enjoy stronger crypto. 如果在您所在国家/地区这样做是合法的,请安装这些文件并享受更强大的加密功能。

( Side note: did you research this at all? The first upteem results would have answered your issue ). 旁注:您是否对此进行了研究?第一个更新的结果将回答您的问题 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM