doupengxie4195 2013-07-18 11:18
浏览 35
已采纳

需要用于php加密脚本的similler java代码

I have a PHP encryption function. I need a java counter part for the same. Due to my limited knowledge in PHP I am unable to understand. Some one knows both the language, kindly help.

PHP code:

function encrypt($decrypted, $keyvalue) {
    // Build a 256-bit $key which is a SHA256 hash of $keyvalue.
    $key = hash('SHA256', $keyvalue, true);
    // Build $iv and $iv_base64.  We use a block size of 128 bits (AES compliant) and CBC mode.  (Note: ECB mode is inadequate as IV is not used.)
    srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
    if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
    // Encrypt $decrypted and an MD5 of $decrypted using $key.  MD5 is fine to use here because it's just to verify successful decryption.
    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
    // We're done!
    return $iv_base64 . $encrypted;
}

Thanks in advance Aniruddha

  • 写回答

1条回答 默认 最新

  • dpleylxzx47207117 2013-07-18 13:49
    关注

    This should do it.

    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.encodeBase64(cipher.doFinal(decryptedAndChecksum));
    
        byte[] ivBase64 = Base64.encodeBase64String(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;
    }
    

    The equivalent of MCRYPT_RIJNDAEL_128 and MCRYPT_MODE_CBC in java is AES/CBC/NoPadding. You also need a utility for Base64 encoding, the above code uses Base64 from the Apache Codec library.

    Also, because the encryption key is 256 bits, you'll need the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files. These can be downloaded from Oracle's website.

    Finally, do heed ntoskrnl's warning. This encryption really could be better, don't copy-paste from the PHP manual.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化