Search code examples
raes

How to count the bytes in RAW to make 16,24,32 bytes to use with AES encryption


I'm trying to make a R code that do some things to store a sensitive data, until that all ok. So, my problem is the bytes of the cyphertext and key, how to make the RAW of key and cyphertext 16,24 or 32 effiencily ?

library(digest)

key <- charToRaw("keymusbe162432bytes")
aes <- digest::AES(key, mode="ECB")
pass1 <- paste0(pass,"textHereIsNotTheSame")
rawcypher <- charToRaw(pass1)
aes$encrypt(rawcypher)
aes$decrypt(aes$encrypt(raw), raw=FALSE)
Error in digest::AES(key, mode = "ECB") : 
  AES only supports 16, 24 and 32 byte keys

Solution

  • According to ?AES

    key - The key as a 16, 24 or 32 byte raw vector for AES-128, AES-192 or AES-256 respectively.

    That implies if it is not exactly 16 or 24 or 32, will result in error

    AES(as.raw(1:16), mode = "ECB")
    #AES cipher object; mode ECB key size 16 
    
    AES(as.raw(1:17), mode = "ECB")
    

    Error in AES(as.raw(1:17), mode = "ECB") : AES only supports 16, 24 and 32 byte keys

    AES(as.raw(1:24), mode = "ECB")
    #AES cipher object; mode ECB key size 24 
    

    With the OP's code, we could make the characters to 16 or 24

    key <- charToRaw("keymusbe162432bytessssss")
    AES(key, mode = 'ECB')
    #AES cipher object; mode ECB key size 24