美文网首页
AES(php+java)

AES(php+java)

作者: guijianshi | 来源:发表于2018-08-08 17:38 被阅读0次

    前言

    由于工作关系,经常需要和机构对接接口,遇到各种各样的加密方式,踩了不少的坑,特别是网上php加密的示例代码都是调用mcrypt扩展包函数,而这个扩展在php71开始就废弃了. 这一系列文章将对各种加密做一个总结,示例代码包含php和java两个版本,方便大家对照.

    AES

    AES加密

    java示例

    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.SecureRandom;
    import java.util.Base64;
    
    public class Test
    {
    
        public static void main(String[] args)
        {
            new Test().go();
        }
    
        public void go()
        {
            Base64.Encoder encode = Base64.getEncoder();
            Base64.Decoder decoder = Base64.getDecoder();
    
            // 实例化 aes类
            Aes aes = new Aes("D2E31857FB4C4C9439EDED69F5A85775");
            try {
                // 需要加密字符串
                String context = "hello world!";
                byte[] aesByte = aes.encode(context);
                // 将ase_encode的结果base64_encode处理
                String aesString = encode.encodeToString(aesByte);
                System.out.println(aesString);
    
                //将加密后的字符串先base64_decode处理
                aesByte = decoder.decode(aesString);
                //解密
                String desString = aes.decode(aesByte);
                System.out.println(desString);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        class Aes
        {
            private String key;
    
            public Aes(String key)
            {
                this.key = key;
            }
    
            /**
             * @param context
             * @return byte[]
             * @throws Exception
             */
            public byte[] encode(String context) throws Exception
            {
                KeyGenerator kgen = KeyGenerator.getInstance("AES");
    
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
                secureRandom.setSeed(key.getBytes());
                kgen.init(128, secureRandom);
    
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
                return cipher.doFinal(context.getBytes("utf8"));
            }
    
            public String decode(byte[] encryptBytes) throws Exception
            {
                KeyGenerator kgen = KeyGenerator.getInstance("AES");
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
                secureRandom.setSeed(key.getBytes());
                kgen.init(128, secureRandom);
    
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
                byte[] decryptBytes = cipher.doFinal(encryptBytes);
                return new String(decryptBytes, "UTF-8");
            }
        }
    }
    
    // 输出
    //kp/X5brtF+IKUO5+7aCRDw==
    //hello world!
    
    

    接下来看看php

    <?php
    class Aes
    {
        private $key;
    
        public function __construct($key)
        {
            $this->key = $key;
        }
        public function encode($str)
        {
            $key = $this->getSslKey();
            return openssl_encrypt($str, 'AES-128-ECB', $key, 1);
        }
    
        public function decode($str)
        {
            $key = $this->getSslKey();
            return openssl_decrypt($t, 'AES-128-ECB', $key, 1);
        }
    
        public function getSslKey()
        {
            $key = substr(openssl_digest(openssl_digest($this->key, 'sha1', true), 'sha1', true), 0, 16);
            return $key;
        }
    }
    
    $str = "hello world!";
    $aes = new Aes('D2E31857FB4C4C9439EDED69F5A85775');
    $encode_str = base64_encode($aes->encode($str));
    echo $encode_str . PHP_EOL;
    echo $aes->encode(base64_decode($encode_str)) . PHP_EOL;
    
    // 输出
    //kp/X5brtF+IKUO5+7aCRDw==
    //hello world!
    
    

    结语

    未完待续

    相关文章

      网友评论

          本文标题:AES(php+java)

          本文链接:https://www.haomeiwen.com/subject/fvexbftx.html