java和php对接通用对称加解密方法
首页->学习资料->编程语言->php教程->php技巧 关键词: 发布时间:2021-03-10 19:12:05 浏览次数:1091

java和php对接,对于数据加解密的方法。


java代码:


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
 
/**
 * @author baihe
 */ public class AES {
 
    /**
 * 加密算法
  *
 * @param sSrc
  * @param sKey
  * @return
  * @throws Exception
 */  public static String Encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.out.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
  if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
  cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
 
        String result = Base64.getEncoder().encodeToString(encrypted);
        return java.net.URLEncoder.encode(result, "UTF-8"); //此处使用BASE64做转码功能,同时能起到2次加密的作用。
  }
 
    // 解密算法
  public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
  if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
  if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            try {
                sSrc = java.net.URLDecoder.decode(sSrc, "UTF-8");
                byte[] encrypted1 = Base64.getDecoder().decode(sSrc);//先用base64解密
  byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "utf-8");
                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 {
        /*
 * 此处使用AES-128-ECB加密模式,key需要为16位。  */  String cKey = "WeDoct_XiangYang";
        // 需要加密的字串
  String cSrc = "{\"doctorIdcard\":\"420682198102024516\",\"doctorBelongTeamid\":\"84\",\"idcard\":\"110101200003070236\",\"mobile\":\"13910043421\",\"name\":\"\\u5927\\u767d\",\"type\":\"1\",\"relationship\":\"10\",\"relationDesc\":\"\\u5b59\\u5973\",\"liveProvinceid\":\"420000000000\",\"liveCityid\":\"420600000000\",\"liveCountyid\":\"420682000000\",\"liveTownshipid\":\"420682104000\",\"liveVillageid\":\"420682104001\",\"liveDetailAddress\":\"\\u8944\\u9633\\u5e02\",\"crowdType\":\"1\",\"isTuberculosis\":\"0\",\"isHypertension\":\"0\",\"isDiabetes\":\"0\",\"isPsychologica\":\"0\",\"isDestitute\":\"0\",\"isDisabilit\":\"0\",\"isPoverty\":\"0\",\"isFlowPeople\":\"1\",\"isFiveInsured\":\"0\",\"isLowInsured\":\"0\",\"app_id\":\"xysignbaokang\",\"app_secret\":\"3RDfblpUvziDOmSk\"}";
 
        System.out.println(cSrc);
        // 加密
  String enString = AES.Encrypt(cSrc, cKey);
        System.out.println("加密后的字串是:" + enString);
 
        // 解密
  String DeString = AES.Decrypt(enString, cKey);
        System.out.println("解密后的字串是:" + DeString);
    }
}
————————————————
版权声明:本文为CSDN博主「《分分合合好,cvcx」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cpongo3/article/details/93625154



php代码:

public static function WeDecrypt($input, $securityKey) {
  $input = urldecode($input); 
  $input = openssl_decrypt($input, 'AES-128-ECB', $securityKey);    
  if (!$input) {
    return false;
   }  
   return $input; 
 }
 
public static function WeDoctorEncrypt($input, $securityKey) {
  
  $inputArr = json_decode($input, true);//转为数组
  if (!is_array($inputArr) || empty($inputArr)) {
    return false;
   }    
   $input = json_encode($inputArr, JSON_UNESCAPED_UNICODE);//转为json字符串   
   //进行Aes加密  
   $data = openssl_encrypt($input, 'AES-128-ECB', $securityKey);
    return urlencode($data); 
 }

https://blog.csdn.net/cpongo3/article/details/93625154


php实现DES加密解密报Call to undefined function mcrypt_create_iv()错解决

https://blog.csdn.net/huaweichenai/article/details/103503092


赞:(0)
踩:(0)
相关文章
phpexcel设置行高及列宽,背景颜色,
单点登录sso原理及php实现方式及de
在php中用curl请求java接口无法获
免费ip地址查询接口
php,java,android, aes加解密-加密
地图上计算两点间的距离
关于百度地图的一些技巧
php中empty,is_null,isset的区别
windows及linux下composer安装教程
一种精妙的数据查询与存储方式
热门文章
win7中将文件拷贝到虚拟机linux下
phpexcel设置行高及列宽,背景颜色,
rabbitmq无法启动
intellij idea不显示git push按钮
php7中使用mongodb的aggregate进行
centos7.4 64位下swoole安装及配置
laravel页面静态化的方法
navicate连接mycat报1184错误
单点登录sso原理及php实现方式及de
devops-jenkins容器为pending状态
好评文章
phpexcel设置行高及列宽,背景颜色,
php7中使用mongodb的aggregate进行
intellij idea打开文件所在文件夹
windows下使用MongoDB Compass Com
win7中将文件拷贝到虚拟机linux下
laravel 中悲观锁 & 乐观锁的使用
单点登录sso原理及php实现方式及de
navicate连接mycat报1184错误
rabbitmq无法启动
laravel整合dingo/api方法步骤:jwt
标签
rabbitmq mysql备份 elasticsearch golang swoole
我的项目
【github】www.github.com/hurong241
【码云】gitee.com/hu_rong/projects
【docker hub】hub.docker.com/repositories/hurong241
【packagist】packagist.org/users/hurong241/packages
站点信息
建站时间:2011年
文章数:607篇
浏览数:945718
粤ICP备18028092号-1  微信:hurong241