---
title: "Encrypt"
language: "en"
type: "Symbol"
summary: "Encrypt[password, expr] encrypts expr using the specified password, to give an encrypted object. Encrypt[keyspec, expr] encrypts expr using the cryptographic key specification keyspec. Encrypt[expr] interactively requests a password with which to encrypt expr."
keywords: 
- encipher
- encode
- code
- crypto
- cipher
- ciphertext
- RSA
- AES
- DES
- secret key
- public key
- private key
- encryption
canonical_url: "https://reference.wolfram.com/language/ref/Encrypt.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Cryptography"
    link: "https://reference.wolfram.com/language/guide/Cryptography.en.md"
  - 
    title: "Systems-Related String Operations"
    link: "https://reference.wolfram.com/language/guide/SystemsRelatedStringOperations.en.md"
  - 
    title: "Discrete Mathematics"
    link: "https://reference.wolfram.com/language/guide/DiscreteMathematics.en.md"
  - 
    title: "Cryptographic Number Theory"
    link: "https://reference.wolfram.com/language/guide/CryptographicNumberTheory.en.md"
  - 
    title: "String Manipulation"
    link: "https://reference.wolfram.com/language/guide/StringManipulation.en.md"
  - 
    title: "Binary Data"
    link: "https://reference.wolfram.com/language/guide/BinaryData.en.md"
  - 
    title: "External Operations"
    link: "https://reference.wolfram.com/language/guide/ExternalOperations.en.md"
related_functions: 
  - 
    title: "Decrypt"
    link: "https://reference.wolfram.com/language/ref/Decrypt.en.md"
  - 
    title: "EncryptFile"
    link: "https://reference.wolfram.com/language/ref/EncryptFile.en.md"
  - 
    title: "EncryptedObject"
    link: "https://reference.wolfram.com/language/ref/EncryptedObject.en.md"
  - 
    title: "SymmetricKey"
    link: "https://reference.wolfram.com/language/ref/SymmetricKey.en.md"
  - 
    title: "GenerateSymmetricKey"
    link: "https://reference.wolfram.com/language/ref/GenerateSymmetricKey.en.md"
  - 
    title: "GenerateAsymmetricKeyPair"
    link: "https://reference.wolfram.com/language/ref/GenerateAsymmetricKeyPair.en.md"
  - 
    title: "GenerateDigitalSignature"
    link: "https://reference.wolfram.com/language/ref/GenerateDigitalSignature.en.md"
  - 
    title: "Hash"
    link: "https://reference.wolfram.com/language/ref/Hash.en.md"
  - 
    title: "FieldMasked"
    link: "https://reference.wolfram.com/language/ref/FieldMasked.en.md"
  - 
    title: "CellularAutomaton"
    link: "https://reference.wolfram.com/language/ref/CellularAutomaton.en.md"
  - 
    title: "ShiftRegisterSequence"
    link: "https://reference.wolfram.com/language/ref/ShiftRegisterSequence.en.md"
  - 
    title: "BinarySerialize"
    link: "https://reference.wolfram.com/language/ref/BinarySerialize.en.md"
  - 
    title: "AuthenticationDialog"
    link: "https://reference.wolfram.com/language/ref/AuthenticationDialog.en.md"
---
# Encrypt

Encrypt["password", expr] encrypts expr using the specified password, to give an encrypted object.

Encrypt[keyspec, expr] encrypts expr using the cryptographic key specification keyspec.

Encrypt[expr] interactively requests a password with which to encrypt expr.

## 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`](https://reference.wolfram.com/language/ref/Method.en.md) 	[`Automatic`](https://reference.wolfram.com/language/ref/Automatic.en.md)	details of encryption method

* With the setting ``Method -> assoc``, 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 $b$ in the modulus, and the padding mode, according to:

|         |                                          |
| ------- | ---------------------------------------- |
| "PKCS1" |  <  $b-11$ |
| "OAEP"  | < $b-41$   |
| None    | $b$        |

---

## Examples (19)

### Basic Examples (3)

Encrypt a message with a password:

