AES加密解密 PHP+JAVA互通
1:php 加密
<?php
declare(strict_types=1);
namespace App\Constants;
class Security {
/**
*
* @param string $string 需要加密的字符串
* @param string $key 密钥
* @return string
*/
public static function encrypt($string, $key)
{
$key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
// openssl_encrypt 加密不同Mcrypt,对秘钥长度要求,超出16加密结果不变
$data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
$data = base64_encode($data);
return $data;
}
/**
* @param string $string 需要解密的字符串
* @param string $key 密钥
* @return string
*/
public static function decrypt($string, $key)
{
$key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$encrypted = base64_decode($string);
return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
}
}
使用
$key = "root:123456789as";
$pass = Security::encrypt("123456", $key);
print_r($pass);
//pvHHE4r31k0yC82me2bi0A==
$pass2 = Security::decrypt($pass, $key);
print_r($pass2);
//123456
2:JAVA加密
package com.zyd.blog.util;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AES1 {
/**
* 加密
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String Encrypt(String sSrc, String sKey) throws Exception {
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new Base64().encodeToString(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
}
/**
* 解密
* @param sSrc
* @param sKey
* @return
* @throws Exception
*/
public static String Decrypt(String sSrc, String sKey) throws Exception {
try {
// 判断Key是否正确
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] encrypted1 = new Base64().decode(sSrc);//先用base64解密
try {
byte[] original = cipher.doFinal(encrypted1);
return new String(original,"utf-8");
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
}
使用
@GetMapping("/encrypt")
@BussinessLog(value = "加密测试", platform = PlatformEnum.WEB)
public ResponseVO encrypt() {
try {
/*
* 此处使用AES-128-ECB加密模式,key需要为16位。
*/
String cKey = "root:123456789as";
// 需要加密的字串
String cSrc = "123456";
System.out.println(cSrc);
// 加密
String enString = AES1.Encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
// 解密
String DeString = AES1.Decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
} catch (Exception e) {
return ResultUtil.error(e.getMessage());
}
return ResultUtil.success("");
}
本文来自博客园,作者:给香菜送点香菜,转载请注明原文链接:https://www.cnblogs.com/mingkewang/articles/17160192.html

浙公网安备 33010602011771号