aes php java_PHP JAVA AES加密

AES/CBC/PKCS5Padding加密

java AES加解密

public class AesEncryptUtils {

//加密

public static String Encrypt(String content, String key, String iv) throws Exception {

byte[] raw = key.getBytes("utf-8");

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"

//使用CBC模式,需要一个向量iv,可增加加密算法的强度

IvParameterSpec ips = new IvParameterSpec(iv.getBytes());

cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);

byte[] encrypted = cipher.doFinal(content.getBytes());

return new BASE64Encoder().encode(encrypted);

}

//解密

public static String Decrypt(String content, String key, String iv) throws Exception {

try {

byte[] raw = key.getBytes("utf-8");

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

IvParameterSpec ips = new IvParameterSpec(iv.getBytes());

cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);

byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);

try {

byte[] original = cipher.doFinal(encrypted1);

String originalString = new String(original);

return originalString;

} catch (Exception e) {

System.out.println(e.toString());

return null;

}

} catch (Exception ex) {

System.out.println(ex.toString());

return null;

}

}

public static void main(String[] args) throws Exception{

String KEY = "qxhzngy266a186ke";//密钥key

String IV = "1ci5crnda6ojzgtr";//向量iv

String encryptStr = AesEncryptUtils.Encrypt("hello你好", KEY, IV);

System.out.println(encryptStr);

System.out.println(AesEncryptUtils.Decrypt(encryptStr, KEY, IV));

}

}

php AES加解密

class Aes

{

private $iv = "";//iv的长度要根据加密方式和模式来定,aes-128-cbc偏移量的是16位

private $key = '';

function __construct($key, $iv)

{

$this->key = $key;$this->iv = $iv;

}

public function encrypt($input){

return base64_encode(openssl_encrypt($input, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv));

}

public function decrypt($input){

return openssl_decrypt(base64_decode($input), 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv);

}

}

$KEY = "qxhzngy266a186ke";//密钥key

$IV = "1ci5crnda6ojzgtr";//向量iv

$a = new Aes($KEY, $IV);

$encryptStr = $a->encrypt("hello你好");

echo $encryptStr.PHP_EOL;

echo $a->decrypt($encryptStr);

cc4565af9b889e15d906cdb55e33522e.png

附 java md5&base64

String str = Base64Utils.encodeToString("hello world 我".getBytes());

System.out.println(str);

System.out.println(new String(Base64Utils.decodeFromString(str)));

System.out.println(DigestUtils.md5Hex("localhost中文"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值