```wl
In[1]:= Encrypt["my password", "Now is the time..."]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{49, 76, 28, 247, 29, 250, 80, 114, 162, 36, 245, 33, 84, 
     251, 177, 186}], "Data" -> ByteArray[{15, 90, 177, 166, 3, 91, 45, 130, 171, 86, 141, 57, 72, 
     66, 175, 88, 191, 47, 26, 186, 178, 152, 200, 171, 77, 122, 114, 198, 190, 136, 144, 68}], 
  "Padding" -> Automatic, "OriginalForm" -> String]]
```

Show the raw encrypted data and details about the encryption algorithm used:

```wl
In[2]:= InputForm[%]
```

Out[2]//InputForm=
EncryptedObject[<\|"Cipher" -> "AES256", "BlockMode" -> "CBC", "InitializationVector" -> 
   ByteArray["MUwc9x36UHKiJPUhVPuxug=="], "Data" -> ByteArray["D1qxpgNbLYKrVo05SEKvWL8vGrqymMirTXpyxr6IkEQ="], 
  "Padding" -> Automatic, "OriginalForm" -> String\|>]

Decrypt with the password to get back the message:

```wl
In[3]:= Decrypt["my password", %]

Out[3]= "Now is the time..."
```

---

Generate a secret key:

```wl
In[1]:= key = GenerateSymmetricKey[]

Out[1]=
SymmetricKey[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "Key" -> ByteArray[{177, 196, 255, 173, 216, 4, 20, 227, 44, 70, 167, 50, 62, 199, 235, 192, 23, 
     217, 191, 131, 174, 250, 61, 221, 128, 191, 202, 69, 18, 252, 13, 38}], "KeySize" -> 256, 
  "InitializationVector" -> None]]
```

Encrypt using the key:

```wl
In[2]:= Encrypt[key, "Now is the time..."]

Out[2]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{228, 243, 187, 112, 206, 102, 93, 129, 3, 9, 159, 104, 61, 
     27, 222, 71}], "Data" -> ByteArray[{158, 167, 47, 74, 127, 4, 166, 86, 247, 211, 22, 2, 141, 
     229, 80, 78, 5, 251, 78, 224, 45, 84, 131, 248, 44, 223, 159, 33, 127, 115, 229, 56}], 
  "Padding" -> Automatic, "OriginalForm" -> String]]

In[3]:= Decrypt[key, %]

Out[3]= "Now is the time..."
```

---

Generate public and private keys:

```wl
In[1]:= keys = GenerateAsymmetricKeyPair[]

Out[1]=
<|"PrivateKey" -> PrivateKey[Association["Type" -> "RSA", "Cipher" -> "RSA", "Padding" -> "PKCS1", 
  "PublicExponent" -> 65537, "PublicModulus" -> 215035051797082778252202122335300485587068154949044\
90604358042338119412987314640960333415644907843 ... 6, 47, 79, 74, 221, 150, 253, 235, 196, 38, 223, 66, 97, 227, 
     53, 42, 130, 107, 58, 52, 150, 30, 37, 87, 115, 59, 185, 8, 151, 217, 173, 129, 127, 138, 47, 
     98, 193, 201, 164, 43, 175, 47, 245, 14, 207, 196, 138, 63, 178, 100, 233}]]]|>
```

Encrypt using the public key:

```wl
In[2]:= Encrypt[keys["PublicKey"], "Now is the time..."]

Out[2]=
EncryptedObject[Association["Cipher" -> "RSA", 
  "Data" -> ByteArray[{61, 46, 143, 121, 9, 248, 190, 17, 22, 191, 207, 198, 220, 128, 208, 68, 
     140, 25, 138, 203, 153, 200, 252, 49, 190, 126, 60, 254, 190, 126, 175, 96, 79, 247, 213, 178, 
   ... , 105, 122, 79, 112, 
     129, 32, 130, 68, 192, 22, 153, 32, 67, 174, 213, 33, 122, 36, 247, 38, 9, 78, 44, 118, 182, 
     63, 211, 3, 97, 27, 44, 83, 164, 129, 160, 32, 22, 7, 166, 170, 16}], 
  "OriginalForm" -> String, "Padding" -> "PKCS1"]]
```

Decrypt using the private key:

```wl
In[3]:= Decrypt[keys["PrivateKey"], %]

Out[3]= "Now is the time..."
```

Alternatively, encrypt using the private key:

