Python and Java instances of DES encryption and decryption

  • 2020-06-23 00:56:25
  • OfStack

DES, short for Data Encryption Standard (Data encryption standard), is a common symmetric encryption algorithm. The characteristics of symmetric encryption and asymmetric encryption and their application scenarios are not described in this paper. Readers can use google. This article explains how to use Java and Python to implement DES encryption and decryption.

Here's a recent application scenario. We need to interface with one system, S, which has verified the user's identity, and the new system, N, also needs to verify the user's identity. The authentication method adopted is that the old system S encrypts user ID, and then the new system N decrypts the encrypted user ID, so as to obtain user ID and authenticate.

Since the old system S was implemented with Java, the new system N is implemented with Python. That is, the Python language is required to decrypt ID, the encrypted user of Java DES.

The DES encryption code implemented by Java is posted 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(" Encrypted before: " + content);
  byte[] encrypted = DES_CBC_Encrypt(content.getBytes(), key.getBytes());
  System.out.println(" After the encryption: " + byteToHexString(encrypted));
  byte[] decrypted = DES_CBC_Decrypt(encrypted, key.getBytes());
  System.out.println(" After decryption: " + 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();
 }
}

DES encryption adopted by Java code adopts CBC mode and PKCS5Padding filling mode. The initialization vector used is the encryption key.

Execute the above Java code, output:


 Encrypted before: zx
 After the encryption: 1DBBD4E9246EBFFA
 After decryption: zx

Python USES pyDes to decrypt Java encrypted strings as shown below.


import binascii
from pyDes import des, CBC, PAD_PKCS5

def des_encrypt(s):
 """
 DES  encryption 
 :param s:  Original string 
 :return:  The encrypted string, 16 Into the system 
 """
 secret_key = '20171117'
 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:  The encrypted string, 16 Into the system 
 :return:  Decrypted string 
 """
 secret_key = '20171117'
 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 de

str_en = des_encrypt('zx')
print(str_en)
str_de = des_descrypt(str_en)
print(str_de)

Python language USES pyDes as the package of DES encryption and decryption processing. When DECRYpting DES, CBC mode is adopted, and PAD_PKCS5 is adopted as the filling mode, and the decryption key is used as the initialization vector. These encryption Settings are 1 for the Java language.

Execute the above Python code and get the following output:


1dbbd4e9246ebffa
zx

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


Related articles: