Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 08:57:37 09月30日 Update

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!

Tags: Encryption code tools bytes length encryption and decryption characters strings online learning arrays algorithms coding more URL help generation utility next hexadecimal Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL OPPO Reno Redmi vpn