Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the des encryption and decryption codes compatible with PHP and Java

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is compatible with PHP and Java des encryption and decryption code", in the daily operation, I believe that many people have doubts about what compatible PHP and Java des encryption and decryption code, Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for everyone to answer the question of "what is compatible with PHP and Java des encryption and decryption code". Next, please follow the editor to study!

Php Code:

Java Code:

Package com.test; import it.sauronsoftware.base64.Base64; import java.security.Key;import java.security.SecureRandom;import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvParameterSpec; public class Main {public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding" / * DES algorithm: encrypts * * @ param data string to be encrypted * @ param key encrypts the private key. The length cannot be less than 8-bit * @ return encrypted byte array. Generally, * @ throws CryptException exception * / public static String encode (String key,String data) throws Exception {return encode (key, data.getBytes ()) is used in combination with Base64 encoding. } / * DES algorithm: encrypting * * @ param data string to be encrypted * @ param key encrypts the private key. The length cannot be less than 8-bit * @ return encrypted byte array. Generally, the * @ throws CryptException exception * / public static String encode (String key,byte [] data) throws Exception {try {DESKeySpec dks = new DESKeySpec (key.getBytes ()) is used in combination with Base64 encoding. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ("DES"); / / the length of key cannot be less than 8 bytes Key secretKey = keyFactory.generateSecret (dks); Cipher cipher = Cipher.getInstance (ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec (key.getBytes ()); AlgorithmParameterSpec paramSpec = iv; cipher.init (Cipher.ENCRYPT_MODE, secretKey,paramSpec); byte [] bytes = cipher.doFinal (data); / / return byte2hex (bytes); return new String (Base64.encode (bytes)) } catch (Exception e) {throw new Exception (e);}} / * DES algorithm, decryption * * @ param data string to be decrypted * @ param key decryption private key, length cannot be less than 8-bit * @ return decrypted byte array * @ throws Exception exception * / public static byte [] decode (String key,byte [] data) throws Exception {try {SecureRandom sr = new SecureRandom () DESKeySpec dks = new DESKeySpec (key.getBytes ()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ("DES"); / / the length of key cannot be less than 8 bytes Key secretKey = keyFactory.generateSecret (dks); Cipher cipher = Cipher.getInstance (ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec (key.getBytes ()); AlgorithmParameterSpec paramSpec = iv; cipher.init (Cipher.DECRYPT_MODE, secretKey,paramSpec); return cipher.doFinal (data) } catch (Exception e) {throw new Exception (e);}} / * get the encoded value * @ param key * @ param data * @ return * / public static String decodeValue (String key,String data) {byte [] datas; String value = null; try {datas = decode (key, Base64.decode (data.getBytes () Value = new String (datas);} catch (Exception e) {value = ";} return value;} public static void main (String [] args) throws Exception {System.out.println (" Ming: abcd ") Secret: "+ Main.encode (" asdfwef5 "," abcd ");}}

PS: with regard to encryption technology, this site also provides the following encryption tools for your reference:

MD5 online encryption tool: http://tools.jb51.net/password/CreateMD5Password

Escape encryption / decryption tool: http://tools.jb51.net/password/escapepwd

Online SHA1 encryption tool: http://tools.jb51.net/password/sha1encode

Short chain (short URL) online generation tool: http://tools.jb51.net/password/dwzcreate

Short chain (short URL) online restore tool: http://tools.jb51.net/password/unshorturl

High strength password generator: http://tools.jb51.net/password/CreateStrongPassword

At this point, the study of "what are the des encryption and decryption codes compatible with PHP and Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report