php 7.4 aes加密结果无法和java同步 40C

因业务需求和第三方公司对接,但aes加密结果始终无法和java同步,php可以加密解密,但php加密以后java无法正常解密,现贴出java加密后加密后字符串,烦请大家帮忙看一下,如何能使php和java的加密结果一样,万分感谢!
注:php版本 7.4+
使用加密函数 openssl_encrypt
加密字符串:123456
key:16个1
java密码学方式:AES/ECB/PKCS5Padding
java加密结果:4A85F03CF27F08BB397C52D363CA45AE
java代码:
package cn.com.ydnsh.util;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class AESUtil{

private static final String KEY_ALGORITHM = "AES";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法

/**
 * AES 加密操作
 *
 * @param content 待加密内容
 * @param key 加密密钥
 * @return 返回Base64转码后的加密数据
 */
public static String encrypt(String content, String key) {
    try {
        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器

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

        cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));// 初始化为加密模式的密码器

        byte[] result = cipher.doFinal(byteContent);// 加密

        return bytesToHexString(result);//通过Base64转码返回

    } catch (Exception ex) {
        Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

/**
 * AES 解密操作
 *
 * @param content
 * @param key
 * @return
 */
public static String decrypt(String content, String key) {

    try {
        //实例化
        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
        //使用密钥初始化,设置为解密模式
        cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
        //执行操作
        byte[] result = cipher.doFinal(hexStringToBytes(content));
        return new String(result, "utf-8");
    } catch (Exception ex) {
        Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

/**
 * 生成加密秘钥
 *
 * @return
 */
private static SecretKeySpec getSecretKey(final String key) {
    //返回生成指定算法密钥生成器的 KeyGenerator 对象
    KeyGenerator kg = null;

    try {
        kg = KeyGenerator.getInstance(KEY_ALGORITHM);

        SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(key.getBytes());
        //AES 要求密钥长度为 128
        kg.init(128, random);

        //生成一个密钥
        SecretKey secretKey = kg.generateKey();

        return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);// 转换为AES专用密钥
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

public static void main(String[] args) {
    String content = "hello,您好";
    String key = "1iq3YbsllQO2Cati";       
    String contents = "+S4n66vTZKxGbRidE8uZ/H8evmIm2gMjcwgL5yME+83qArJLbvQoCNU5IS7dACFJpKjDVIcn1DsJoCQktzj8bVfCIGVfcqMSazJxCJqBXfZ4VTzqZ87sIkDbMk05HBmW2vmxp9KQAsG+1o89UHArIHeNk+fN81wAob3rMLETHJ2M5bkfLYszNP6qeVhMqauSs3q8fi0ZboENRPTt+oPRip4+AgtDjXKT6FBwhn2DC30zEnc/mZYy62AMAHZkik7HKShJwqxX2XzScY6Ynhat0hTqfJSCoIEDukjIQj/QMMU3D+vhRZhgbL5QZCj/JOL0mv7A3Yt3F8GZwEfa/z6m3yVPcM6Ek/U5atNEyPbx4xg=";       
    System.out.println("content:" + content);
    String s1 = AESUtil.encrypt(content, key);
    System.out.println("s1:" + s1);
    System.out.println("s2:"+AESUtil.decrypt(s1, key));
    System.out.println("s3:"+AESUtil.decrypt(contents, key));

}
/**
 * 数组转换成十六进制字符串
 * 
 * @param byte[]
 * @return HexString
 */
public static final String bytesToHexString(byte[] bArray) {
    StringBuffer sb = new StringBuffer(bArray.length);
    String sTemp;
    for (int i = 0; i < bArray.length; i++) {
        sTemp = Integer.toHexString(0xFF & bArray[i]);
        if (sTemp.length() < 2)
            sb.append(0);
        sb.append(sTemp.toUpperCase());
    }
    return sb.toString();
}
/**
 * 将16进制字符串转换为byte[]
 * 
 * @explain 16进制字符串不区分大小写,返回的数组相同
 * @param hexString
 *            16进制字符串
 * @return byte[]
 */
public static byte[] hexStringToBytes(String hexString) {
    if (null == hexString || "".equals(hexString.trim())) {
        return new byte[0];
    }

    byte[] bytes = new byte[hexString.length() / 2];
    // 16进制字符串
    String hex;
    for (int i = 0; i < hexString.length() / 2; i++) {
        // 每次截取2位
        hex = hexString.substring(i * 2, i * 2 + 2);
        // 16进制-->十进制
        bytes[i] = (byte) Integer.parseInt(hex, 16);
    }
    return bytes;
}

}

php

3个回答

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @author Administrator
 * @create 2019-03-20
 * @desc 安全工具类
 **/
public class SecurityUtil {
    public static final String KEY = "09bd821d3e764f44899a9dc6";
    public static final String IV = "2M9tOpWi";
    public static final String DEFAULT_ENC_NAME = "UTF-8";

    public static String java_openssl_encrypt(String data) {
        return java_openssl_encrypt(data, IV);
    }

    /**
     * java_openssl_encrypt加密算法
     *
     * @param data
     * @param iv
     * @return
     * @throws Exception
     */
    public static String java_openssl_encrypt(String data, String iv) {
        try {
            Cipher cipher = createCipher(iv, Cipher.ENCRYPT_MODE);
            return URLEncoder.encode(Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes())), DEFAULT_ENC_NAME);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String java_openssl_decrypt(String data) {
        return java_openssl_decrypt(data, IV);
    }

    /**
     * java_openssl_decrypt解密
     *
     * @param data
     * @param iv
     * @return
     */
    public static String java_openssl_decrypt(String data, String iv) {
        try {
            Cipher cipher = createCipher(iv, Cipher.DECRYPT_MODE);
            return new String(cipher.doFinal(Base64.getDecoder().decode(URLDecoder.decode(data, DEFAULT_ENC_NAME))));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建密码器Cipher
     *
     * @param iv
     * @param mode 加/解密模式
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws InvalidAlgorithmParameterException
     */
    private static Cipher createCipher(String iv, int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, 
InvalidKeyException, InvalidAlgorithmParameterException {
        byte[] key = KEY.getBytes();
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        cipher.init(mode, new SecretKeySpec(key, "DESede"), ivParameterSpec);
        return cipher;
    }
}

https://www.cnblogs.com/liaojie970/p/10690981.html

加密解密一时半会也看不懂 看不出问题出在哪

最快的方法就是 百度一个PHP加密算法 不行 的 再换下一个 多找几个
比如找三个 如果三个的加密结果都是一样的 并且能解密

那就要怀疑Java那边的解密算法有问题了

我的资源里面有这个解决的代码。就是对接java 的

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