({{, (RefLink[GenerateSymmetricKey, paclet:ref/GenerateSymmetricKey])[] 
randomly generates a (RefLink[SymmetricKey, paclet:ref/SymmetricKey]) object suitable for use with cryptographic functions.}, {, (RefLink[GenerateSymmetricKey, paclet:ref/GenerateSymmetricKey])["password"]
derives a (RefLink[SymmetricKey, paclet:ref/SymmetricKey]) object from the password string given.}, {, (RefLink[GenerateSymmetricKey, paclet:ref/GenerateSymmetricKey])[bytes]
generates a (RefLink[SymmetricKey, paclet:ref/SymmetricKey]) object using the byte array or list of bytes directly as the key.}, {, (RefLink[GenerateSymmetricKey, paclet:ref/GenerateSymmetricKey])[(RefLink[DerivedKey, paclet:ref/DerivedKey])[…]]
generates a symmetric key object with a key given by the (RefLink[DerivedKey, paclet:ref/DerivedKey]) object.}, {, (RefLink[GenerateSymmetricKey, paclet:ref/GenerateSymmetricKey])[opts]
randomly generates a symmetric key using the specified options.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[GenerateSymmetricKey, paclet:ref/GenerateSymmetricKey])[] uses a system-specific, high-entropy randomness source.

(RefLink[GenerateSymmetricKey, paclet:ref/GenerateSymmetricKey]) has a (RefLink[Method, paclet:ref/Method]) option that specifies the cryptography method to use.

The current default setting is "AES256".

With (RefLink[Method, paclet:ref/Method])->"name", default settings for the specified method are used.

Particular settings can be specified using (RefLink[Method, paclet:ref/Method])->assoc, where the entries in the association assoc are:

Supported methods, together with default key size and initialization vector size, include:

The following ciphers can use any key size that is a multiple of 8:

All ciphers except RC4 can use the block modes "ECB", "CBC", "CFB", "OFB". "RC4" can only use (RefLink[None, paclet:ref/None]).

"AES128", "AES192", "AES256" ciphers also support CTR block mode.

Possible settings for "InitializationVector" include:

With "InitializationVector"->(RefLink[None, paclet:ref/None]), (RefLink[Encrypt, paclet:ref/Encrypt]) will generate a new initialization vector whenever it is run. In this case, the vector can be determined only from the (RefLink[EncryptedObject, paclet:ref/EncryptedObject]) that is produced.

(RefLink[GenerateSymmetricKey, paclet:ref/GenerateSymmetricKey])["password"] effectively uses (RefLink[GenerateDerivedKey, paclet:ref/GenerateDerivedKey]) to generate a key.

In (RefLink[GenerateSymmetricKey, paclet:ref/GenerateSymmetricKey])[bytes], bytes can be a (RefLink[ByteArray, paclet:ref/ByteArray]) object or a list of integers between 0 and 255. The length of bytes must match the key size of the cipher used.

OpenersPrimaryExamplesSection(Spacer1)Examples  (7)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Generate a random symmetric key:

GenerateSymmetricKey[]

Use the key to encrypt:

Encrypt[%,"Now is the time..."]

Decrypt[%%,%]

OpenersExampleSection1(Spacer1)Scope  (5)

Derive a symmetric key from a password:

GenerateSymmetricKey["my password"]

GenerateSymmetricKey["foo"]===GenerateSymmetricKey["foo"]

GenerateSymmetricKey["foo"]===GenerateSymmetricKey["bar"]

OpenersExampleSubsection0(Spacer1)Methods  (4)

Generate a symmetric key appropriate for encryption using Blowfish:

GenerateSymmetricKey[Method-><|"Cipher"->"Blowfish"|>]

A simplified syntax:

GenerateSymmetricKey[Method->"Blowfish"]

Generate a symmetric key with a key size of 512 bits:

GenerateSymmetricKey[Method-><|"Cipher"->"Blowfish","KeySize"->512|>]

The key size must be a multiple of 8:

GenerateSymmetricKey[Method-><|"Cipher"->"Blowfish","KeySize"->500|>]

Only some ciphers allow a variable key size:

GenerateSymmetricKey[Method-><|"Cipher"->"AES256","KeySize"->512|>]

When no initialization vector is given, a new one will be generated every time (RefLink[Encrypt, paclet:ref/Encrypt]) is called:

GenerateSymmetricKey[Method-><|"Cipher"->"AES256","InitializationVector"->None|>]

With the initialization vector set to (RefLink[Automatic, paclet:ref/Automatic]), a random initialization vector is generated once:

GenerateSymmetricKey[Method-><|"Cipher"->"AES256","InitializationVector"->Automatic|>]

Generate a symmetric key with a specific initialization vector:

GenerateSymmetricKey[Method-><|"Cipher"->"AES256","InitializationVector"->ByteArray[{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}]|>]

Generate a symmetric key that will use CFB as the block mode:

GenerateSymmetricKey[Method-><|"Cipher"->"AES256","BlockMode"->"CFB"|>]

Not all block modes work with all ciphers:

GenerateSymmetricKey[Method-><|"Cipher"->"RC4","BlockMode"->"CFB"|>]

OpenersExampleSection3(Spacer1)Possible Issues  (1)

Some block modes are more secure than others:

ecb=GenerateSymmetricKey[Method-><|"Cipher"->"AES256","BlockMode"->"ECB"|>]

cbc=GenerateSymmetricKey[Method-><|"Cipher"->"AES256","BlockMode"->"CBC"|>]

There is a noticeable repeating pattern when encrypting uniform data using ECB:

ListLinePlot[Normal[Encrypt[ecb,ByteArray[ConstantArray[1,100]]]["Data"]]]

There is not with CBC:

ListLinePlot[Normal[Encrypt[cbc,ByteArray[ConstantArray[1,100]]]["Data"]]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[SymmetricKey, paclet:ref/SymmetricKey])  ▪  (RefLink[Encrypt, paclet:ref/Encrypt])  ▪  (RefLink[Decrypt, paclet:ref/Decrypt])  ▪  (RefLink[GenerateDerivedKey, paclet:ref/GenerateDerivedKey])  ▪  (RefLink[GenerateAsymmetricKeyPair, paclet:ref/GenerateAsymmetricKeyPair])  ▪  (RefLink[RandomInteger, paclet:ref/RandomInteger])  ▪  (RefLink[FieldMasked, paclet:ref/FieldMasked])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Cryptography, paclet:guide/Cryptography])}, {▪(RefLinkPlain[Discrete Mathematics, paclet:guide/DiscreteMathematics])}, {▪(RefLinkPlain[File Operations, paclet:guide/FileOperations])}})}})