Python and Java for DES encryption and decryption instances, pythondes

Source: Internet
Author: User
Tags asymmetric encryption

Python and Java for DES encryption and decryption instances, pythondes

DES is the abbreviation of Data Encryption Standard (Data Encryption Standard). It is a common symmetric Encryption algorithm. This article does not describe the features and application scenarios of symmetric encryption and asymmetric encryption. You can google it on your own. This article describes how to use Java and Python to encrypt and decrypt DES.

This is the most recent application scenario. We need to connect to a system, system S has verified the user's identity, and the new system N also needs to verify the user's identity. The old system S encrypts the user ID, and the new system N decrypts the encrypted user ID to obtain the user ID and perform authentication.

Because the old system S is implemented in Java, the new system N is implemented in Python. That is to say, you must use the Python language to decrypt the user ID encrypted by Java DES.

The DES encryption code implemented in Java is provided here.

Import javax. crypto. spec. ivParameterSpec; import javax. crypto. cipher; import javax. crypto. secretKey; import javax. crypto. secretKeyFactory; import javax. crypto. spec. DESKeySpec; public class Main {public static void main (String [] args) {String content = "zx"; String key = "20171117"; System. out. println ("before encryption:" + content); byte [] encrypted = DES_CBC_Encrypt (content. getBytes (), key. getBytes (); System. out. println ("encrypted:" + byteToHexString (encrypted); byte [] decrypted = DES_CBC_Decrypt (encrypted, key. getBytes (); System. out. println ("decrypted:" + new String (decrypted);} public static byte [] DES_CBC_Encrypt (byte [] content, byte [] keyBytes) {try {DESKeySpec keySpec = new DESKeySpec (keyBytes); SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DES"); SecretKey key = keyFactory. generateSecret (keySpec); Cipher cipher = Cipher. getInstance ("DES/CBC/PKCS5Padding"); cipher. init (Cipher. ENCRYPT_MODE, key, new IvParameterSpec (keySpec. getKey (); byte [] result = cipher. doFinal (content); return result;} catch (Exception e) {System. out. println ("exception:" + e. toString ();} return null;} private static byte [] DES_CBC_Decrypt (byte [] content, byte [] keyBytes) {try {DESKeySpec keySpec = new DESKeySpec (keyBytes ); secretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DES"); SecretKey key = keyFactory. generateSecret (keySpec); Cipher cipher = Cipher. getInstance ("DES/CBC/PKCS5Padding"); cipher. init (Cipher. DECRYPT_MODE, key, new IvParameterSpec (keyBytes); byte [] result = cipher. doFinal (content); return result;} catch (Exception e) {System. out. println ("exception:" + e. toString ();} return null;} private static String byteToHexString (byte [] bytes) {StringBuffer sb = new StringBuffer (bytes. length); String sTemp; for (int I = 0; I <bytes. length; I ++) {sTemp = Integer. toHexString (0xFF & bytes [I]); if (sTemp. length () <2) sb. append (0); sb. append (sTemp. toUpperCase ();} return sb. toString ();}}

Java code uses the DES encryption adopts the CBC mode, uses the PKCS5Padding mode, and uses the encryption key as the initialization vector.

Run the above Java code and output:

Before encryption: After zx is encrypted: After 1DBBD4E9246EBFFA is decrypted: zx

Python uses pyDes to decrypt Java-encrypted strings.

Import binasciifrom pyDes import des, CBC, PAD_PKCS5def des_encrypt (s): "DES encryption: param s: original string: return: Encrypted string, hexadecimal "secret_key = '000000' iv = secret_key k = des (secret_key, CBC, iv, pad = None, padmode = PAD_PKCS5) en = k. encrypt (s, padmode = PAD_PKCS5) return binascii. b2a_hex (en) def des_descrypt (s): "DES decryption: param s: Encrypted string, hexadecimal: return: the decrypted string "secret_key = '000000' iv = secret_key k = des (secret_key, CBC, iv, pad = None, padmode = PAD_PKCS5) de = k. decrypt (binascii. a2b_hex (s), padmode = PAD_PKCS5) return destr_en = des_encrypt ('zx') print (str_en) str_de = des_descrypt (str_en) print (str_de)

Python uses pyDes as the DES encryption and decryption package. DES uses the CBC mode for decryption, PAD_PKCS5 as the filling mode, and the decryption key as the initialization vector. These encryption settings are consistent with those used in Java.

Run the above Python code to get the following output:

1dbbd4e9246ebffazx

As you can see, the Python language can be decrypted normally in the same way as the string encrypted in the Java language.

The above example of using Python and Java to encrypt and decrypt DES is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer.

Related Article

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.