3
$\begingroup$

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

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

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
  |>]

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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