PHP7 解决 java对应的 AES/ECB/PKCS5Padding 算法

4 篇文章 0 订阅

先点击链接了解一下算法吧
在线生成AES加密

java

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class aes {
    //加密方法 str为传输的值 key取商户私钥字符串的前16位
    public static String aesEncrypt(String str, String key) throws Exception {
        if (str == null || key == null) return null;
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
        byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
        return new BASE64Encoder().encode(bytes);
    }

    public static String aesDecrypt(String str, String key) throws Exception {
        if (str == null || key == null) return null;
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
        byte[] bytes = new BASE64Decoder().decodeBuffer(str);
        bytes = cipher.doFinal(bytes);
        return new String(bytes, "utf-8");
    }
}

php

<?php
/**
 * Created by PhpStorm.
 * User: Shinelon
 * Date: 2018/7/19
 * Time: 14:22
 */

class AES
{
   public static function encrypt($data, $key) {
       $data =  openssl_encrypt($data, 'aes-128-ecb', base64_decode($key), OPENSSL_RAW_DATA);
       return base64_encode($data);
   }

    public static function decrypt($data, $key) {
        $encrypted = base64_decode($data);
        return openssl_decrypt($encrypted, 'aes-128-ecb', base64_decode($key), OPENSSL_RAW_DATA);
    }
}

AES/ECB/PKCS5Padding说明

  • AES:加密算法
  • ECB:模式
  • PKCS5Padding:补码方式
AES/ECB/PKCS7PaddingAES/ECB/PKCS5Padding是两种常见的AES加密模式和填充方式。它们的区别在于填充方式的不同。 PKCS5Padding和PKCS7Padding都是用于填充数据块的,以确保数据块的长度满足加密算法的要求。它们的主要区别在于对于数据块长度不满足加密算法要求时的处理方式。 PKCS5Padding是针对8字节数据块的填充方式,当数据块长度不满8字节时,会使用特定的字节填充数据块,填充的字节值等于需要填充的字节数。例如,如果数据块长度为6字节,则会填充2个字节的值为0x02的字节。 PKCS7Padding是通用的填充方式,可以用于任意长度的数据块。当数据块长度不满足加密算法要求时,会使用特定的字节填充数据块,填充的字节值等于需要填充的字节数。例如,如果数据块长度为6字节,则会填充2个字节的值为0x02的字节。 因此,PKCS5Padding和PKCS7Padding的区别在于对于数据块长度不满足加密算法要求时的处理方式不同。 下面是一个示例代码,演示了AES/ECB/PKCS5PaddingAES/ECB/PKCS7Padding的使用: ```java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; public class AESExample { public static void main(String[] args) throws Exception { String key = "0123456789abcdef"; String plaintext = "Hello World"; // AES/ECB/PKCS5Padding Cipher cipher1 = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKeySpec keySpec1 = new SecretKeySpec(key.getBytes(), "AES"); cipher1.init(Cipher.ENCRYPT_MODE, keySpec1); byte[] encrypted1 = cipher1.doFinal(plaintext.getBytes()); System.out.println("AES/ECB/PKCS5Padding Encrypted: " + new String(encrypted1)); // AES/ECB/PKCS7Padding Cipher cipher2 = Cipher.getInstance("AES/ECB/PKCS7Padding"); SecretKeySpec keySpec2 = new SecretKeySpec(key.getBytes(), "AES"); cipher2.init(Cipher.ENCRYPT_MODE, keySpec2); byte[] encrypted2 = cipher2.doFinal(plaintext.getBytes()); System.out.println("AES/ECB/PKCS7Padding Encrypted: " + new String(encrypted2)); } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值