How do I encrypt special characters or numbers using AES-256?

How do I encrypt special characters or numbers using AES-256?

Manage alerts

Loading saved threads...

Nikhil · External communityPost link
External question — Stack Overflow Stack Exchange Author: Nikhil Original post: https://stackoverflow.com/questions/67739093 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I would like to encrypt a string in Go using AES-256, without any GCM processing, to compare against MQL4. I encounter issues when I try to encrypt special characters or numbers. Should I be pre-processing my plaintext somehow? I am new to Go so any help would be appreciated; my code is below this explanation. If I encrypt the plaintext "This is a secret" and then decrypt the ciphertext (encoded to hex), I get the same result (i.e. "This is a secret"). pt is the variable name of the plaintext in the code below. If I try to encrypt "This is a secret; 1234", the ciphertext has a group of zeroes at the end, and when I decrypt I only get "This is a secret". Similar ciphertext in MQL4 does not have zeroes at the end and decrypts correctly. If I try to encrypt only "1234", I get build errors, stemming from "crypto/aes.(*aesCipherAsm).Encrypt(0xc0000c43c0, 0xc0000ac058, 0x4, 0x4, 0xc0000ac070, 0x4, 0x8) C:/Program Files/Go/src/crypto/aes/cipher_asm.go:60 +0x125" Here is my code: package main import ( "crypto/aes" "encoding/hex" "fmt" ) func main() { // cipher key key := "thisis32bitlongpassphraseimusing" // plaintext pt := "This is a secret" // pt := "This is a secret; 1234" // zeroes in ciphertext // pt := "1234" // doesn't build c := EncryptAES([]byte(key), pt) // plaintext fmt.Println(pt) // ciphertext fmt.Println(c) // decrypt DecryptAES([]byte(key), c) } func EncryptAES(key []byte, plaintext string) string { c, err := aes.NewCipher(key) CheckError(err) out := make([]byte, len(plaintext)) c.Encrypt(out, []byte(plaintext)) return hex.EncodeToString(out) } func DecryptAES(key []byte, ct string) { ciphertext, _ := hex.DecodeString(ct) c, err := aes.NewCipher(key) CheckError(err) pt := make([]byte, len(ciphertext)) c.Decrypt(pt, ciphertext) s := string(pt[:]) fmt.Println("DECRYPTED:", s) } func CheckError(err error) { if err != nil { panic(err) } }
Quote
Report
Rob Napier · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Rob Napier Original post: https://stackoverflow.com/a/67739396 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. You're creating a raw AES encryptor here. AES can only encrypt precisely 16 bytes of plaintext, producing exactly 16 bytes of cipher text. Your first example "This is a secret" is exactly 16 bytes long, so it works as expected. Your second example is too long. Only the first 16 bytes are being encrypted. The third example is too short and you're likely running into uninitialized memory. The specific characters in your text are irrelevant. Encryption is performed on raw bytes, not letters. In order to encrypt larger (or smaller) blocks of text, you need to use a block cipher mode on top of AES. Common modes are GCM, CBC, and CTR, but there are many others. In most cases, when someone says "AES" without any qualifier, they mean AES-CBC. (GCM is becoming much more popular, and it's a great mode, but it's not so popular that it's assumed quite yet.) I don't know anything about MQL4, but I assume you're trying to reimplement CryptEncode ? I don't see any documentation on how they do the encryption. You need to know what mode they use, how they derive their key, how they generate (and possibly encode) their IV, whether they include an HMAC or other auth, and more. You need to know exactly how they implement whatever they mean by "CRYPT_AES256." There is no one, standard answer to this.
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange Author: PaulB Original post: https://stackoverflow.com/a/67739883 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. MQL4 only supports a very specific implementation of AES encryption and unless you use the correct settings in your other code you will not achieve compatibility between the two platforms. Specifically you need to ensure the following are implemented: Padding Mode: Zeros Cipher Mode: ECB (so no IV) KeySize: 256 BlockSize: 128 You also need to remember in MQL4 that encryption/decryption is a two stage process (to AES256 then to BASE64). You can try the online AES encryption/decryption tool to verify your results available here: The online toolbox
Quote
Report

Post Reply

Checking account access…