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;