zoukankan      html  css  js  c++  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);
    

      

     附 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中文"));
    

      

  • 相关阅读:
    HDOJ 2871 Memory Control(线段树区间合并与查询)
    POJ 3468 A Simple Problem with Integers(线段树成段更新)
    POJ 2923 Relocation(状态压缩 + 两次DP)
    POJ 1436 Horizontally Visible Segments(线段树区间染色查询)
    POJ 2528 Mayor's posters(离散化的线段树)
    HDOJ 3308 LCIS(线段树区间合并与查询)
    异常处理的指导原则
    CSC命令
    .NET命名空间举例
    System.DateTime
  • 原文地址:https://www.cnblogs.com/gavinjunftd/p/13274347.html
Copyright © 2011-2022 走看看