# Can I make a SymmetricKey with my own key value?

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! :)

• You can use InputForm (or FullForm) to "look inside." May 24, 2017 at 11:11

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" ->
ByteArray["ZECnBd3OGoeAXK2Dc/KZSA=="],
"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:

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

new =
SymmetricKey[<|
"Cipher" -> "AES128",
"BlockMode" -> "CBC",
"Key" -> rndKey,
"InitializationVector" -> None
|>]