Encrypt
Details and Options
- Encrypt yields EncryptedObject[…], suitable for decryption with Decrypt.
- Encrypt[key,"string"] yields an EncryptedObject containing the encrypted version of the contents of the string as encoded in UTF-8.
- Encrypt[key,ByteArray[…]] yields an EncryptedObject containing the encrypted version of the raw bytes in the ByteArray object.
- For a general expression, Encrypt[key,expr] yields an EncryptedObject essentially containing an encrypted version of Compress[expr].
- The encryption method is based on the cipher and parameters of the key. See GenerateSymmetricKey and GenerateAsymmetricKeyPair for details.
- In a notebook interface, Encrypt[expr] generates a dialog box; in a textual interface it generates a textual prompt.
- Encrypt has the following option:
-
Method Automatic details of encryption method - With the setting Methodassoc, the association assoc gives details of the encryption method to use.
- The following elements can be given in the association assoc:
-
"Padding" Automatic padding mode "InitializationVector" Automatic initialization vector for block ciphers "BlockMode" "CBC" block chaining mode ("ECB","CBC", "OFB", "CFB", "CTR") - The "Padding" element in the association assoc specifies the padding method to pad incomplete input.
- Encryption with symmetric key supports the following padding methods: Automatic and None.
- Encryption with a public key and decryption with a private key support the following padding methods: "PKCS1", "OAEP", None.
- Encryption with a private key and decryption with a public key support the following padding methods: "PKCS1", None.
- The "PKCS1" padding method is used as a default for asymmetric encryption.
- All ciphers except RC4 and RSA can use the block modes "ECB", "CBC", "CFB", "OFB". "RC4" can only use None.
- Possible settings for "InitializationVector" include:
-
Automatic generate an appropriate initialization vector ByteArray[…] use an explicitly specified initialization vector - With "InitializationVector"->Automatic, Encrypt will generate a new initialization vector whenever it is run. Later, the vector can be obtained from the EncryptedObject that was produced.
- Supported ciphers, together with default initialization vector size, include:
-
"Blowfish" 64 bits "CAST5" 64 bits "DES" 64 bits "IDEA" 64 bits "AES128" 128 bits "AES192" 128 bits "AES256" 128 bits - For RSA cipher, the maximum length of data that can be encrypted is determined by the number of bytes
in the modulus, and the padding mode, according to: -
"PKCS1" < 
"OAEP" < 
None 
Examples
open all close allBasic Examples (3)
Encrypt a message with a password:
Encrypt["my password", "Now is the time..."]Show the raw encrypted data and details about the encryption algorithm used:
InputForm[%]Decrypt with the password to get back the message:
Decrypt["my password", %]key = GenerateSymmetricKey[]Encrypt[key, "Now is the time..."]Decrypt[key, %]Generate public and private keys:
keys = GenerateAsymmetricKeyPair[]Encrypt[keys["PublicKey"], "Now is the time..."]Decrypt using the private key:
Decrypt[keys["PrivateKey"], %]Alternatively, encrypt using the private key:
Encrypt[keys["PrivateKey"], "Now is the time..."]Now decrypt using the public key:
Decrypt[keys["PublicKey"], %]Scope (2)
Encrypt["my password", Graphics[Disk[]]]Decrypt["my password", %]Byte arrays are encrypted literally, making them more easily usable with external programs:
Encrypt["my password", ByteArray[{1, 2, 3, 4, 5}]]Decrypt["my password", %]Options (5)
Method (5)
Encrypt using a specific initialization vector:
key = GenerateSymmetricKey[];
Encrypt[key, "Specified IV example", Method -> <|"InitializationVector" -> ByteArray[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}]|>]With the initialization vector set to Automatic, a random initialization vector will be generated every time Encrypt is called:
key = GenerateSymmetricKey[];Encrypt[key, "Automatic IV example", Method -> <|"InitializationVector" -> Automatic|>]%["InitializationVector"]key = GenerateSymmetricKey[];Encrypt[key, "CTR block mode example", Method -> <|"BlockMode" -> "CTR"|>]Specify a padding scheme to use for RSA encryption:
keys = GenerateAsymmetricKeyPair[];
Encrypt[keys["PublicKey"], "Now is the time...", Method -> <|"Padding" -> "OAEP"|>]Encrypt exactly one block of data by setting "Padding" to None:
key = GenerateSymmetricKey[];Encrypt[key, ByteArray["3072ICGBQhJED8uxEt7OLg=="], Method -> <|"Padding" -> None|>]Applications (2)
Encrypt a message and save the ciphertext to a file:
enc = Encrypt["my password", "Now is the time..."]file = FileNameJoin[{$TemporaryDirectory, "test"}];BinaryWrite[file, Normal[enc["Data"]]];Close[file];Read back the contents of the file as bytes:
BinaryReadList[file]Compare with the original ciphertext:
Normal[enc["Data"]]DeleteFile[file]Write simple cryptographic signing and verification functions:
sign[key_PrivateKey, expr_] := Encrypt[key, Hash[expr, "SHA256"]]verify[key_PublicKey, expr_, signature_EncryptedObject] := Decrypt[key, signature] === Hash[expr, "SHA256"]Generate a pair of public and private keys:
keys = GenerateAsymmetricKeyPair[]expr = Graphics[Disk[]]sig = sign[keys["PrivateKey"], expr]Verify that the signature is authentic:
verify[keys["PublicKey"], expr, sig]Verifying with another expression will fail:
verify[keys["PublicKey"], "another expression", sig]Properties & Relations (2)
The returned encrypted object stores all aspects of the encrypted result:
enc = Encrypt["my password", ByteArray[{1, 2, 3, 4, 5}]]Extract the bytes in the ciphertext:
enc["Data"]//NormalExtract the bytes in the initialization vector used for encryption:
enc["InitializationVector"]//NormalSome block modes are more secure than others:
key = GenerateSymmetricKey[];There is a noticeable repeating pattern when encrypting uniform data using ECB:
ecb = Encrypt[key, ByteArray[ConstantArray[1, 100]], Method -> <|"BlockMode" -> "ECB"|>];ListLinePlot[Normal[ecb["Data"]]]cbc = Encrypt[key, ByteArray[ConstantArray[1, 100]], Method -> <|"BlockMode" -> "CBC"|>];ListLinePlot[Normal[cbc["Data"]]]Possible Issues (5)
Timing (1)
Encrypting using a password requires key derivation, which is intentionally slow:
AbsoluteTiming[Encrypt["password", "test"]]You can avoid this by pre-generating a key:
key = GenerateSymmetricKey["password"]AbsoluteTiming[Encrypt[key, "test"]]Encrypted Data Size (1)
Incompatible Keys (1)
Incompatible Padding Modes (1)
Incompatible Block Modes (1)
RSA is an asymmetric cipher that does not support block modes of operation, so Encrypt will proceed ignoring the setting for "BlockMode":
keys = GenerateAsymmetricKeyPair[];
Encrypt[keys["PublicKey"], "RSA is an asymmetric cipher", Method -> <|"BlockMode" -> "CBC"|>]RC4 is a stream that does not support block modes of operation, so Encrypt will proceed ignoring the setting for "BlockMode":
key = GenerateSymmetricKey[Method -> "RC4"];
Encrypt[key, "RC4 is a stream cipher", Method -> <|"BlockMode" -> "CFB"|>]History
Introduced in 2015 (10.1) | Updated in 2019 (12.0) ▪ 2020 (12.1) ▪ 2021 (12.3) ▪ 2023 (13.3)
Text
Wolfram Research (2015), Encrypt, Wolfram Language function, https://reference.wolfram.com/language/ref/Encrypt.html (updated 2023).
CMS
Wolfram Language. 2015. "Encrypt." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2023. https://reference.wolfram.com/language/ref/Encrypt.html.
APA
Wolfram Language. (2015). Encrypt. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Encrypt.html
BibTeX
@misc{reference.wolfram_2026_encrypt, author="Wolfram Research", title="{Encrypt}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/Encrypt.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_encrypt, organization={Wolfram Research}, title={Encrypt}, year={2023}, url={https://reference.wolfram.com/language/ref/Encrypt.html}, note=[Accessed: 13-June-2026]}