JAVA和PHP通用的加解密整理版
欢迎进入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]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.