```wl
In[4]:= Encrypt[keys["PrivateKey"], "Now is the time..."]

Out[4]=
EncryptedObject[Association["Cipher" -> "RSA", 
  "Data" -> ByteArray[{99, 14, 232, 41, 58, 171, 86, 25, 54, 19, 142, 61, 200, 22, 220, 183, 247, 
     166, 18, 56, 111, 184, 248, 181, 92, 80, 158, 60, 249, 146, 4, 174, 27, 37, 220, 149, 68, 0, 
   ... 9, 171, 159, 40, 152, 
     184, 168, 2, 52, 62, 84, 92, 6, 204, 226, 60, 210, 37, 0, 150, 168, 234, 61, 74, 184, 158, 66, 
     194, 240, 215, 100, 227, 116, 54, 60, 83, 248, 46, 231, 37, 169}], "OriginalForm" -> String, 
  "Padding" -> "PKCS1"]]
```

Now decrypt using the public key:

```wl
In[5]:= Decrypt[keys["PublicKey"], %]

Out[5]= "Now is the time..."
```

### Scope (2)

Encrypt any expression:

```wl
In[1]:= Encrypt["my password", Graphics[Disk[]]]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{65, 83, 54, 251, 181, 42, 44, 220, 231, 215, 143, 104, 87, 
     84, 209, 136}], "Data" -> ByteArray[{24, 161, 6, 131, 7, 82, 210, 205,  ... 43, 168, 242, 41, 23, 133, 144, 
     182, 137, 117, 43, 169, 236, 162, 255, 215, 243, 121, 30, 225, 75, 70, 115, 131, 106, 16, 28, 
     148, 46, 11, 193, 10, 21, 187, 160, 136, 74, 134}], "Padding" -> Automatic, 
  "OriginalForm" -> Expression]]

In[2]:= Decrypt["my password", %]

Out[2]= [image]
```

---

Byte arrays are encrypted literally, making them more easily usable with external programs:

```wl
In[1]:= Encrypt["my password", ByteArray[{1, 2, 3, 4, 5}]]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{200, 97, 119, 16, 18, 32, 116, 56, 148, 21, 46, 142, 2, 216, 
     54, 214}], "Data" -> ByteArray[{154, 174, 205, 198, 221, 114, 61, 129, 26, 19, 255, 222, 204, 
     172, 69, 95}], "Padding" -> Automatic, "OriginalForm" -> ByteArray]]

In[2]:= Decrypt["my password", %]

Out[2]= ByteArray[{1, 2, 3, 4, 5}]
```

### Options (5)

#### Method (5)

Encrypt using a specific initialization vector:

```wl
In[1]:=
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}]|>]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}], 
  "Data" -> ByteArray[{190, 29, 4, 110, 102, 182, 126, 46, 120, 77, 148, 119, 118, 30, 97, 211, 
     201, 205, 237, 204, 96, 143, 156, 80, 94, 165, 8, 139, 212, 135, 169, 163}], 
  "Padding" -> Automatic, "OriginalForm" -> String]]
```

---

With the initialization vector set to ``Automatic``, a random initialization vector will be generated every time ``Encrypt`` is called:

```wl
In[1]:= key  = GenerateSymmetricKey[];Encrypt[key, "Automatic IV example", Method -> <|"InitializationVector" -> Automatic|>]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{223, 78, 246, 32, 33, 129, 66, 18, 68, 15, 203, 177, 18, 
     222, 206, 46}], "Data" -> ByteArray[{241, 130, 193, 26, 63, 40, 40, 132, 112, 162, 89, 254, 
     143, 115, 51, 184, 7, 197, 128, 130, 56, 33, 151, 25, 195, 186, 128, 207, 63, 104, 194, 48}], 
  "Padding" -> Automatic, "OriginalForm" -> String]]

In[2]:= %["InitializationVector"]

Out[2]= ByteArray[{223, 78, 246, 32, 33, 129, 66, 18, 68, 15, 203, 177, 18, 222, 206, 46}]
```

---

Encrypt using CTR block mode:

