Mathematica has a function that one can use like this

GenerateSymmetricKey[Method -> <|"Cipher" -> "AES128", "BlockMode" -> "CBC"|>]

but it picks the value for Key (16 bytes), and I want to provide that, as it's generated outside of MMA in my case. Is there a way for me to do this?

I was about to try desperate things like

 mykey = GenerateSymmetricKey[...];
 mykey["Key"] = ...;

but that seems like an abstraction violation. Just looking at the interface to this function either makes me think that I'm missing something important, or that they just don't have a very useful design.

Also, when one gets a SymmetricKey, how can I look "inside" it? It is presented as an opaque object, and I'm sure that this is Mathematica 101 (although I couldn't figure it out).

Many thanks! :)

  • 1
    $\begingroup$ You can use InputForm (or FullForm) to "look inside." $\endgroup$
    – Mr.Wizard
    Commented May 24, 2017 at 11:11

1 Answer 1


Hopefully I don't misunderstand your question. We can look at the input form of the SymmetricKey using, appropriately, InputForm:

mykey = GenerateSymmetricKey[Method -> <|"Cipher" -> "AES128", "BlockMode" -> "CBC"|>];

mykey // InputForm
SymmetricKey[<|"Cipher" -> "AES128", 
  "BlockMode" -> "CBC", "Key" -> 
  "InitializationVector" -> None|>]

If you already have a "Key" value to use you could construct this expression directly. For example using a separately generated random key:

rndKey = RandomInteger[255, 16] // ByteArray;

new =
    "Cipher" -> "AES128", 
    "BlockMode" -> "CBC",
    "Key" -> rndKey, 
    "InitializationVector" -> None

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.