-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AES encrypt and decrypt #9
Comments
@gogolxdong you can't reuse one import nimcrypto/rijndael
import nimcrypto/bcmode
import base64, strutils
var cbc : CBC[aes128]
var key:string = "1234123412ABCDEF"
var iv: string = "ABCDEF1234123412"
var message: cstring="hello"
var decrypted:cstring = cast[cstring](alloc0(100))
var encrypted:cstring = cast[cstring](alloc0(100))
cbc.init(key.toOpenArrayByte(0, key.len-1), iv.toOpenArrayByte(0, iv.len-1))
cbc.encrypt(cast[ptr byte]( message), cast[ptr byte]( encrypted), 100)
echo encrypted
## Initialization of context one more time
cbc.init(key.toOpenArrayByte(0, key.len-1), iv.toOpenArrayByte(0, iv.len-1))
echo cbc.decrypt(cast[ptr byte](encrypted), cast[ptr byte]( decrypted), 100)
echo decrypted
dealloc decrypted
dealloc encrypted
## Do not forget to clear `CBC[aes128]` context
cbc.clear() |
import nimcrypto/rijndael
import nimcrypto/bcmode
import base64, strutils
var cbc : CBC[aes128]
var key:string = "1234123412ABCDEF"
var iv: string = "ABCDEF1234123412"
var message: cstring="106593A41D2C0E8453AC2043D3E1860FA30984A594063B002349FBF72B602727"
var decrypted:cstring = cast[cstring](alloc0(100))
cbc.init(key.toOpenArrayByte(0, key.len-1), iv.toOpenArrayByte(0, iv.len-1))
echo cbc.decrypt(cast[ptr byte](message), cast[ptr byte]( decrypted), 100)
echo decrypted
dealloc decrypted
cbc.clear() cannot restore the encrypted message from js , using CBC and PkcsPadding7 |
@gogolxdong could you please show original message you are trying to decrypt in hexadecimal format. |
message is 1022218369@qq.com. Isn't the message in hexadecimal format? I though it was. |
@gogolxdong your |
what does message aligned to 16 bytes mean? Will you show me? |
AES encoding processing data via blocks of 128 bits (16 bytes). So you can't actually encode/decode safely 1 byte sequence or 15 bytes sequence, you need to have at least 16 bytes sequence. Its why PKCS#7 padding is used to pad data to 16 bytes. So |
what's the padding mode nimcrypto using? |
@gogolxdong there no padding schemes in |
Got it , but I'm afraid I'm not competent enough to write such a padding implementation. You should make some notes or write example codes to remind user these gotchas. |
@gogolxdong any luck with those padding examples ? |
decrypt from encrypted message doesn't restore.
The text was updated successfully, but these errors were encountered: