Introduction
This article summarizes various common encryption algorithms and encoding algorithms used in web scraping, detailing their principles and basic implementation methods in JavaScript and Python. When encountering JS encryption, you can quickly restore the encryption process. Some websites may have additional processing during encryption, but the general methods are similar.
Common encryption algorithms:
- Symmetric encryption (same key for encryption and decryption): DES, 3DES, AES, RC4, Rabbit
- Asymmetric encryption (distinguishing between public and private keys): RSA, DSA, ECC
- Message digest/signature algorithms: MD5, SHA, HMAC, PBKDF2
Common encoding algorithm: Base64
JavaScript Encryption and Decryption Modules
Crypto-JS
Crypto-JS supports MD5, SHA, RIPEMD-160, HMAC, PBKDF2, AES, DES, 3DES (Triple DES), Rabbit, RC4, etc., but does not support RSA or ECC. It is a widely used encryption module, installed using the command <span>npm install crypto-js</span>
.
References:
-
Crypto-JS Documentation: https://cryptojs.gitbook.io/docs/
-
Crypto-JS GitHub: https://github.com/brix/crypto-js
Node-RSA
Node-RSA provides support for the RSA algorithm, installed using the command <span>npm install node-rsa</span>
.
References: Node-RSA GitHub: https://github.com/rzcoder/node-rsa
JSEncrypt
References: JSEncrypt provides more comprehensive support for the RSA algorithm, installed using the command <span>npm install jsencrypt</span>
.
- JSEncrypt Documentation: http://travistidwell.com/jsencrypt/
- JSEncrypt GitHub: https://github.com/travist/jsencrypt
Python Encryption and Decryption Libraries
Cryptodome & Crypto
In Python, many algorithms are implemented through the third-party libraries Cryptodome or Crypto. Cryptodome is almost a replacement for Crypto, which has not been updated for many years and has many unknown bugs, so it is not recommended to install Crypto!
Cryptodome supports almost all mainstream encryption algorithms, including MD5, SHA, BLAKE2b, BLAKE2s, HMAC, PBKDF2, AES, DES, 3DES (Triple DES), ECC, RSA, RC4, etc.
Cryptodome is installed using the command <span>pip install pycryptodome</span>
, while Crypto is installed using the command <span>pip install pycrypto</span>
.
References:
-
Crypto Library: https://www.dlitz.net/software/pycrypto/
-
Cryptodome Library: https://www.pycryptodome.org/en/latest/
Hashlib
The standard library hashlib in Python provides common digest algorithms such as MD5, SHA, BLAKE2b, BLAKE2s, etc.
References:
- hashlib Library: https://docs.python.org/3/library/hashlib.html
- Liao Xuefeng hashlib: https://www.liaoxuefeng.com/wiki/1016959663602400/1017686752491744
HMAC
The standard library hmac in Python provides support for the HMAC algorithm.
References:
- hmac Library: https://docs.python.org/3/library/hmac.html
- Liao Xuefeng hmac: https://www.liaoxuefeng.com/wiki/1016959663602400/1183198304823296
pyDes
The third-party library pyDes in Python provides support for the DES algorithm. It can be installed using the command <span>pip install pydes</span>
.
References: pyDes Library: https://github.com/twhiteman/pyDes
ESA
The third-party library rsa in Python provides support for the RSA algorithm. It can be installed using the command <span>pip install rsa</span>
.
References: rsa Library: https://stuvel.eu/python-rsa-doc/
Basic Parameters for Encryption and Decryption
In some symmetric and asymmetric encryption algorithms, the following three parameters are often used: initialization vector (iv), encryption mode (mode), and padding method (padding). Let’s first introduce the meanings and functions of these three parameters:
Initialization Vector (iv)
In cryptography, the initialization vector (iv) is a fixed-length value used in conjunction with the key as a means of encrypting data. The length of the iv depends on the encryption method, usually corresponding to the length of the encryption key or the block size used.
It is generally required to be a random or pseudo-random number during use, as an initialization vector generated from random numbers can achieve semantic security, making it difficult for attackers to crack the ciphertext generated from the same plaintext using the same key.
References: Wikipedia: https://en.wikipedia.org/wiki/Initialization_vector
Encryption Mode (mode)
Currently popular encryption and digital authentication algorithms use block encryption methods, which divide the plaintext to be encrypted into fixed-size data blocks and then execute the cryptographic algorithm on them to obtain ciphertext. The block size is usually the same as the length of the key.
The encryption mode is developed based on the encryption algorithm and can also exist independently of the encryption algorithm. The encryption mode defines how to convert plaintext larger than one block size into ciphertext by reusing the encryption algorithm, describing the process of encrypting each data block. The following are some commonly used encryption modes:
-
ECB: Electronic Code Book (ECB mode) is a basic encryption method where the ciphertext is divided into blocks of equal length (with padding if necessary), and each block is encrypted and output individually to form the ciphertext.
-
CBC: Cipher Block Chaining (CBC mode) is a chaining mode where the ciphertext of the previous block is XORed with the plaintext of the current block before encryption, enhancing the difficulty of cracking.
-
PCBC: Propagating Cipher Block Chaining (PCBC mode), also known as Plaintext Cipher Block Chaining, is a mode that causes small changes in the ciphertext to lead to significant errors in the plaintext during decryption, and has the same property during encryption.
-
CFB: Cipher Feedback (CFB mode) can turn block ciphers into self-synchronizing stream ciphers, similar to CBC, where the decryption process of CFB is almost the reverse of the encryption process of CBC.
-
OFB: Output Feedback (OFB mode) can turn block ciphers into synchronous stream ciphers, generating a key stream block that is XORed with the plaintext block to obtain ciphertext. Like other stream ciphers, flipping one bit in the ciphertext will also flip the corresponding bit in the plaintext.
-
CTR: Counter mode (CTR mode), also known as ICM mode (Integer Counter Mode) and SIC mode (Segmented Integer Counter), uses an incrementing operator that XORs the output of the key encryption with the plaintext to obtain ciphertext, effectively achieving one-time encryption. This method is simple, fast, secure, and reliable, and can be encrypted in parallel, but the key can only be used once if the calculator cannot maintain it for long.
References: Wikipedia: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
Padding Method (padding)
Block ciphers can only process data blocks of a fixed length, while the length of messages is usually variable. Therefore, some modes require padding the last block of data before encryption. There are several padding methods, the simplest of which is to pad the plaintext with null characters to make its length a multiple of the block length. Common padding methods include:
-
PKCS7: When padding, first obtain the number of bytes to be padded = block length – (data length % block length), and fill all bytes in the padding sequence with the required padding byte length value.
-
PKCS5: PKCS5 is a subset algorithm of PKCS7, conceptually similar but with a fixed block size of 8 bytes.
-
ZeroPadding: When padding, first obtain the number of bytes to be padded = block length – (data length % block length), and fill all bytes in the padding sequence with 0.
-
ISO10126: When padding, first obtain the number of bytes to be padded = block length – (data length % block length), fill the last byte in the padding sequence with the required padding byte length value, and fill the remaining bytes with random values.
-
ANSIX923: When padding, first obtain the number of bytes to be padded = block length – (data length % block length), fill the last byte in the padding sequence with the required padding byte length value, and fill the remaining bytes with zeros.
References:
- Wikipedia: https://en.wikipedia.org/wiki/Padding_(cryptography)
- PKCS7/PKCS5 Padding Algorithm: https://segmentfault.com/a/1190000019793040
Base64
Introduction: Base64 is a method of representing arbitrary binary data using 64 characters.
References:
- Base64 Baidu Encyclopedia: https://baike.baidu.com/item/base64/8545775
- Base64 Wikipedia: https://en.wikipedia.org/wiki/Base64
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function base64Encode() {
var srcs = CryptoJS.enc.Utf8.parse(text);
var encodeData = CryptoJS.enc.Base64.stringify(srcs);
return encodeData
}
function base64Decode() {
var srcs = CryptoJS.enc.Base64.parse(encodeData);
var decodeData = srcs.toString(CryptoJS.enc.Utf8);
return decodeData
}
var text = "I love Python!"
var encodeData = base64Encode()
var decodeData = base64Decode()
console.log("Base64 encoded: ", encodeData)
console.log("Base64 decoded: ", decodeData)
// Base64 encoded: SSBsb3ZlIFB5dGhvbiE=
// Base64 decoded: I love Python!
Python Implementation
import base64
def base64_encode(text):
encode_data = base64.b64encode(text.encode())
return encode_data
def base64_decode(encode_data):
decode_data = base64.b64decode(encode_data)
return decode_data
if __name__ == '__main__':
text = 'I love Python!'
encode_data = base64_encode(text)
decode_data = base64_decode(encode_data)
print('Base64 encoded:', encode_data)
print('Base64 decoded:', decode_data)
# Base64 encoded:b'SSBsb3ZlIFB5dGhvbiE='
# Base64 decoded:b'I love Python!'
MD5
Introduction: The full name is MD5 Message-Digest Algorithm, also known as hash algorithm, designed by American cryptographer Ronald Linn Rivest and published in 1992 as RFC 1321 to replace the MD4 algorithm.
Digest algorithms are one-way encryption, meaning that the plaintext cannot be decrypted after being encrypted by the digest algorithm. Another characteristic of digest algorithms is that the ciphertext is of fixed length, converting arbitrary length data into a fixed-length string (usually represented as a hexadecimal string).
It is called a digest algorithm because its algorithm extracts important features of the plaintext. Therefore, two different plaintexts may produce the same ciphertext when using the digest algorithm, although the probability is very low.
References:
- RFC 1321: https://datatracker.ietf.org/doc/rfc1321/
- MD5 Wikipedia: https://en.wikipedia.org/wiki/MD5
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function MD5Test() {
var text = "I love python!"
return CryptoJS.MD5(text).toString()
}
console.log(MD5Test()) // 21169ee3acd4a24e1fcb4322cfd9a2b8
Python Implementation
import hashlib
def md5_test1():
md5 = hashlib.new('md5', 'I love python!'.encode('utf-8'))
print(md5.hexdigest())
def md5_test2():
md5 = hashlib.md5()
md5.update('I love '.encode('utf-8'))
md5.update('python!'.encode('utf-8'))
print(md5.hexdigest())
if __name__ == '__main__':
md5_test1() # 21169ee3acd4a24e1fcb4322cfd9a2b8
md5_test2() # 21169ee3acd4a24e1fcb4322cfd9a2b8
PBKDF2
Introduction: The full name is Password-Based Key Derivation Function 2, PBKDF2 is part of the RSA Laboratories’ public key cryptography standards (PKCS) series, recommended for password hashing in RFC 8018 (PKCS #5 v2.1) published in 2017.
PBKDF2 uses a pseudorandom function (such as HMAC) to take the plaintext and a salt value as input parameters, performing repeated operations to ultimately produce a key. If the number of repetitions is sufficiently large, the cost of cracking becomes very high.
References:
- RFC 8018: https://datatracker.ietf.org/doc/rfc8018/
- PBKDF2 Wikipedia: https://en.wikipedia.org/wiki/PBKDF2
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function pbkdf2Encrypt() {
var text = "I love Python!"
var salt = "43215678"
// key length 128, 10 iterations
var encryptedData = CryptoJS.PBKDF2(text, salt, {keySize: 128/32,iterations: 10});
return encryptedData.toString()
}
console.log(pbkdf2Encrypt()) // 7fee6e8350cfe96314c76aaa6e853a50
Python Implementation
import binascii
from Cryptodome.Hash import SHA1
from Cryptodome.Protocol.KDF import PBKDF2
text = 'I love Python!'
salt = b'43215678'
result = PBKDF2(text, salt, count=10, hmac_hash_module=SHA1)
result = binascii.hexlify(result)
print(result)
# b'7fee6e8350cfe96314c76aaa6e853a50'
SHA
Introduction: The full name is Secure Hash Algorithm, designed by the National Security Agency (NSA) of the United States, primarily used in the Digital Signature Standard (DSS) defined digital signature algorithm (DSA).
SHA usually refers to the five algorithms in the SHA family: SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, with the latter four sometimes referred to as SHA-2. SHA is a slightly more secure digest algorithm than MD5, with MD5 producing a 32-bit ciphertext while SHA-1 produces a 40-bit ciphertext. The stronger the version, the longer the ciphertext, at the cost of slower speed.
References:
- RFC 3174: https://datatracker.ietf.org/doc/rfc3174/
- SHA Wikipedia: https://en.wikipedia.org/wiki/Secure_Hash_Algorithms
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function SHA1Encrypt() {
var text = "I love python!"
return CryptoJS.SHA1(text).toString();
}
console.log(SHA1Encrypt()) // 23c02b203bd2e2ca19da911f1d270a06d86719fb
Python Implementation
import hashlib
def sha1_test1():
sha1 = hashlib.new('sha1', 'I love python!'.encode('utf-8'))
print(sha1.hexdigest())
def sha1_test2():
sha1 = hashlib.sha1()
sha1.update('I love python!'.encode('utf-8'))
print(sha1.hexdigest())
if __name__ == '__main__':
sha1_test1() # 23c02b203bd2e2ca19da911f1d270a06d86719fb
sha1_test2() # 23c02b203bd2e2ca19da911f1d270a06d86719fb
HMAC
Introduction: The full name is Hash-based Message Authentication Code, a keyed-hash message authentication code proposed in 1996 and published as RFC 2104 in 1997.
The HMAC encryption algorithm is a secure message authentication protocol based on a cryptographic hash function and a shared key. It requires both parties to share a key, agree on an algorithm, and perform a hash operation on the message to form a fixed-length authentication code. Both parties determine the legitimacy of the message through the verification of the authentication code.
References:
- RFC 2104: https://datatracker.ietf.org/doc/rfc2104/
- HMAC Wikipedia: https://en.wikipedia.org/wiki/HMAC
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function HMACEncrypt() {
var text = "I love python!"
var key = "secret"
return CryptoJS.HmacMD5(text, key).toString();
// return CryptoJS.HmacSHA1(text, key).toString();
// return CryptoJS.HmacSHA256(text, key).toString();
}
console.log(HMACEncrypt())
Python Implementation
import hmac
def hmac_test1():
message = b'I love python!'
key = b'secret'
md5 = hmac.new(key, message, digestmod='MD5')
print(md5.hexdigest())
def hmac_test2():
key = 'secret'.encode('utf8')
sha1 = hmac.new(key, digestmod='sha1')
sha1.update('I love '.encode('utf8'))
sha1.update('Python!'.encode('utf8'))
print(sha1.hexdigest())
if __name__ == '__main__':
hmac_test1() # 9c503a1f852edcc3526ea56976c38edf
hmac_test2() # 2d8449a4292d4bbeed99ce9ea570880d6e19b61a
DES
Introduction: The full name is Data Encryption Standard, which uses the same key for both encryption and decryption, belonging to symmetric encryption algorithms. It was established as a federal information processing standard (FIPS) by the National Bureau of Standards in 1977. DES is a block cipher algorithm that uses a 56-bit key (commonly thought to be 64 bits, but every 8th bit is set as a parity bit, so effectively only 56 bits are valid). Due to the relatively short key length of 56 bits, DES is considered insecure and has largely been replaced by the more advanced AES standard.
- Supported modes: CBC, CFB, CTR, CTRGladman, ECB, OFB, etc.
- Supported padding: ZeroPadding, NoPadding, AnsiX923, Iso10126, Iso97971, Pkcs7, etc.
References:
- RFC 4772: https://datatracker.ietf.org/doc/rfc4772/
- DES Wikipedia: https://en.wikipedia.org/wiki/Data_Encryption_Standard
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function desEncrypt() {
var key = CryptoJS.enc.Utf8.parse(desKey),
iv = CryptoJS.enc.Utf8.parse(desIv),
srcs = CryptoJS.enc.Utf8.parse(text),
// CBC encryption mode, Pkcs7 padding method
encrypted = CryptoJS.DES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
function desDecrypt() {
var key = CryptoJS.enc.Utf8.parse(desKey),
iv = CryptoJS.enc.Utf8.parse(desIv),
srcs = encryptedData,
// CBC encryption mode, Pkcs7 padding method
decrypted = CryptoJS.DES.decrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
var text = "I love Python!" // Object to be encrypted
var desKey = "6f726c64f2c2057" // Key
var desIv = "0123456789ABCDEF" // Initialization vector
var encryptedData = desEncrypt()
var decryptedData = desDecrypt()
console.log("Encrypted string: ", encryptedData)
console.log("Decrypted string: ", decryptedData)
// Encrypted string: +ndbEkWNw2QAfIYQtwC14w==
// Decrypted string: I love Python!
Python Implementation
import binascii
# Encryption mode CBC, padding method PAD_PKCS5
from pyDes import des, CBC, PAD_PKCS5
def des_encrypt(key, text, iv):
k = des(key, CBC, iv, pad=None, padmode=PAD_PKCS5)
en = k.encrypt(text, padmode=PAD_PKCS5)
return binascii.b2a_hex(en)
def des_decrypt(key, text, iv):
k = des(key, CBC, iv, pad=None, padmode=PAD_PKCS5)
de = k.decrypt(binascii.a2b_hex(text), padmode=PAD_PKCS5)
return de
if __name__ == '__main__':
secret_key = '12345678' # Key
text = 'I love Python!' # Object to be encrypted
iv = secret_key # Offset
secret_str = des_encrypt(secret_key, text, iv)
print('Encrypted string:', secret_str)
clear_str = des_decrypt(secret_key, secret_str, iv)
print('Decrypted string:', clear_str)
# Encrypted string:b'302d3abf2421169239f829b38a9545f1'
# Decrypted string:b'I love Python!'
3DES
Introduction: The full name is Triple Data Encryption Standard, a symmetric encryption algorithm. Developed by IBM in the early 1970s, it was adopted as a data encryption standard by the National Bureau of Standards in 1977. It effectively applies the DES encryption algorithm three times to each data block.
Due to the increased computational power of computers, the original DES key length became vulnerable to brute-force attacks; 3DES was designed to provide a relatively simple method to avoid cracking by increasing the key length of DES. Therefore, strictly speaking, 3DES is not a completely new block cipher algorithm.
- Supported modes: CBC, CFB, CTR, CTRGladman, ECB, OFB, etc.
- Supported padding: ZeroPadding, NoPadding, AnsiX923, Iso10126, Iso97971, Pkcs7, etc.
References:
- RFC 1851: https://datatracker.ietf.org/doc/rfc1851/
- 3DES Wikipedia: https://en.wikipedia.org/wiki/Triple_DES
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function tripleDesEncrypt() {
var key = CryptoJS.enc.Utf8.parse(desKey),
iv = CryptoJS.enc.Utf8.parse(desIv),
srcs = CryptoJS.enc.Utf8.parse(text),
// ECB encryption mode, Iso10126 padding method
encrypted = CryptoJS.TripleDES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Iso10126
});
return encrypted.toString();
}
function tripleDesDecrypt() {
var key = CryptoJS.enc.Utf8.parse(desKey),
iv = CryptoJS.enc.Utf8.parse(desIv),
srcs = encryptedData,
// ECB encryption mode, Iso10126 padding method
decrypted = CryptoJS.TripleDES.decrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Iso10126
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
var text = "I love Python!" // Object to be encrypted
var desKey = "6f726c64f2c2057c" // Key
var desIv = "0123456789ABCDEF" // Offset
var encryptedData = tripleDesEncrypt()
var decryptedData = tripleDesDecrypt()
console.log("Encrypted string: ", encryptedData)
console.log("Decrypted string: ", decryptedData)
// Encrypted string: 3J0NX7x6GbewjjhoW2HKqg==
// Decrypted string: I love Python!
Python Implementation
from Cryptodome.Cipher import DES3
from Cryptodome import Random
# Need to pad, str is not a multiple of 16, so pad to a multiple of 16
def add_to_16(value):
while len(value) % 16 != 0:
value += '\0'
return str.encode(value)
def des_encrypt(key, text, iv):
# Encryption mode OFB
cipher_encrypt = DES3.new(add_to_16(key), DES3.MODE_OFB, iv)
encrypted_text = cipher_encrypt.encrypt(text.encode("utf-8"))
return encrypted_text
def des_decrypt(key, text, iv):
# Encryption mode OFB
cipher_decrypt = DES3.new(add_to_16(key), DES3.MODE_OFB, iv)
decrypted_text = cipher_decrypt.decrypt(text)
return decrypted_text
if __name__ == '__main__':
key = '12345678' # Key, 16 bits
text = 'I love Python!' # Object to be encrypted
iv = Random.new().read(DES3.block_size) # DES3.block_size == 8
secret_str = des_encrypt(key, text, iv)
print('Encrypted string:', secret_str)
clear_str = des_decrypt(key, secret_str, iv)
print('Decrypted string:', clear_str)
# Encrypted string:b'\xa5\x8a\xd4R\x99\x16j\xba?vg\xf2\xb6\xa9'
# Decrypted string:b'I love Python!'
AES
Introduction: The full name is Advanced Encryption Standard, also known as Rijndael encryption, published by the National Institute of Standards and Technology (NIST) in 2001 and became effective as a standard in 2002. It is a block encryption standard adopted by the U.S. federal government.
This standard is used to replace the original DES and has been widely analyzed and used worldwide. It has only one key, which is used for both encryption and decryption.
- Supported modes: CBC, CFB, CTR, CTRGladman, ECB, OFB, etc.
- Supported padding: ZeroPadding, NoPadding, AnsiX923, Iso10126, Iso97971, Pkcs7, etc.
References:
- RFC 3268: https://datatracker.ietf.org/doc/rfc3268/
- AES Wikipedia: https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function tripleAesEncrypt() {
var key = CryptoJS.enc.Utf8.parse(aesKey),
iv = CryptoJS.enc.Utf8.parse(aesIv),
srcs = CryptoJS.enc.Utf8.parse(text),
// CBC encryption mode, Pkcs7 padding method
encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
function tripleAesDecrypt() {
var key = CryptoJS.enc.Utf8.parse(aesKey),
iv = CryptoJS.enc.Utf8.parse(aesIv),
srcs = encryptedData,
// CBC encryption mode, Pkcs7 padding method
decrypted = CryptoJS.AES.decrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
var text = "I love Python!" // Object to be encrypted
var aesKey = "6f726c64f2c2057c" // Key, multiple of 16
var aesIv = "0123456789ABCDEF" // Offset, multiple of 16
var encryptedData = tripleAesEncrypt()
var decryptedData = tripleAesDecrypt()
console.log("Encrypted string: ", encryptedData)
console.log("Decrypted string: ", decryptedData)
// Encrypted string: dZL7TLJR786VGvuUvqYGoQ==
// Decrypted string: I love Python!
Python Implementation
import base64
from Cryptodome.Cipher import AES
# Need to pad, str is not a multiple of 16, so pad to a multiple of 16
def add_to_16(value):
while len(value) % 16 != 0:
value += '\0'
return str.encode(value)
# Encryption method
def aes_encrypt(key, t, iv):
aes = AES.new(add_to_16(key), AES.MODE_CBC, add_to_16(iv)) # Initialize the encryptor
encrypt_aes = aes.encrypt(add_to_16(t)) # Perform AES encryption
encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8') # Execute encryption and return bytes
return encrypted_text
# Decryption method
def aes_decrypt(key, t, iv):
aes = AES.new(add_to_16(key), AES.MODE_CBC, add_to_16(iv)) # Initialize the encryptor
base64_decrypted = base64.decodebytes(t.encode(encoding='utf-8')) # First reverse decrypt base64 to bytes
decrypted_text = str(aes.decrypt(base64_decrypted), encoding='utf-8').replace('\0', '') # Execute decryption and return str
return decrypted_text
if __name__ == '__main__':
secret_key = '12345678' # Key
text = 'I love Python!' # Object to be encrypted
iv = secret_key # Initialization vector
encrypted_str = aes_encrypt(secret_key, text, iv)
print('Encrypted string:', encrypted_str)
decrypted_str = aes_decrypt(secret_key, encrypted_str, iv)
print('Decrypted string:', decrypted_str)
# Encrypted string:lAVKvkQh+GtdNpoKf4/mHA==
# Decrypted string:I love Python!
RC4
Introduction: The full name is Rivest Cipher 4, also known as ARC4 or ARCFOUR, is a stream cipher algorithm with a variable key length. It uses the same key for both encryption and decryption, thus also belonging to symmetric encryption algorithms.
RC4 is the encryption algorithm used in Wired Equivalent Privacy (WEP) and was once one of the algorithms that could be used in TLS. The speed of this algorithm can reach about 10 times that of DES encryption, and it has a high level of non-linearity. However, multiple vulnerabilities have been discovered in RC4, making it particularly susceptible to attacks. As an outdated validation and encryption algorithm, RC4 is gradually not recommended for use.
References:
- RFC 7465: https://datatracker.ietf.org/doc/rfc7465/
- RC4 Wikipedia: https://en.wikipedia.org/wiki/RC4
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function RC4Encrypt() {
return CryptoJS.RC4.encrypt(text, key).toString();
}
function RC4Decrypt(){
return CryptoJS.RC4.decrypt(encryptedData, key).toString(CryptoJS.enc.Utf8);
}
var text = "I love Python!"
var key = "6f726c64f2c2057c"
var encryptedData = RC4Encrypt()
var decryptedData = RC4Decrypt()
console.log("Encrypted string: ", encryptedData)
console.log("Decrypted string: ", decryptedData)
// Encrypted string: U2FsdGVkX18hMm9WWdoEQGPolnXzlg9ryArdGNwv
// Decrypted string: I love Python!
Python Implementation
import base64
from Cryptodome.Cipher import ARC4
def rc4_encrypt(key, t):
enc = ARC4.new(key.encode('utf8'))
res = enc.encrypt(t.encode('utf-8'))
res = base64.b64encode(res)
return res
def rc4_decrypt(key, t):
data = base64.b64decode(t)
enc = ARC4.new(key.encode('utf8'))
res = enc.decrypt(data)
return res
if __name__ == "__main__":
secret_key = '12345678' # Key
text = 'I love Python!' # Object to be encrypted
encrypted_str = rc4_encrypt(secret_key, text)
print('Encrypted string:', encrypted_str)
decrypted_str = rc4_decrypt(secret_key, encrypted_str)
print('Decrypted string:', decrypted_str)
# Encrypted string:b'8tNVu3/U/veJR2KgyBw='
# Decrypted string:b'I love Python!'
Rabbit
Introduction: The Rabbit encryption algorithm is a high-performance stream cipher encryption method first proposed in 2003, which creates a key stream from a 128-bit key and a 64-bit initialization vector (iv).
References:
- RFC 4503: https://datatracker.ietf.org/doc/rfc4503/
- Rabbit Wikipedia: https://en.wikipedia.org/wiki/Rabbit_(cipher)
JavaScript Implementation
// Import crypto-js encryption module
var CryptoJS = require('crypto-js')
function rabbitEncrypt() {
return CryptoJS.Rabbit.encrypt(text, key).toString();
}
function rabbitDecrypt() {
return CryptoJS.Rabbit.decrypt(encryptedData, key).toString(CryptoJS.enc.Utf8);
}
var text = "I love Python!"
var key = "6f726c64f2c2057"
var encryptedData = rabbitEncrypt()
var decryptedData = rabbitDecrypt()
console.log("Encrypted string: ", encryptedData)
console.log("Decrypted string: ", decryptedData)
// Encrypted string: U2FsdGVkX1+ZVCHRXlhmG5Xw87YPWMNIBlbukuh8
// Decrypted string: I love Python!
Python Implementation
Currently, there is no third-party library available for direct implementation of the Rabbit algorithm in Python. You can refer to: https://asecuritysite.com/encryption/rabbit2
RSA
Introduction: The full name is Rivest-Shamir-Adleman, proposed in 1977 by Ronald Linn Rivest, Adi Shamir, and Leonard Adleman. The RSA encryption algorithm is an asymmetric encryption algorithm widely used in public key encryption and electronic commerce.
It is generally regarded as one of the best public key schemes available today. RSA is the first algorithm that can be used for both encryption and digital signatures, capable of resisting all known cryptographic attacks to date.
References:
- RSA Wikipedia: https://en.wikipedia.org/wiki/RSA_(cryptosystem)
JavaScript Implementation
// Import node-rsa encryption module
var NodeRSA = require('node-rsa');
function rsaEncrypt() {
pubKey = new NodeRSA(publicKey,'pkcs8-public');
var encryptedData = pubKey.encrypt(text, 'base64');
return encryptedData
}
function rsaDecrypt() {
priKey = new NodeRSA(privatekey,'pkcs8-private');
var decryptedData = priKey.decrypt(encryptedData, 'utf8');
return decryptedData
}
var key = new NodeRSA({b: 512}); // Generate 512-bit key
var publicKey = key.exportKey('pkcs8-public'); // Export public key
var privatekey = key.exportKey('pkcs8-private'); // Export private key
var text = "I love Python!"
var encryptedData = rsaEncrypt()
var decryptedData = rsaDecrypt()
console.log("Public key:\n", publicKey)
console.log("Private key:\n", privatekey)
console.log("Encrypted string: ", encryptedData)
console.log("Decrypted string: ", decryptedData)
/*
Public key:
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOV1BwTJSVce/QjJAro5fXG9WzOpal09
Qtv1yuXKE81vZSNTHxW6dICwPT/kjCfC3bA5Qs6wnYBANuwD6wlAS0UCAwEAAQ==
-----END PUBLIC KEY-----
Private key:
-----BEGIN PRIVATE KEY-----
MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA5XUHBMlJVx79CMkC
ujl9cb1bM6lqXT1C2/XK5coTzW9lI1MfFbp0gLA9P+SMJ8LdsDlCzrCdgEA27APr
CUBLRQIDAQABAkAiXwJbJC+5PioXG80tyhjRZdT4iyMkrl2Kh2oKO9f1iLaBXLya
D0HW82wFh+cUy8GcMl9jse8DE8wd1TdORmHhAiEA/rwmWjXHVgDqcH/fqk8Ufku0
fXvs56h5QDoh1so5vokCIQDmmL3JDW6Y7RuK2qwFbHBZtYPRFRVdn5X1oqU2FOSX
3QIhAOVTjVN5RtNuT6Cn/jvcpZ5tmTe+8TA8w6vGqeAsfn/BAiBvKKIUEQ2HWoU0
YkUaODPQiteIKomqIAvB5S2O7HNlYQIgWMuLUxGZbbcAmIX+YmRXuET97S7OWv+z
WHVfb/rbXtI=
-----END PRIVATE KEY-----
Encrypted string: hHXTF1K3w55Wd6OSjVYtqxceJ5VhlySNUahel9pwKD92Ef7wIT7DYPuJRKiqz5tuHtUqujbmbZBSL0qDE/EA+A==
Decrypted string: I love Python!
*/
Python Implementation
Module: rsa:
import rsa
def rsa_encrypt(pu_key, t):
# Public key encryption
rsa = rsa.encrypt(t.encode("utf-8"), pu_key)
return rsa
def rsa_decrypt(pr_key, t):
# Private key decryption
rsa = rsa.decrypt(t, pr_key).decode("utf-8")
return rsa
if __name__ == "__main__":
public_key, private_key = rsa.newkeys(512) # Generate public and private keys
print('Public key:', public_key)
print('Private key:', private_key)
text = 'I love Python!' # Object to be encrypted
encrypted_str = rsa_encrypt(public_key, text)
print('Encrypted string:', encrypted_str)
decrypted_str = rsa_decrypt(private_key, encrypted_str)
print('Decrypted string:', decrypted_str)
'''
Public key:PublicKey(7636479066127060956100056267701318377455704072072698049978592945665550579944731953431504993757594103617537700972424661030900303472123028864161050235168613, 65537)
Private key:PrivateKey(7636479066127060956100056267701318377455704072072698049978592945665550579944731953431504993757594103617537700972424661030900303472123028864161050235168613, 65537, 3850457767980968449796700480128630632818465005441846698224554128042451115530564586537997896922067523638756079019054611200173122138274839877369624069360253, 4713180694194659323798858305046043997526301456820208338158979730140812744181638767, 1620238976946735819854194349514460863335347861649166352709029254680140139)
Encrypted string:b"\x1aaeps\xa0c}\xb6\xcf\xa3\xb0\xbb\xedA\x7f}\x03\xdc\xd5\x1c\x9b\xdb\xda\xf9q\x80[=\xf5\x91\r\xd0'\x66\xce\x1f\x01\xef\xa5\xdb3\x96\t0qIxF\xbd\x11\xd6\xb25\xc5\xe1pM\xb4M\xc2\xd4\x03\xa6"
Decrypted string:I love Python!
'''
Module Cryptodome:
import base64
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_v1_5
data = "cKK8B2rWwfwWeXhz"
public_key = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM1xhOWaThSMpfxFsjV5YaWOFHt+6RvS+zH2Pa47VVr8PkZYnRaaKKy2MYBuEh7mZfM/R1dUXTgu0gp6VTNeNQkCAwEAAQ=="
rsa_key = RSA.import_key(base64.b64decode(public_key)) # Import the public key
cipher = PKCS1_v1_5.new(rsa_key) # Generate object
cipher_text = base64.b64encode(cipher.encrypt(data.encode(encoding="utf-8")))
print(cipher_text)
– EOF –
Recommended Reading Click the title to jump
1. Major events affecting the web scraping field: Five years of lawsuits end in defeat
2. Easily avoid pitfalls, making gzip bombs ineffective in web scraping
3. A powerful tool that significantly improves web scraping efficiency!
If you find this article helpful, please share it with more people
Recommended to follow “Python Developers” to enhance Python skills
Likes and views are the greatest support❤️