zoukankan      html  css  js  c++  java
  • PHP的openssl_encrypt方法的Java实现

    <?php
    class OpenSSL3DES
    {
        /*密钥,22个字符*/
        const KEY='09bd821d3e764f44899a9dc6';
        /*向量,8个或10个字符*/
        const IV='2M9tOpWi';
     
        /**
         * 加密
         * @param boolean $status 是否加密
         * @return string 处理过的数据
         * Java语言的实现地址:
         * https://www.cnblogs.com/-ccj/p/10372497.html
         * https://blog.csdn.net/xiojing825/article/details/78491374
         */
        public static function encrypt($data,$status=false){
            if ($status){
                return urlencode(base64_encode(openssl_encrypt($data, 'des-ede3-cbc', self::KEY, OPENSSL_RAW_DATA, self::IV)));
            }
           return $data;
        }
        /**
         * 解密
         * @return string 加密的字符串不是完整的会返回空字符串值
         */
        public static function decrypt($data,$status=false){
            if ($status){
                return openssl_decrypt(base64_decode(urldecode($data)), 'des-ede3-cbc', self::KEY, OPENSSL_RAW_DATA, self::IV);
            }
            return $data;
        }
    }
    /*用户登录JSON参数*/
    $sign = json_encode(array(
                  'username'=>'admin',
                  'password'=>'123456',
                  'time_token'=>time() + (7 * 24 * 60 * 60)
              ));
    $sign_encrypt=OpenSSL3DES::encrypt($sign,true);
    echo $sign_encrypt;  
    echo OpenSSL3DES::decrypt($sign_encrypt,true);
    ?>

    Java对应实现:

    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.net.URLEncoder;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.util.Base64;
    
    /**
     * @author Administrator
     * @create 2019-03-20
     * @desc 安全工具类
     **/
    public class SecurityUtil {
        public static final String KEY = "09bd821d3e764f44899a9dc6";
        public static final String IV = "2M9tOpWi";
        public static final String DEFAULT_ENC_NAME = "UTF-8";
    
        public static String java_openssl_encrypt(String data) {
            return java_openssl_encrypt(data, IV);
        }
    
        /**
         * java_openssl_encrypt加密算法
         *
         * @param data
         * @param iv
         * @return
         * @throws Exception
         */
        public static String java_openssl_encrypt(String data, String iv) {
            try {
                Cipher cipher = createCipher(iv, Cipher.ENCRYPT_MODE);
                return URLEncoder.encode(Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes())), DEFAULT_ENC_NAME);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (InvalidAlgorithmParameterException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static String java_openssl_decrypt(String data) {
            return java_openssl_decrypt(data, IV);
        }
    
        /**
         * java_openssl_decrypt解密
         *
         * @param data
         * @param iv
         * @return
         */
        public static String java_openssl_decrypt(String data, String iv) {
            try {
                Cipher cipher = createCipher(iv, Cipher.DECRYPT_MODE);
                return new String(cipher.doFinal(Base64.getDecoder().decode(URLDecoder.decode(data, DEFAULT_ENC_NAME))));
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (InvalidAlgorithmParameterException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 创建密码器Cipher
         *
         * @param iv
         * @param mode 加/解密模式
         * @return
         * @throws NoSuchAlgorithmException
         * @throws NoSuchPaddingException
         * @throws InvalidKeyException
         * @throws InvalidAlgorithmParameterException
         */
        private static Cipher createCipher(String iv, int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, 
    InvalidKeyException, InvalidAlgorithmParameterException {
            byte[] key = KEY.getBytes();
            Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
            cipher.init(mode, new SecretKeySpec(key, "DESede"), ivParameterSpec);
            return cipher;
        }
    }
  • 相关阅读:
    Xilinx之软件平台 ISE与Vivado前世今生
    博客开园
    第一天:开始你的Jobeet项目
    MySQL之alter语句用法总结
    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
    MySQL中distinct和group by性能比较[转]
    GROUP BY,WHERE,HAVING之间的区别和用法
    split(),preg_split()与explode()函数分析与介
    解析posix与perl标准的正则表达式区别
    sql关键字的解释执行顺序
  • 原文地址:https://www.cnblogs.com/liaojie970/p/10690981.html
Copyright © 2011-2022 走看看