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

      

  • 相关阅读:
    SQL Server2005重新安装不上的问题及其解决(转)
    取消2003默认共享
    自写生成实体类工具
    双核886针CPU简明制作教程
    VS 2008 在安装SP1后智能提示变成英文的解决办法
    VS2008 安装盘的问题
    拖放 DataGrid 列摘自MSDN
    VB.net SP1 的兼容性问题
    Windows Server 2008+VS2008
    Combobox 的解决方法
  • 原文地址:https://www.cnblogs.com/gavinjunftd/p/13274347.html
Copyright © 2011-2022 走看看