Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

求助:java AES加密 转php AES

哪位大神能帮我把这段java的aes加密写成php版的


package com.ruoyi.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class SymmetricEncoder {

    private static Logger symmetricEncoderlogger = LoggerFactory.getLogger(SymmetricEncoder.class);

    /*
 * 加密 1.构造密钥生成器 2.根据ecnodeRules规则初始化密钥生成器 3.产生密钥 4.创建和初始化密码器 5.内容加密 6.返回字符串
 */
public static String AESEncode(String encodeRules, String content) {
    try {
        // 1.构造密钥生成器,指定为AES算法,不区分大小写
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        // 2.根据ecnodeRules规则初始化密钥生成器
        // 生成一个128位的随机源,根据传入的字节数组
        keygen.init(128, new SecureRandom(encodeRules.getBytes()));
        // 3.产生原始对称密钥
        SecretKey original_key = keygen.generateKey();
        // 4.获得原始对称密钥的字节数组
        byte[] raw = original_key.getEncoded();
        // 5.根据字节数组生成AES密钥
        SecretKey key = new SecretKeySpec(raw, "AES");
        // 6.根据指定算法AES自成密码器
        Cipher cipher = Cipher.getInstance("AES");
        // 7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
        cipher.init(Cipher.ENCRYPT_MODE, key);
        // 8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
        byte[] byte_encode = content.getBytes("utf-8");
        // 9.根据密码器的初始化方式--加密:将数据加密
        byte[] byte_AES = cipher.doFinal(byte_encode);
        // 10.将加密后的数据转换为字符串
        // 这里用Base64Encoder中会找不到包
        // 解决办法:
        // 在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
        String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
        // 11.将字符串返回
        return AES_encode;
    } catch (Exception e) {
        symmetricEncoderlogger.error("AES加密失败",e);
    }

    // 如果有错就返加nulll
    return null;
}

public static String stringToMD5(String plainText) {
    byte[] secretBytes = null;
    try {
        MessageDigest digest = MessageDigest.getInstance("md5");
        digest.update(plainText.getBytes());
        secretBytes = digest.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("没有这个md5算法!");
    }
    String md5code = new BigInteger(1, secretBytes).toString(16);
    return md5code;
}

public static void main(String[] args) {
    String cipher = stringToMD5(AESEncode("密钥", "XML内容"));
    System.out.println("生成的密文为:"+cipher);
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...