```wl
In[1]:= key  = GenerateSymmetricKey[];Encrypt[key, "CTR block mode example", Method -> <|"BlockMode" -> "CTR"|>]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CTR", 
  "InitializationVector" -> ByteArray[{229, 185, 128, 10, 250, 127, 94, 177, 247, 104, 54, 30, 199, 
     25, 59, 117}], "Data" -> ByteArray[{171, 59, 209, 159, 144, 216, 135, 230, 188, 40, 184, 172, 
     125, 89, 217, 160, 59, 23, 225, 92, 140, 45}], "Padding" -> Automatic, 
  "OriginalForm" -> String]]
```

---

Specify a padding scheme to use for RSA encryption:

```wl
In[1]:=
keys = GenerateAsymmetricKeyPair[];
Encrypt[keys["PublicKey"], "Now is the time...", Method -> <|"Padding" -> "OAEP"|>]

Out[1]=
EncryptedObject[Association["Cipher" -> "RSA", 
  "Data" -> ByteArray[{133, 178, 36, 171, 243, 144, 190, 243, 75, 85, 44, 137, 64, 181, 242, 134, 
     175, 198, 39, 252, 124, 231, 188, 49, 57, 185, 202, 146, 142, 56, 163, 215, 238, 13, 118, 211, 
 ... 56, 65, 81, 215, 
     19, 22, 28, 254, 91, 61, 137, 212, 153, 161, 236, 169, 13, 127, 217, 169, 101, 151, 177, 26, 
     174, 145, 155, 38, 51, 27, 133, 233, 85, 250, 153, 35, 93, 43, 154, 168}], 
  "OriginalForm" -> String, "Padding" -> "OAEP"]]
```

---

Encrypt exactly one block of data by setting ``"Padding"`` to ``None`` :

```wl
In[1]:= key  = GenerateSymmetricKey[];Encrypt[key, ByteArray[{223, 78, 246, 32, 33, 129, 66, 18, 68, 15, 203, 177, 18, 222, 206, 46}], Method -> <|"Padding" -> None|>]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{183, 7, 133, 61, 69, 196, 178, 196, 99, 152, 88, 37, 207, 
     233, 171, 240}], "Data" -> ByteArray[{16, 50, 67, 250, 144, 248, 243, 166, 45, 169, 162, 147, 
     106, 16, 250, 171}], "Padding" -> None, "OriginalForm" -> ByteArray]]
```

### Applications (2)

Encrypt a message and save the ciphertext to a file:

```wl
In[1]:= enc = Encrypt["my password", "Now is the time..."]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{183, 88, 252, 124, 52, 88, 209, 184, 22, 86, 55, 201, 79, 
     126, 80, 169}], "Data" -> ByteArray[{29, 194, 202, 122, 65, 169, 70, 249, 12, 240, 140, 219, 
     168, 2, 6, 219, 46, 239, 219, 194, 200, 166, 16, 150, 190, 2, 20, 20, 44, 85, 154, 208}], 
  "Padding" -> Automatic, "OriginalForm" -> String]]

In[2]:= file = FileNameJoin[{$TemporaryDirectory, "test"}];

In[3]:= BinaryWrite[file, Normal[enc["Data"]]];Close[file];
```

Read back the contents of the file as bytes:

```wl
In[4]:= BinaryReadList[file]

Out[4]= {82, 123, 218, 16, 165, 131, 90, 224, 46, 135, 121, 250, 211, 66, 243, 65, 32, 164, 150, 92, 96, 157, 76, 215, 238, 224, 94, 137, 62, 137, 247, 251}
```

Compare with the original ciphertext:

```wl
In[5]:= Normal[enc["Data"]]

Out[5]= {82, 123, 218, 16, 165, 131, 90, 224, 46, 135, 121, 250, 211, 66, 243, 65, 32, 164, 150, 92, 96, 157, 76, 215, 238, 224, 94, 137, 62, 137, 247, 251}
```

Delete the temporary file:

```wl
In[6]:= DeleteFile[file]
```

---

Write simple cryptographic signing and verification functions:

```wl
In[1]:= sign[key_PrivateKey, expr_] := Encrypt[key, Hash[expr, "SHA256"]]

In[2]:= verify[key_PublicKey, expr_, signature_EncryptedObject] := Decrypt[key, signature] === Hash[expr, "SHA256"]
```

Generate a pair of public and private keys:

