搜索
首页php教程php手册JAVA和PHP通用的加解密整理版

JAVA和PHP通用的加解密整理版

Jun 06, 2016 pm 07:52 PM
javaphp整理实际的解密进入

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入 日常开放中 平台中通常不会只有单一的环境,因此跨平台的通讯 通常会使用标准的AES,DES等加密规则 公司的项目开发中 遇到了JAVA和PHP的加密解密跨平台的问题 经过多方查找资料以及研究找出一个通用的

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  日常开放中 平台中通常不会只有单一的环境,因此跨平台的通讯 通常会使用标准的AES,DES等加密规则

  公司的项目开发中 遇到了JAVA和PHP的加密解密跨平台的问题 经过多方查找资料以及研究找出一个通用的基础加解密方案如下

  1:JAVA代码 (3DES版)

  import javax.crypto.Cipher;

  import javax.crypto.SecretKey;

  import javax.crypto.spec.SecretKeySpec;

  import org.apache.log4j.Logger;

  import sun.misc.BASE64Decoder;

  import sun.misc.BASE64Encoder;

  /**

  * Java版3DES加密解密,适用于PHP版3DES加密解密(PHP语言开发的MCRYPT_3DES算法、MCRYPT_MODE_ECB模式、PKCS7填充方式)

  * @author G007N

  */

  public class DesBase64Tool {

  private static SecretKey secretKey = null;//key对象

  private static Cipher cipher = null;   //私?加密对象Cipher

  private static String keyString = "AKlMU89D3FchIkhKyMma6FiE";//密钥

  private static Logger log = Logger.getRootLogger();

  static{

  try {

  secretKey = new SecretKeySpec(keyString.getBytes(), "DESede");//获得密钥

  /*获得一个私?加密类Cipher,DESede是算法,ECB是加密模式,PKCS5Padding是填充方式*/

  cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");

  } catch (Exception e) {

  log.error(e.getMessage(), e);

  }

  }

  /**

  * 加密

  * @param message

  * @return

  */

  public static String desEncrypt(String message) {

  String result = "";   //DES加密字符串

  String newResult = "";//去掉换行符后的加密字符串

  try {

  cipher.init(Cipher.ENCRYPT_MODE, secretKey);     //设置工作模式为加密模式,给出密钥

  byte[] resultBytes = cipher.doFinal(message.getBytes("UTF-8")); //正式执行加密操作

  BASE64Encoder enc = new BASE64Encoder();

  result = enc.encode(resultBytes);//进行BASE64编码

  newResult = filter(result);      //去掉加密串中的换行符

  } catch (Exception e) {

  log.error(e.getMessage(), e);

  }

  return newResult;

  }

  /**

  * 解密

  * @param message

  * @return

  * @throws Exception

  */

  public static String desDecrypt(String message) throws Exception {

  String result = "";

  try {

  BASE64Decoder dec = new BASE64Decoder();

  byte[] messageBytes = dec.decodeBuffer(message);  //进行BASE64编码

  cipher.init(Cipher.DECRYPT_MODE, secretKey);      //设置工作模式为解密模式,给出密钥

  byte[] resultBytes = cipher.doFinal(messageBytes);//正式执行解密操作

  result = new String(resultBytes,"UTF-8");

  } catch (Exception e) {

  e.printStackTrace();

  }

  return result;

  }

  /**

  * 去掉加密字符串换行符

  * @param str

  * @return

  */

  public static String filter(String str) {

  String output = "";

  StringBuffer sb = new StringBuffer();

  for (int i = 0; i

  int asc = str.charAt(i);

  if (asc != 10 && asc != 13) {

  sb.append(str.subSequence(i, i+1));

  }

  }

  output = new String(sb);

  return output;

  }

  /**

  * 加密解密测试

  * @param args

  */

  public static void main(String[] args) {

  try {

  String strText = "Hello world!";

  String deseResult = desEncrypt(strText);//加密

  System.out.println("加密结果:"+deseResult);

  String desdResult = desDecrypt(deseResult);//解密

  System.out.println("解密结果:"+desdResult);

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

[1] [2] [3] 

JAVA和PHP通用的加解密整理版

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具