Hi all,

I have only recently started working with NIM and I am loving it but I have hit 
a little stumbling block. I am trying to get some simple AES encryption working 
and for the life of me I can't seem to be able to do it. I have no doubt some 
AES experts on here will spot my mistake instantly.

Any chance you could have a go at running this code to see why I can't decrypt 
the input please? The important bits are:

  1. The key is KEYDEFGHIHKLOMNA
  2. The IV is IVCDEFGHIHKLOMNA
  3. The encrypted intput in AES CBC 128 is D9C69ED09EACB9A874FCBEB3DC054522 
which is just my name davidkennedy encrypted on this site: 
devglan.com/online-tools/aes-encryption-decryption



I have added some echo outputs to see what I think should be coming out the 
other side but I never seem to be able to even get the hex version of my name 
visible! I am using byte sequences to store the IV and Key as in my eventual 
code that is what I will need to use. Thanks Here is the basic code:
    
    
    import winim
    import nimcrypto
    import std/base64
    
    
    ## Converts a string to the corresponding byte sequence.
    proc toByteSeq*(str: string): seq[byte] {.inline.} =
                @(str.toOpenArrayByte(0, str.high))
    
    func toString*(bytes: openArray[byte]): string {.inline.} =
        ## Converts a byte sequence to the corresponding string.
        let length = bytes.len
        if length > 0:
            result = newString(length)
            copyMem(result.cstring, bytes[0].unsafeAddr, length)
    
    proc decrypt(encryptedInput: string, key: seq[byte], iv: seq[byte]): 
seq[byte] =
        # some var definitions for decrypting
        
        let cryptedInput = toByteSeq(encryptedInput)
        
        var
            encodedPay = newSeq[byte](len(cryptedInput))
            decodedPay = newSeq[byte](len(cryptedInput))
        
        encodedPay = cryptedInput
        
        # Decrypt the encrypted bytes of the main payload
        var test: CBC[aes128]
        test.init(key, iv)
        test.decrypt(encodedPay, decodedPay)
        test.clear()
        
        echo "IV              : ", toHex(iv)
        echo "PLAIN TEXT HEX  : ", toHex(encodedPay)
        #echo "PLAIN TEXT: ", plaintext
        echo "DECODED TEXT HEX: ", toHex(decodedPay)
        echo "SHOULD BE HEX   : 64617669646b656e6e656479" #Decrypted AES CBC128 
of davidkennedy
        echo "DECODED TEXT B64: ", encode(decodedPay)
        echo "SHOULD BE B64   : ZGF2aWRrZW5uZWR5"
        
        #echo "DECODED TEXT: ", decText
        echo "String :", decodedPay.toString() # convert tranformed text to 
string
    
    when isMainModule:
            
            let key: seq[byte] = toByteSeq("KEYDEFGHIHKLOMNA")
            let iv:  seq[byte] = toByteSeq("IVCDEFGHIHKLOMNA")
            
            var encryptedInput = "D9C69ED09EACB9A874FCBEB3DC054522" #AES CBC128 
encryption of davidkennedy in HEX
            var code = decrypt(encryptedInput,key,iv)
    
    
    
    Run

Reply via email to