|
4 | 4 | "metadata": {
|
5 | 5 | "colab": {
|
6 | 6 | "name": "Encryption examples.ipynb",
|
7 |
| - "provenance": [] |
| 7 | + "provenance": [], |
| 8 | + "include_colab_link": true |
8 | 9 | },
|
9 | 10 | "kernelspec": {
|
10 | 11 | "name": "python3",
|
11 | 12 | "display_name": "Python 3"
|
12 |
| - }, |
13 |
| - "accelerator": "GPU" |
| 13 | + } |
14 | 14 | },
|
15 | 15 | "cells": [
|
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "metadata": { |
| 19 | + "id": "view-in-github", |
| 20 | + "colab_type": "text" |
| 21 | + }, |
| 22 | + "source": [ |
| 23 | + "<a href=\"https://colab.research.google.com/github/pbelevich/csprng/blob/encrypt_decrypt_notebook/notebooks/encrypt_decrypt.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" |
| 24 | + ] |
| 25 | + }, |
16 | 26 | {
|
17 | 27 | "cell_type": "markdown",
|
18 | 28 | "metadata": {
|
|
34 | 44 | {
|
35 | 45 | "cell_type": "code",
|
36 | 46 | "metadata": {
|
37 |
| - "id": "jC1O-C25vI0W" |
| 47 | + "id": "jC1O-C25vI0W", |
| 48 | + "outputId": "beee40f0-7fa8-4354-affd-2fcf18305685", |
| 49 | + "colab": { |
| 50 | + "base_uri": "https://localhost:8080/" |
| 51 | + } |
38 | 52 | },
|
39 | 53 | "source": [
|
40 | 54 | "# !pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html\n",
|
41 | 55 | "# for CUDA runtime:\n",
|
42 | 56 | "!pip install --pre torchcsprng -f https://download.pytorch.org/whl/nightly/cu101/torch_nightly.html"
|
43 | 57 | ],
|
44 |
| - "execution_count": null, |
45 |
| - "outputs": [] |
| 58 | + "execution_count": 1, |
| 59 | + "outputs": [ |
| 60 | + { |
| 61 | + "output_type": "stream", |
| 62 | + "text": [ |
| 63 | + "Looking in links: https://download.pytorch.org/whl/nightly/cu101/torch_nightly.html\n", |
| 64 | + "Collecting torchcsprng\n", |
| 65 | + "\u001b[?25l Downloading https://download.pytorch.org/whl/nightly/cu101/torchcsprng-0.2.0.dev20201210%2Bcu101-cp36-cp36m-linux_x86_64.whl (4.4MB)\n", |
| 66 | + "\u001b[K |████████████████████████████████| 4.4MB 552kB/s \n", |
| 67 | + "\u001b[?25hCollecting torch==1.8.0.dev20201210+cu101\n", |
| 68 | + "\u001b[?25l Downloading https://download.pytorch.org/whl/nightly/cu101/torch-1.8.0.dev20201210%2Bcu101-cp36-cp36m-linux_x86_64.whl (788.6MB)\n", |
| 69 | + "\u001b[K |████████████████████████████████| 788.6MB 25kB/s \n", |
| 70 | + "\u001b[?25hRequirement already satisfied: typing-extensions in /usr/local/lib/python3.6/dist-packages (from torch==1.8.0.dev20201210+cu101->torchcsprng) (3.7.4.3)\n", |
| 71 | + "Requirement already satisfied: dataclasses; python_version < \"3.7\" in /usr/local/lib/python3.6/dist-packages (from torch==1.8.0.dev20201210+cu101->torchcsprng) (0.8)\n", |
| 72 | + "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from torch==1.8.0.dev20201210+cu101->torchcsprng) (1.18.5)\n", |
| 73 | + "\u001b[31mERROR: torchvision 0.8.1+cu101 has requirement torch==1.7.0, but you'll have torch 1.8.0.dev20201210+cu101 which is incompatible.\u001b[0m\n", |
| 74 | + "Installing collected packages: torch, torchcsprng\n", |
| 75 | + " Found existing installation: torch 1.7.0+cu101\n", |
| 76 | + " Uninstalling torch-1.7.0+cu101:\n", |
| 77 | + " Successfully uninstalled torch-1.7.0+cu101\n", |
| 78 | + "Successfully installed torch-1.8.0.dev20201210+cu101 torchcsprng-0.2.0.dev20201210+cu101\n" |
| 79 | + ], |
| 80 | + "name": "stdout" |
| 81 | + } |
| 82 | + ] |
46 | 83 | },
|
47 | 84 | {
|
48 | 85 | "cell_type": "code",
|
|
53 | 90 | "import torch\n",
|
54 | 91 | "import torchcsprng as csprng"
|
55 | 92 | ],
|
56 |
| - "execution_count": null, |
| 93 | + "execution_count": 2, |
57 | 94 | "outputs": []
|
58 | 95 | },
|
59 | 96 | {
|
|
64 | 101 | "source": [
|
65 | 102 | "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')"
|
66 | 103 | ],
|
67 |
| - "execution_count": null, |
| 104 | + "execution_count": 3, |
68 | 105 | "outputs": []
|
69 | 106 | },
|
70 | 107 | {
|
|
79 | 116 | {
|
80 | 117 | "cell_type": "code",
|
81 | 118 | "metadata": {
|
82 |
| - "id": "rw7WYZ-50To9" |
| 119 | + "id": "rw7WYZ-50To9", |
| 120 | + "outputId": "7e9accec-5ac1-4de2-fcc5-860df06ad338", |
| 121 | + "colab": { |
| 122 | + "base_uri": "https://localhost:8080/" |
| 123 | + } |
83 | 124 | },
|
84 | 125 | "source": [
|
85 | 126 | "key = torch.empty(16, dtype=torch.uint8, device=device).random_(0, 256)\n",
|
86 | 127 | "key"
|
87 | 128 | ],
|
88 |
| - "execution_count": null, |
89 |
| - "outputs": [] |
| 129 | + "execution_count": 4, |
| 130 | + "outputs": [ |
| 131 | + { |
| 132 | + "output_type": "execute_result", |
| 133 | + "data": { |
| 134 | + "text/plain": [ |
| 135 | + "tensor([ 94, 155, 111, 73, 36, 65, 95, 175, 103, 249, 39, 221, 225, 106,\n", |
| 136 | + " 147, 18], dtype=torch.uint8)" |
| 137 | + ] |
| 138 | + }, |
| 139 | + "metadata": { |
| 140 | + "tags": [] |
| 141 | + }, |
| 142 | + "execution_count": 4 |
| 143 | + } |
| 144 | + ] |
90 | 145 | },
|
91 | 146 | {
|
92 | 147 | "cell_type": "markdown",
|
|
109 | 164 | {
|
110 | 165 | "cell_type": "code",
|
111 | 166 | "metadata": {
|
112 |
| - "id": "LcuVmhyU0WTn" |
| 167 | + "id": "LcuVmhyU0WTn", |
| 168 | + "outputId": "2b887caa-ef4c-4e2a-bc7e-d00191cb727c", |
| 169 | + "colab": { |
| 170 | + "base_uri": "https://localhost:8080/" |
| 171 | + } |
113 | 172 | },
|
114 | 173 | "source": [
|
115 | 174 | "initial = torch.empty(42, dtype=torch.float32, device=device).normal_(-24.0, 42.0)\n",
|
116 | 175 | "initial"
|
117 | 176 | ],
|
118 |
| - "execution_count": null, |
119 |
| - "outputs": [] |
| 177 | + "execution_count": 5, |
| 178 | + "outputs": [ |
| 179 | + { |
| 180 | + "output_type": "execute_result", |
| 181 | + "data": { |
| 182 | + "text/plain": [ |
| 183 | + "tensor([ -33.2154, -4.4617, -2.9105, -37.1893, -6.0623, -47.4273,\n", |
| 184 | + " -44.7682, 23.7199, 52.3595, -80.1355, 10.7041, -52.0069,\n", |
| 185 | + " -59.1335, -51.7051, -96.7531, -47.6263, -37.4668, -26.9140,\n", |
| 186 | + " -8.7239, -68.0884, -73.8865, -35.0378, -24.9022, -71.3780,\n", |
| 187 | + " -83.3700, -73.9181, -56.9136, -14.0332, -0.1193, -24.6635,\n", |
| 188 | + " -12.3755, 26.1501, -21.7292, 61.0362, -8.2424, -27.3287,\n", |
| 189 | + " -116.2249, -32.5120, -56.9908, -44.0908, 15.9910, -12.2838])" |
| 190 | + ] |
| 191 | + }, |
| 192 | + "metadata": { |
| 193 | + "tags": [] |
| 194 | + }, |
| 195 | + "execution_count": 5 |
| 196 | + } |
| 197 | + ] |
120 | 198 | },
|
121 | 199 | {
|
122 | 200 | "cell_type": "markdown",
|
|
135 | 213 | "source": [
|
136 | 214 | "encrypted = torch.empty(22, dtype=torch.int64, device=device)"
|
137 | 215 | ],
|
138 |
| - "execution_count": null, |
| 216 | + "execution_count": 6, |
139 | 217 | "outputs": []
|
140 | 218 | },
|
141 | 219 | {
|
|
150 | 228 | {
|
151 | 229 | "cell_type": "code",
|
152 | 230 | "metadata": {
|
153 |
| - "id": "TK4OjPRq4lsJ" |
| 231 | + "id": "TK4OjPRq4lsJ", |
| 232 | + "outputId": "a9c7bee0-e95c-49fa-a6a8-8a33b58695e0", |
| 233 | + "colab": { |
| 234 | + "base_uri": "https://localhost:8080/" |
| 235 | + } |
154 | 236 | },
|
155 | 237 | "source": [
|
156 | 238 | "csprng.encrypt(initial, encrypted, key, \"aes128\", \"ecb\")"
|
157 | 239 | ],
|
158 |
| - "execution_count": null, |
159 |
| - "outputs": [] |
| 240 | + "execution_count": 7, |
| 241 | + "outputs": [ |
| 242 | + { |
| 243 | + "output_type": "execute_result", |
| 244 | + "data": { |
| 245 | + "text/plain": [ |
| 246 | + "tensor([-1712310488599806515, -8311769289706173237, -8219472943074007291,\n", |
| 247 | + " 4944862344658876406, 5289614356287313567, -6556419268017486570,\n", |
| 248 | + " -2070634943111059129, -6988005632242749066, -6786984565899259673,\n", |
| 249 | + " 2617853683610034142, 7394987086142621793, 9037048344238914391,\n", |
| 250 | + " -4389724417591829293, 639143755047053321, 6630155308790632093,\n", |
| 251 | + " 8233678956273288167, 7762736707078557167, -66820074992426839,\n", |
| 252 | + " 2506358826618882964, 3811127519115226076, 7223286519671228245,\n", |
| 253 | + " -4680879586720463643])" |
| 254 | + ] |
| 255 | + }, |
| 256 | + "metadata": { |
| 257 | + "tags": [] |
| 258 | + }, |
| 259 | + "execution_count": 7 |
| 260 | + } |
| 261 | + ] |
160 | 262 | },
|
161 | 263 | {
|
162 | 264 | "cell_type": "markdown",
|
|
175 | 277 | "source": [
|
176 | 278 | "decrypted = torch.empty_like(initial)"
|
177 | 279 | ],
|
178 |
| - "execution_count": null, |
| 280 | + "execution_count": 8, |
179 | 281 | "outputs": []
|
180 | 282 | },
|
181 | 283 | {
|
|
193 | 295 | "id": "kojXCFGK5v6l"
|
194 | 296 | },
|
195 | 297 | "source": [
|
196 |
| - "csprng.decrypt(encrypted, decrypted, key, \"aes128\", \"ecb\")" |
| 298 | + "# csprng.decrypt(encrypted, decrypted, key, \"aes128\", \"ecb\")" |
197 | 299 | ],
|
198 |
| - "execution_count": null, |
| 300 | + "execution_count": 10, |
199 | 301 | "outputs": []
|
200 | 302 | },
|
201 | 303 | {
|
|
0 commit comments