Welcome to the Java Community Forum and interact with 2 million technical staff. Generally, there is no single environment in the daily open middleware. Therefore, Standard AES is usually used for cross-platform communication, DES and other encryption rules during project development, the company encountered the cross-platform problem of JAVA and PHP encryption and decryption.
Welcome to the Java Community Forum and interact with 2 million technical staff> generally, there is no single environment in the open platform. Therefore, Standard AES is usually used for cross-platform communication, DES and other encryption rules during project development, the company encountered the cross-platform problem of JAVA and PHP encryption and decryption.
Welcome to the Java Community Forum and interact with 2 million technical staff> enter
In daily open platforms, there is usually no single environment. Therefore, cross-platform Communication usually uses standard encryption rules such as AES and DES.
During project development, the company encountered the problem of cross-platform encryption and decryption of JAVA and PHP. After finding information from multiple parties and researching it, we found a general basic encryption and decryption solution as follows:
1: JAVA code (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 encryption and decryption, applicable to PHP 3DES encryption and decryption (MCRYPT_3DES algorithm developed in PHP, MCRYPT_MODE_ECB mode, and PKCS7 filling mode)
* @ Author G007N
*/
Public class DesBase64Tool {
Private static SecretKey secretKey = null; // key object
Private static Cipher cipher = null; // private? Encrypted object Cipher
Private static String keyString = "AKlMU89D3FchIkhKyMma6FiE"; // key
Private static Logger log = Logger. getRootLogger ();
Static {
Try {
SecretKey = new SecretKeySpec (keyString. getBytes (), "DESede"); // obtain the key
/* Obtain a private key? The encryption class Cipher, DESede is the algorithm, ECB is the encryption mode, and PKCS5Padding is the filling mode */
Cipher = Cipher. getInstance ("DESede/ECB/PKCS5Padding ");
} Catch (Exception e ){
Log. error (e. getMessage (), e );
}
}
/**
* Encryption
* @ Param message
* @ Return
*/
Public static String desEncrypt (String message ){
String result = ""; // DES encrypted String
String newResult = ""; // The encrypted String after the line break is removed
Try {
Cipher. init (Cipher. ENCRYPT_MODE, secretKey); // sets the working mode to encryption mode and provides the key
Byte [] resultBytes = cipher. doFinal (message. getBytes ("UTF-8"); // perform the encryption
BASE64Encoder enc = new BASE64Encoder ();
Result = enc. encode (resultBytes); // BASE64 encoding
NewResult = filter (result); // remove the line break from the encrypted string
} Catch (Exception e ){
Log. error (e. getMessage (), e );
}
Return newResult;
}
/**
* Decryption
* @ 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 encoding
Cipher. init (Cipher. DECRYPT_MODE, secretKey); // sets the working mode to decryption mode and provides the key
Byte [] resultBytes = cipher. doFinal (messageBytes); // perform the decryption operation
Result = new String (resultBytes, "UTF-8 ");
} Catch (Exception e ){
E. printStackTrace ();
}
Return result;
}
/**
* Remove the encrypted string line break
* @ Param str
* @ Return
*/
Public static String filter (String str ){
String output = "";
StringBuffer sb = new StringBuffer ();
For (int I = 0; I <str. length (); I ++ ){
Int asc = str. charAt (I );
If (asc! = 10 & asc! = 13 ){
Sb. append (str. subSequence (I, I + 1 ));
}
}
Output = new String (sb );
Return output;
}
/**
* Encryption/Decryption Test
* @ Param args
*/
Public static void main (String [] args ){
Try {
String strText = "Hello world! ";
String deseResult = desEncrypt (strText); // Encryption
System. out. println ("encryption Result:" + deseResult );
String desdResult = desDecrypt (deseResult); // decrypt
System. out. println ("decryption Result:" + desdResult );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
[1] [2] [3]