Working with people to make an android App, I am responsible for the server side of PHP,
Data transmission between the mobile phone and the server is encrypted,
He uses this Java data encryption class for his mobile phone.
PHP version of the data encryption class corresponding to the request.
Package Com.ssh.util;import Java.io.bufferedoutputstream;import Java.io.fileoutputstream;import Java.security.Key; Import Javax.crypto.cipher;import Javax.crypto.keygenerator;import Javax.crypto.secretkey;import Javax.crypto.secretkeyfactory;import Javax.crypto.spec.desedekeyspec;import com.sun.org.apache.xerces.internal.impl.dv.util.base64;/** * Desede Symmetric Cryptographic Algorithm Demo * * @author zolly * */public class DESedeCod ER {/** * key algorithm * */public static final String key_algorithm = "Desede"; /** * Encryption/decryption algorithm/working mode/Fill Method * */public static final String cipher_algorithm = "desede/ecb/pkcs5padding"; /** * * Generate key * * @return byte[] Binary key * */public static byte[] Initkey () throws Exception { Instantiate key generator Keygenerator kg = keygenerator.getinstance (key_algorithm); Initialize key generator Kg.init (168); Generate key Secretkey Secretkey = Kg.generatekey (); Get the binary key encoding form byte[] key = secretkey.getencoded (); BuffereDoutputstream keystream = new Bufferedoutputstream (New FileOutputStream ("DESedeKey.dat")); Keystream.write (key, 0, key.length); Keystream.flush (); Keystream.close (); Return key; }/** * Convert key * * @param key * Binary key * @return Key Key * */public static key ToKe Y (byte[] key) throws Exception {//Instantiate des key desedekeyspec DKs = new Desedekeyspec (key); Instantiate the key factory secretkeyfactory keyfactory = secretkeyfactory. getinstance (Key_algorithm); Generate key Secretkey Secretkey = Keyfactory.generatesecret (DKS); return secretkey; */** * Encrypt data * * @param data * To be encrypted * @param key * key * @return Byt E[] Encrypted Data * */public static byte[] Encrypt (byte[] data, byte[] key) throws Exception {//Restore key key K = Tokey (key); Instantiate Cipher Cipher = Cipher.getinstance (cipher_algoritHM); Initialize, set to cryptographic mode Cipher.init (Cipher.encrypt_mode, k); Execute Operation return cipher.dofinal (data); /** * Decrypt Data * * @param data * To be decrypted * @param key * key * @return Byt E[] Decrypted Data * */public static byte[] Decrypt (byte[] data, byte[] key) throws Exception {//Welcome key key K = Tokey (key); Instantiate Cipher Cipher = Cipher.getinstance (cipher_algorithm); Initialize, set to decryption mode Cipher.init (Cipher.decrypt_mode, k); Execute Operation return cipher.dofinal (data); /** * Encrypt String * @param data * @return */public static string encode (string str,string screatkey) { String result = ""; byte[] data = Desedecoder.encrypt (Str.getbytes (), screatkey.getbytes ()); result = Base64.encode (data); return result; /** * Decrypt String * @param str * @return */public static string decode (String str,string screatkey) { StRing result = ""; try {byte[] data = Base64.decode (str); data = Desedecoder.decrypt (data, screatkey.getbytes ()); result = new String (data); } catch (Exception e) {e.printstacktrace (); } return result; /** * Test for decryption * * @throws Exception */public static void main (string[] args) throws Exception { String key = "2c7ddyby20mmky3391xivikz"; String str = "Hello world~"; System.out.println ("Key:" +key); System.out.println ("Original:" + str); Encrypt String value = Encode (Str,key); SYSTEM.OUT.PRINTLN ("After encryption:" + value); System.out.println ("After decryption:" + decode (Value,key)); }}
Reply content:
Working with people to make an android App, I am responsible for the server side of PHP,
Data transmission between the mobile phone and the server is encrypted,
He uses this Java data encryption class for his mobile phone.
PHP version of the data encryption class corresponding to the request.
Package Com.ssh.util;import Java.io.bufferedoutputstream;import Java.io.fileoutputstream;import Java.security.Key; Import Javax.crypto.cipher;import Javax.crypto.keygenerator;import Javax.crypto.secretkey;import Javax.crypto.secretkeyfactory;import Javax.crypto.spec.desedekeyspec;import com.sun.org.apache.xerces.internal.impl.dv.util.base64;/** * Desede Symmetric Cryptographic Algorithm Demo * * @author zolly * */public class DESedeCod ER {/** * key algorithm * */public static final String key_algorithm = "Desede"; /** * Encryption/decryption algorithm/working mode/Fill Method * */public static final String cipher_algorithm = "desede/ecb/pkcs5padding"; /** * * Generate key * * @return byte[] Binary key * */public static byte[] Initkey () throws Exception { Instantiate key generator Keygenerator kg = keygenerator.getinstance (key_algorithm); Initialize key generator Kg.init (168); Generate key Secretkey Secretkey = Kg.generatekey (); Get the binary key encoding form byte[] key = secretkey.getencoded (); BuffereDoutputstream keystream = new Bufferedoutputstream (New FileOutputStream ("DESedeKey.dat")); Keystream.write (key, 0, key.length); Keystream.flush (); Keystream.close (); Return key; }/** * Convert key * * @param key * Binary key * @return Key Key * */public static key ToKe Y (byte[] key) throws Exception {//Instantiate des key desedekeyspec DKs = new Desedekeyspec (key); Instantiate the key factory secretkeyfactory keyfactory = secretkeyfactory. getinstance (Key_algorithm); Generate key Secretkey Secretkey = Keyfactory.generatesecret (DKS); return secretkey; */** * Encrypt data * * @param data * To be encrypted * @param key * key * @return Byt E[] Encrypted Data * */public static byte[] Encrypt (byte[] data, byte[] key) throws Exception {//Restore key key K = Tokey (key); Instantiate Cipher Cipher = Cipher.getinstance (cipher_algoritHM); Initialize, set to cryptographic mode Cipher.init (Cipher.encrypt_mode, k); Execute Operation return cipher.dofinal (data); /** * Decrypt Data * * @param data * To be decrypted * @param key * key * @return Byt E[] Decrypted Data * */public static byte[] Decrypt (byte[] data, byte[] key) throws Exception {//Welcome key key K = Tokey (key); Instantiate Cipher Cipher = Cipher.getinstance (cipher_algorithm); Initialize, set to decryption mode Cipher.init (Cipher.decrypt_mode, k); Execute Operation return cipher.dofinal (data); /** * Encrypt String * @param data * @return */public static string encode (string str,string screatkey) { String result = ""; byte[] data = Desedecoder.encrypt (Str.getbytes (), screatkey.getbytes ()); result = Base64.encode (data); return result; /** * Decrypt String * @param str * @return */public static string decode (String str,string screatkey) { StRing result = ""; try {byte[] data = Base64.decode (str); data = Desedecoder.decrypt (data, screatkey.getbytes ()); result = new String (data); } catch (Exception e) {e.printstacktrace (); } return result; /** * Test for decryption * * @throws Exception */public static void main (string[] args) throws Exception { String key = "2c7ddyby20mmky3391xivikz"; String str = "Hello world~"; System.out.println ("Key:" +key); System.out.println ("Original:" + str); Encrypt String value = Encode (Str,key); SYSTEM.OUT.PRINTLN ("After encryption:" + value); System.out.println ("After decryption:" + decode (Value,key)); }}
Or rely on their own more reliable ...
/** * Desede Data Encryption Decryption class * * @author thisky@qq.com */class desedecoder {public static function encrypt ($STR, $key) { $TD = Self::gettd ($key); $ret = Base64_encode (Mcrypt_generic ($TD, Self::p kcs5_pad ($STR, 8))); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); return $ret; public static function Decrypt ($STR, $key) {$td = Self::gettd ($key); $ret = self::p kcs5_unpad (Mdecrypt_generic ($TD, Base64_decode ($STR))); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); return $ret; } private static function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize); Return $text. Str_repeat (Chr ($pad), $pad); } private static function Pkcs5_unpad ($text) {$pad = Ord ($text {strlen ($text)-1}); if ($pad > strlen ($text)) {return false; if (strspn ($text, Chr ($pad), strlen ($text)-$pad)! = $pad) {return false; } return substr ($text, 0,-1 * $pad); } private static function Getiv () {return Pack (' H16 ', ' 0102030405060708 '); } private static function Gettd ($key) {$iv = Self::getiv (); $TD = Mcrypt_module_open (Mcrypt_3des, ', MCRYPT_MODE_ECB, '); Mcrypt_generic_init ($TD, $key, $IV); return $TD; }}
AES (Word patch)
I've got the same problem! Ask for help!