How to realize software registration code machine code by Python DES encryption and decryption

Time:2021-6-9

This article mainly introduces how to achieve Python DES encryption and decryption software registration code machine code, the article through the example code is very detailed, for everyone’s study or work has a certain reference learning value, need friends can refer to

principle

  • Determine whether the identification file exists in the path, decrypt and compare if it exists, and enter machine code registration if it does not exist
  • Obtain the system C disk serial number as the identification ID, and add a random number as confusion to generate the final machine code.
  • The machine code is sent to the software developer. After the developer decrypts the machine code, he adds his own identifier and generates a key by encryption, which is sent to the user.
  • User input key, program comparison and save.
  • The next time the user opens the software, restart step ‘1’.

explain

  • Encryption: the serial number is encrypted by DES, and then encoded by Base64.
  • Decryption: the password is decoded by Base64, and then decrypted by DES.
  • Write file: convert binary character to hexadecimal to save.
  • Read file: convert hexadecimal to binary.

code

# coding: utf-8
 
import win32api
import pyDes
from binascii import b2a_hex, a2b_hex
import base64
import os
def getCVolumeSerialNumber():
  CVolumeSerialNumber=win32api.GetVolumeInformation("C:\")[1]
  # print(CVolumeSerialNumber)
  if CVolumeSerialNumber:
    return str(CVolumeSerialNumber)
  else:
    return 0
 
def DesEncrypt(str):
  k = pyDes.des(Des_Key, pyDes.CBC, Des_IV, pad=None, padmode=pyDes.PAD_PKCS5)
  encryptStr = k.encrypt(str)
  string = base64.b64encode(encryptStr)
  # print(string)
  Return string # return to Base64
 
def DesDecrypt(string):
  string = base64.b64decode(string)
  k = pyDes.des(Des_Key, pyDes.CBC, Des_IV, pad=None, padmode=pyDes.PAD_PKCS5)
  decryptStr = k.decrypt(string)
  # print(decryptStr)
  return decryptStr
 
Des_Key = "12345678" # Key
Des_ IV = "12345678" # self determined IV vector
def Register():
  if os.path.isfile('conf.bin'):
    with open('conf.bin', 'rb') as fp:
      key = a2b_hex(fp.read())
      print(key)
    serialnumber = getCVolumeSerialNumber()
    decryptstr = DesDecrypt(key).decode('utf8')
    print(decryptstr)
    if serialnumber in decryptstr:
      if 'Buy' in decryptstr:
        print('>> Buy')
        Print ("> > verification complete")
        return 1
      elif 'Trial' in decryptstr:
        print('>> Trial')
        return 2
  rand = str(random.randrange(1, 1000))
  serialnumber = getCVolumeSerialNumber() + rand
  print(serialnumber)
  encryptstr = DesEncrypt(serialnumber).decode('utf8')
  Print ("> > Serial No.:, encryptstr)
  while True:
    Key = input ("> > verification code:)
    try:
      decryptstr = DesDecrypt(key.encode('utf8')).decode('utf8')
      print(decryptstr)
      if serialnumber in decryptstr:
        if 'Buy' in decryptstr:
          print('>> Buy')
          with open('conf.bin', 'wb') as fp:
            fp.write(b2a_hex(key.encode('utf8')))
            Print ("> > verification complete")
          return 1
        elif 'Trial' in decryptstr:
          print('>> Trial')
          return 2
    except Exception as e:
      print(e)
      Print ("> > input error")
      continue

effect

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.