```wl
In[3]:= keys = GenerateAsymmetricKeyPair[]

Out[3]=
<|"PrivateKey" -> PrivateKey[Association["Type" -> "RSA", "Cipher" -> "RSA", "Padding" -> "PKCS1", 
  "PublicExponent" -> 65537, "PublicModulus" -> 293428276514383866944852323306043081043522759719933\
54159902010399138078243379414164093861380394492 ...  42, 108, 41, 10, 104, 234, 19, 113, 110, 143, 
     182, 86, 243, 149, 142, 198, 202, 121, 178, 169, 212, 193, 90, 210, 212, 173, 118, 164, 81, 
     34, 129, 218, 11, 3, 31, 72, 245, 34, 212, 27, 34, 123, 247, 127, 222, 30, 10, 110, 27, 25}]]]|>
```

Define an expression to sign:

```wl
In[4]:= expr = Graphics[Disk[]]

Out[4]= [image]
```

Generate a signature:

```wl
In[5]:= sig = sign[keys["PrivateKey"], expr]

Out[5]=
EncryptedObject[Association["Data" -> ByteArray[{28, 7, 249, 98, 137, 12, 191, 239, 78, 80, 110, 
     169, 228, 213, 84, 66, 44, 168, 150, 78, 215, 68, 67, 53, 94, 184, 74, 31, 161, 169, 67, 187, 
     168, 201, 164, 137, 52, 2, 176, 11, 214, 95,  ...    108, 181, 51, 144, 70, 173, 241, 62, 79, 132, 130, 55, 11, 237, 49, 31, 252, 232, 163, 47, 
     253, 3, 149, 126, 130, 19, 177, 136, 138, 43, 3, 61, 214, 184, 45, 119, 242, 130, 9, 210}], 
  "OriginalForm" -> Expression, "Padding" -> "PKCS1"]]
```

Verify that the signature is authentic:

```wl
In[6]:= verify[keys["PublicKey"], expr, sig]

Out[6]= True
```

Verifying with another expression will fail:

```wl
In[7]:= verify[keys["PublicKey"], "another expression", sig]

Out[7]= False
```

### Properties & Relations (2)

The returned encrypted object stores all aspects of the encrypted result:

```wl
In[1]:= enc = Encrypt["my password", ByteArray[{1, 2, 3, 4, 5}]]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{94, 37, 187, 116, 240, 194, 218, 23, 28, 254, 180, 12, 76, 
     155, 138, 211}], "Data" -> ByteArray[{177, 5, 216, 46, 33, 59, 85, 84, 93, 103, 85, 241, 223, 
     93, 50, 2}], "Padding" -> Automatic, "OriginalForm" -> ByteArray]]
```

Extract the bytes in the ciphertext:

```wl
In[2]:= enc["Data"]//Normal

Out[2]= {177, 5, 216, 46, 33, 59, 85, 84, 93, 103, 85, 241, 223, 93, 50, 2}
```

Extract the bytes in the initialization vector used for encryption:

```wl
In[3]:= enc["InitializationVector"]//Normal

Out[3]= {94, 37, 187, 116, 240, 194, 218, 23, 28, 254, 180, 12, 76, 155, 138, 211}
```

---

Some block modes are more secure than others:

```wl
In[1]:= key  = GenerateSymmetricKey[];
```

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

```wl
In[2]:= ecb = Encrypt[key, ByteArray[ConstantArray[1, 100]], Method -> <|"BlockMode" -> "ECB"|>];

In[3]:= ListLinePlot[Normal[ecb["Data"]]]

Out[3]= [image]
```

There is not with CBC:

```wl
In[4]:= cbc = Encrypt[key, ByteArray[ConstantArray[1, 100]], Method -> <|"BlockMode" -> "CBC"|>];

In[5]:= ListLinePlot[Normal[cbc["Data"]]]

Out[5]= [image]
```

### Possible Issues (5)

#### Timing (1)

Encrypting using a password requires key derivation, which is intentionally slow:

```wl
In[1]:= AbsoluteTiming[Encrypt["password", "test"]]

Out[1]=
{0.261085, EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{207, 3, 148, 155, 10, 138, 90, 103, 6, 175, 66, 92, 6, 100, 
     108, 5}], "Data" -> ByteArray[{174, 127, 143, 52, 16, 130, 6, 199, 138, 96, 251, 20, 150, 100, 
     101, 251}], "Padding" -> Automatic, "OriginalForm" -> String]]}
```

