찾다
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 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.