AES encryption details
Introduction: Full Name advanced Encryption Standard Advanced Encryption Standard, also known as Rijndael Encryption in cryptography, was published by the National Institute of Standards and Technology (NIST) in 2001 and became an effective Standard in 2002. It is a block Encryption Standard adopted by the FEDERAL government of the United States. This standard is used to replace the original DES, has been widely analyzed and used all over the world, it itself only a key, that is, used to achieve encryption, but also for decryption.
Mode support
CBC, CFB, CTR, CTRGladman, ECB, OFB, etc.
Padding support
ZeroPadding, NoPadding, AnsiX923, Iso10126, Iso97971, Pkcs7, etc.
Basic encryption and decryption parameters
In symmetric and asymmetric encryption algorithms, three parameters are often used: initial vector IV, encryption mode mode and padding mode. Firstly, the meanings and functions of the three parameters are introduced:
Initial vector IV
In cryptography, the initial vector is also known as the initial function, and key combination, as a way to encrypt the data, it is the value of a fixed length, the length of the iv depends on the encryption method, usually with the length of the encryption key or password used quite, generally in use process will be asked him to be a random number or pseudo random number, Only the initial vector generated by random numbers can achieve semantic security, making it difficult for attackers to crack the ciphertext that is consistent with the original text and generated with the same key.
Encryption mode
At present, the popular encryption and digital authentication algorithms are all using block encryption, which means that the encrypted plaintext is divided into fixed size data blocks, and then the cipher algorithm is implemented on them. To obtain the ciphertext. Data blocks usually take the same length as the key. Encryption model developed on the basis of the encryption algorithm, at the same time can also be independent of the encryption algorithm, encryption mode defines how through the repeated use of encryption algorithm will be greater than the size of a data block plaintext into ciphertext, describes the process of each data block encryption, now use more encryption scheme are: CBC, ECB, PBCB, CFB, OFB, CTR.
Padding method padding
Block ciphers can only handle data of a certain length, and the length of messages is usually variable. Therefore, the last piece of data in some modes needs to be filled before encryption. There are several methods of filling, the simplest of which is to fill the blank character at the end of the plaintext so that the length is an integer multiple of the block data. The common filling methods are PKCS7, PKCSS, ZeroPadding, ISO10126, and ANSIX923.
JavaScript encryption
Var CryptoJS = require('crypto-js') function tripleAesEncrypt() {var key = Parse (aesKey), iv = cryptojs.enc.utf8.parse (aesIv), SRCS = cryptojs.enc.utf8.parse (text), // CBC encryption, Encrypt (SRCS, key, {iv: iv, mode: cryptojs.mode. CBC, padding: 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 encryption mode 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!" Var aesKey = "6f726c64f2C2057c" var aesIv = "0123456789ABCDEF" // Offset, 16x var encryptedData = tripleAesEncrypt() var decryptedData = tripleAesDecrypt() console.log(" String: ", encryptedData) console.log(" Decrypted String: ", decryptedData) // dZL7TLJR786VGvuUvqYGoQ== // Decrypted string: I love Python!Copy the code
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! Encrypted_str = rc4_encrypt(secret_key, text) print(' encrypted_string: ', encrypted_str) decrypted_str = rc4_decrypt(secret_key, encrypted_str) print(' decrypted string: ', encrypted_str) B '8tnvu3 /U/veJR2KgyBw=' # ' 'Copy the code
Case analysis
Target sites: aHR0cDovL2p6c2MubW9odXJkLmdvdi5jbi9kYXRhL2NvbXBhbnk =
The analysis revealed that the enterprise data was loaded through Ajax and was encrypted. Based on previous experience, the content before encryption should be JSON data, and it is not clear how the data is encrypted. As a result, generic JSON data can be searched for json.parse to find the object code.
Through the global search, we found that there are three possible codes. Through debugging analysis, we found that app.84b2e728.js is the target file.
To debug suspicious code with breakpoints:
Parse (h(t.ata)) var e = json.parse (h(t.ata)); , directly follow h function, you can see the AES encryption is obvious:
As can be seen from the figure above, variable R is the data before encryption, and variable t is the data after encryption.
function h(t) {
var e = d.a.enc.Hex.parse(t)
, n = d.a.enc.Base64.stringify(e)
, a = d.a.AES.decrypt(n, f, {
iv: m,
mode: d.a.mode.CBC,
padding: d.a.pad.Pkcs7
})
, r = a.toString(d.a.enc.Utf8);
return r.toString()
}
Copy the code
The encryption mode is CBC, the fill mode is PKCS7, and the values of the missing offset m and f can be found above.
f = d.a.enc.Utf8.parse("jo8j9wGw%6HbxfFn")
m = d.a.enc.Utf8.parse("0123456789ABCDEF")
Copy the code
In Python, you need to introduce CryptoJS, just override this function.