This article is a mirror article of machine translation, please click here to jump to the original article.

View: 27517|Reply: 1

[Source] .net/c# CryptoJS aes encrypted source code

[Copy link]
Posted on 10/7/2017 5:39:00 PM | | | |
Recently, I did a post simulation login function, the login password is encrypted by CryptoJS aes, and then I want to write the corresponding algorithm in C#
Introduction to AES encryption algorithm:

Advanced Encryption Standard (abbreviation: AES), also known as Rijndael encryption in cryptography, is a block encryption standard adopted by the US federal government. This standard is used to replace the original DES and has been analyzed and widely used around the world. After a five-year selection process, the Advanced Cryptographic Standard was published by the National Institute of Standards and Technology (NIST) in FIPS PUB 197 on November 26, 2001, and became a valid standard on May 26, 2002. In 2006, the Advanced Encryption Standard became one of the most popular algorithms for symmetric key cryptography.

The js algorithm is as follows:



I searched for online AES encryption websites on the Internet, and the results are different from my own encryption, AES seems to have many modes, and I have seen other people's introductions from the Internet


Start by preparing a plaintext and key:
var plaintText = 'aaaaaaaaaaaaaaaa'; Plain text
var keyStr = 'bbbbbbbbbbbbbbbb'; Generally, the key is a string  
According to the official website documentation, the AES method supports AES-128, AES-192 and AES-256, and the encryption method used in the encryption process depends on the type of incoming key, otherwise it will be encrypted according to AES-256.
CryptoJS supports AES-128, AES-192, and AES-256. It will pick the variant by the size of the key you pass in. If you use a passphrase, then it will generate a 256-bit key.
Since Java is given according to 128bit, but since it is a string, it needs to be converted to 128bit on the frontend.
At first, I thought that using CryptoJS.enc.Hex.parse would correctly convert it to a 128-bit key. But otherwise...
After many attempts, you need to use the CryptoJS.enc.Utf8.parse method to convert the key to 128-bit. Well, since it is said that it is multiple attempts, then the reason is not known, and it will be studied more deeply later.
Before using the key of the string type, you need to parse with uft8 before you can use it
var key = CryptoJS.enc.Utf8.parse(keyStr);  
Since the backend uses PKCS5Padding, but when using CryptoJS, it was found that there was no offset at all.
Encryption
var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {  
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});
Since the ciphertext generated by CryptoJS is an object, if you convert it directly to a string is a Base64 encoded, the attribute on encryptedData.ciphertext is converted to a string is the format required by the backend.
var encryptedBase64Str = encryptedData.toString();
Output: 'RJcecVhTqCHHnlibzTypzuDvG8kjWC+ot8JuxWVdLgY='
console.log(encryptedBase64Str);
You need to read ciphertext.toString() on encryptedData to get the same ciphertext as Java
var encryptedStr = encryptedData.ciphertext.toString();  
Output: '44971e715853a821c79e589bcd3ca9cee0ef1bc923582fa8b7c26ec5655d2e06'
console.log(encryptedStr);  
Since the encrypted ciphertext is a 128-bit string, it needs to be converted to Base64 encoded format when decrypting.
Then you need to use the method CryptoJS.enc.Hex.parse to convert it to hexadecimal, and then use CryptoJS.enc.Base64.stringify to convert it into a Base64 encoded string, and then you can pass it to the CryptoJS.AES.decrypt method to decrypt it.
To get the ciphertext of the string type, you need to parse it with the Hex method first
var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedStr);
Convert ciphertext to Base64 strings
Only Base64 type string ciphertext can decrypt it
var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);  
The string converted to Base64 encodement can be passed into the CryptoJS.AES.decrypt method for decryption operation.
Decryption
var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {  
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});
After CryptoJS decryption, it is still an object, and turning it into plaintext needs to be converted to a string in Utf8 format.
After decryption, the plaintext string needs to be transposed in the manner of Utf8
var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);  
console.log(decryptedStr); // 'aaaaaaaaaaaaaaaa'




Here is the source code for the .NET/C# implementation of AES:







Previous:[12306] Sorry, because you operate the passenger too often, please try again tomorrow
Next:C# implements process memory mapping file share memory
Posted on 5/29/2018 12:27:20 AM |
Refer to the advanced source code of the big guy
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com