You can avoid this by pre-generating a key:

```wl
In[2]:= key = GenerateSymmetricKey["password"]

Out[2]=
SymmetricKey[Association["Cipher" -> "AES256", 
  "Key" -> ByteArray[{239, 5, 14, 94, 43, 137, 111, 138, 131, 164, 148, 166, 198, 173, 208, 80, 
     182, 110, 47, 23, 32, 82, 117, 148, 194, 87, 184, 164, 185, 249, 220, 167}], "KeySize" -> 256]]

In[3]:= AbsoluteTiming[Encrypt[key, "test"]]

Out[3]=
{0.0002923, EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{66, 165, 120, 214, 22, 9, 109, 192, 182, 179, 2, 3, 105, 0, 
     50, 70}], "Data" -> ByteArray[{89, 212, 170, 153, 109, 67, 141, 204, 65, 245, 55, 114, 33, 17, 
     193, 146}], "Padding" -> Automatic, "OriginalForm" -> String]]}
```

#### Encrypted Data Size (1)

The ciphertext may be a different length from the input:

```wl
In[1]:= Encrypt["my password", ByteArray[{1, 2, 3, 4, 5}]]

Out[1]=
EncryptedObject[Association["Cipher" -> "AES256", "BlockMode" -> "CBC", 
  "InitializationVector" -> ByteArray[{50, 94, 7, 147, 26, 216, 140, 69, 244, 7, 98, 148, 42, 5, 
     227, 32}], "Data" -> ByteArray[{127, 160, 102, 76, 182, 116, 25, 156, 118, 77, 109, 92, 154, 
     93, 186, 11}], "Padding" -> Automatic, "OriginalForm" -> ByteArray]]

In[2]:= Length[%["Data"]]

Out[2]= 16
```

#### Incompatible Keys (1)

Encryption with elliptic curve-based keys is not currently supported:

```wl
In[1]:= keys = GenerateAsymmetricKeyPair["EllipticCurve"]

Out[1]=
<|"PrivateKey" -> PrivateKey[Association["Type" -> "EllipticCurve", "CurveName" -> "secp256k1", 
  "PublicCurvePoint" -> 
   {79833859148311109820209544853226862943606618002146259241579641444648027110094, 
    11469015296511597720608853796794273483 ...      11, 97, 246, 195, 191, 133, 144, 25, 168, 183}], "PublicCurvePoint" -> 
   {79833859148311109820209544853226862943606618002146259241579641444648027110094, 
    11469015296511597720608853796794273483601389591758550254066842918560271542455}]]|>

In[2]:= Encrypt[keys["PrivateKey"], "Now is the time..."]
```

Encrypt::invkeyspec: Key should be a string or a valid SymmetricKey, PrivateKey, or PublicKey.

```wl
Out[2]= $Failed
```

#### Incompatible Padding Modes (1)

Encrypting with a private key is not possible for certain padding modes:

```wl
In[1]:= keys = GenerateAsymmetricKeyPair[]

Out[1]=
<|"PrivateKey" -> PrivateKey[Association["Type" -> "RSA", "Cipher" -> "RSA", "Padding" -> "PKCS1", 
  "PublicExponent" -> 65537, "PublicModulus" -> 254361675936841574469037198423907568163466860674730\
05490042153895557753257257397671167497983718138 ... 207, 169, 227, 86, 47, 139, 178, 214, 99, 208, 124, 144, 32, 
     112, 81, 156, 3, 206, 245, 178, 42, 21, 110, 143, 129, 52, 155, 202, 47, 65, 59, 120, 58, 149, 
     224, 147, 18, 229, 196, 48, 117, 85, 188, 3, 231, 38, 97, 47, 32, 13, 217}]]]|>

In[2]:= Encrypt[keys["PrivateKey"], "Now is the time...", Method -> <|"Padding" -> "OAEP"|>]
```

Encrypt::padding: Specified padding OAEP could not be used.

```wl
Out[2]= $Failed
```

#### 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"`` :

```wl
In[1]:=
keys  = GenerateAsymmetricKeyPair[];
Encrypt[keys["PublicKey"], "RSA is an asymmetric cipher", Method -> <|"BlockMode" -> "CBC"|>]
```

