Android-Understand JAVA & PHP, and find a PHP version of the JAVA Data Encryption Class.

Source: Internet
Author: User
I work with people to create an AndroidApp. I use PHP on the server, and data transmission between the mobile phone and the server is encrypted. The mobile phone uses the JAVA Data Encryption Class, evaluate the corresponding PHP Data Encryption Class. {Code...} cooperates with people to build an Android App. I am responsible for using PHP on the server side,
Data transmission between the mobile phone and the server is encrypted,
He uses the JAVA Data Encryption Class on his mobile phone,
Evaluate the corresponding PHP Data Encryption Class.

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 encryption algorithm demonstration ** @ author zolly **/public class DES EdeCoder {/*** key algorithm **/public static final String KEY_ALGORITHM = "DESede "; /*** encryption/decryption algorithm/working mode/filling mode **/public static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding "; /***** generate the key ** @ return byte [] binary key **/public static byte [] initkey () throws Exception {// instantiate the key generator KeyGenerator kg = KeyGenerator. getInstance (KEY_ALGORITHM); // initialize the key generator kg. init (168); // generate secret key SecretKey = Kg. generateKey (); // obtain the binary key encoding Format byte [] key = secretKey. getEncoded (); BufferedOutputStream keystream = new BufferedOutputStream (new FileOutputStream ("DESedeKey. dat "); keystream. write (key, 0, key. length); keystream. flush (); keystream. close (); return key;}/*** conversion key ** @ param Key * binary Key * @ return key **/public static Key toKey (byte [] Key) throws Exception {// instantiate Des key DESedeKeySpec dks = New DESedeKeySpec (key); // instantiate the key factory SecretKeyFactory keyFactory = SecretKeyFactory. getInstance (KEY_ALGORITHM); // generate SecretKey secretKey = keyFactory. generateSecret (dks); return secretKey ;} /***** encrypt data ** @ param data * data to be encrypted * @ param key * @ return byte [] 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); // initialization, set to the encryption mode cipher. init (Cipher. ENCRYPT_MODE, k); // execute the return cipher operation. doFinal (data );} /***** decrypt data ** @ param data * data to be decrypted * @ param key * @ return byte [] 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); // initialization, set to the decryption mode cipher. init (Cipher. DECRYPT_MODE, k); // execute the return cipher operation. doFinal (data);}/*** encrypted 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) {St Ring 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 encryption and 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); // encrypted String value = encode (str, key); System. out. println ("encrypted:" + value); System. out. println ("decrypted:" + decode (value, key ));}}

Reply content:

Working with people to build an Android App, I am responsible for using PHP on the server side,
Data transmission between the mobile phone and the server is encrypted,
He uses the JAVA Data Encryption Class on his mobile phone,
Evaluate the corresponding PHP Data Encryption Class.

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 encryption algorithm demonstration ** @ author zolly **/public class DES EdeCoder {/*** key algorithm **/public static final String KEY_ALGORITHM = "DESede "; /*** encryption/decryption algorithm/working mode/filling mode **/public static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding "; /***** generate the key ** @ return byte [] binary key **/public static byte [] initkey () throws Exception {// instantiate the key generator KeyGenerator kg = KeyGenerator. getInstance (KEY_ALGORITHM); // initialize the key generator kg. init (168); // generate secret key SecretKey = Kg. generateKey (); // obtain the binary key encoding Format byte [] key = secretKey. getEncoded (); BufferedOutputStream keystream = new BufferedOutputStream (new FileOutputStream ("DESedeKey. dat "); keystream. write (key, 0, key. length); keystream. flush (); keystream. close (); return key;}/*** conversion key ** @ param Key * binary Key * @ return key **/public static Key toKey (byte [] Key) throws Exception {// instantiate Des key DESedeKeySpec dks = New DESedeKeySpec (key); // instantiate the key factory SecretKeyFactory keyFactory = SecretKeyFactory. getInstance (KEY_ALGORITHM); // generate SecretKey secretKey = keyFactory. generateSecret (dks); return secretKey ;} /***** encrypt data ** @ param data * data to be encrypted * @ param key * @ return byte [] 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); // initialization, set to the encryption mode cipher. init (Cipher. ENCRYPT_MODE, k); // execute the return cipher operation. doFinal (data );} /***** decrypt data ** @ param data * data to be decrypted * @ param key * @ return byte [] 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); // initialization, set to the decryption mode cipher. init (Cipher. DECRYPT_MODE, k); // execute the return cipher operation. doFinal (data);}/*** encrypted 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) {St Ring 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 encryption and 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); // encrypted String value = encode (str, key); System. out. println ("encrypted:" + value); System. out. println ("decrypted:" + decode (value, key ));}}

Still rely on your own more reliable...

/*** DESede data encryption and 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: pkcs5_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:: pkcs5_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 ', '20140901');} 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 patching)

Are you there! I encountered the same problem! Please help!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.