douxiangui5011 2019-01-11 14:16
浏览 123

AesEncryption在Java和PHP代码中导致不同的结果,为什么?

I am connecting a third party interface and they only gave me a Java code demo, I need to connect it with my PHP system.But I can't handle the encryption, my encryption proccess in PHP always gets different result with the third party Java code.

I install phpseclib/phpseclib package via composer in order to perform AES encryption.

Java Encryption Code:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
private static final String KEY_ALGORITHM = "AES";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

public static String encrypt(String content, String encryptKey) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

            byte[] byteContent = content.getBytes("utf-8");

            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(encryptKey));
            byte[] result = cipher.doFinal(byteContent);


            return toHexString(result);
        } catch (Exception ex) {
        }

        return null;
    }  

private static SecretKeySpec getSecretKey(final String encryptKey) {
        KeyGenerator kg = null;

        try {
            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(encryptKey.getBytes());
            kg.init(128, secureRandom);


            SecretKey secretKey = kg.generateKey();
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException ex) {
        }

        return null;
    }

public static String toHexString(byte b[]) {
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < b.length; i++) {
        String plainText = Integer.toHexString(0xff & b[i]);
        if (plainText.length() < 2)
            plainText = "0" + plainText;
        hexString.append(plainText);
    }
    return hexString.toString();
}

PHP encryption code:

//composer require phpseclib/phpseclib
use phpseclib\Crypt\AES;
function aesEncrypt($message, $key)
{
    $cipher = new AES(AES::MODE_ECB);
    $cipher->setKeyLength(128);
    $cipher->setKey(hex2bin($key));

    $cryptText = $cipher->encrypt($message);

    return bin2hex($cryptText);
}

Java Result:

before:testStringtestString
key:acccd6fa0caf52a0e5e5fda8bd3ff55a
after:2bbd3011eb084c9494228fe913e6e033aaffb1aa04ef9d0f14614c21fd16af9a

PHP Result:

before:testStringtestString
key:acccd6fa0caf52a0e5e5fda8bd3ff55a
after:d9a683511cdeb174bf51a285140071a8b38b57c6c6133d1b9425846ae0ec333b
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入