Encrypt::invmethodkeyi: Method option BlockMode is not one of {Padding} and will be ignored.

```wl
Out[1]=
EncryptedObject[Association["Cipher" -> "RSA", 
  "Data" -> ByteArray[{15, 43, 178, 83, 218, 5, 3, 35, 168, 117, 247, 56, 250, 210, 45, 101, 116, 
     72, 189, 242, 241, 27, 229, 66, 28, 233, 74, 85, 122, 83, 39, 232, 207, 51, 67, 18, 115, 252, 
  ... , 0, 42, 88, 106, 225, 
     50, 121, 81, 105, 62, 15, 140, 139, 208, 153, 240, 59, 74, 162, 68, 188, 228, 112, 0, 53, 159, 
     187, 199, 3, 61, 146, 65, 147, 106, 102, 81, 118, 47, 182, 139}], "OriginalForm" -> String, 
  "Padding" -> "PKCS1"]]
```

RC4 is a stream that does not support block modes of operation, so ``Encrypt`` will proceed ignoring the setting for ``"BlockMode"`` :

```wl
In[2]:=
key  = GenerateSymmetricKey[Method -> "RC4"];
Encrypt[key, "RC4 is a stream cipher", Method -> <|"BlockMode" -> "CFB"|>]
```

Encrypt::rc4blockmode: The option BlockMode -> CFB is invalid for RC4 encryption method and will be ignored.

```wl
Out[2]=
EncryptedObject[Association["Cipher" -> "RC4", "BlockMode" -> None, "InitializationVector" -> {}, 
  "Data" -> ByteArray[{53, 211, 225, 183, 204, 145, 209, 140, 109, 135, 39, 230, 15, 125, 175, 149, 
     190, 204, 172, 240, 246, 126}], "Padding" -> Automatic, "OriginalForm" -> String]]
```

## See Also

* [`Decrypt`](https://reference.wolfram.com/language/ref/Decrypt.en.md)
* [`EncryptFile`](https://reference.wolfram.com/language/ref/EncryptFile.en.md)
* [`EncryptedObject`](https://reference.wolfram.com/language/ref/EncryptedObject.en.md)
* [`SymmetricKey`](https://reference.wolfram.com/language/ref/SymmetricKey.en.md)
* [`GenerateSymmetricKey`](https://reference.wolfram.com/language/ref/GenerateSymmetricKey.en.md)
* [`GenerateAsymmetricKeyPair`](https://reference.wolfram.com/language/ref/GenerateAsymmetricKeyPair.en.md)
* [`GenerateDigitalSignature`](https://reference.wolfram.com/language/ref/GenerateDigitalSignature.en.md)
* [`Hash`](https://reference.wolfram.com/language/ref/Hash.en.md)
* [`FieldMasked`](https://reference.wolfram.com/language/ref/FieldMasked.en.md)
* [`CellularAutomaton`](https://reference.wolfram.com/language/ref/CellularAutomaton.en.md)
* [`ShiftRegisterSequence`](https://reference.wolfram.com/language/ref/ShiftRegisterSequence.en.md)
* [`BinarySerialize`](https://reference.wolfram.com/language/ref/BinarySerialize.en.md)
* [`AuthenticationDialog`](https://reference.wolfram.com/language/ref/AuthenticationDialog.en.md)

## Related Guides

* [`Cryptography`](https://reference.wolfram.com/language/guide/Cryptography.en.md)
* [Systems-Related String Operations](https://reference.wolfram.com/language/guide/SystemsRelatedStringOperations.en.md)
* [Discrete Mathematics](https://reference.wolfram.com/language/guide/DiscreteMathematics.en.md)
* [Cryptographic Number Theory](https://reference.wolfram.com/language/guide/CryptographicNumberTheory.en.md)
* [String Manipulation](https://reference.wolfram.com/language/guide/StringManipulation.en.md)
* [Binary Data](https://reference.wolfram.com/language/guide/BinaryData.en.md)
* [External Operations](https://reference.wolfram.com/language/guide/ExternalOperations.en.md)

## History

* [Introduced in 2015 (10.1)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn101.en.md) \| [Updated in 2019 (12.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn120.en.md) ▪ [2020 (12.1)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn121.en.md) ▪ [2021 (12.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn123.en.md) ▪ [2023 (13.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn133.en.md)