Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
LCB Cipher
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Neural-LCB
LCB Cipher
Commits
247eb349
Commit
247eb349
authored
1 year ago
by
Indrakanti Aishwarya
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
3436fa86
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
NIST/SECURE_LCB_FILE_GENERATION.ipynb
+568
-0
568 additions, 0 deletions
NIST/SECURE_LCB_FILE_GENERATION.ipynb
with
568 additions
and
0 deletions
NIST/SECURE_LCB_FILE_GENERATION.ipynb
0 → 100644
+
568
−
0
View file @
247eb349
{
"cells": [
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"#1#Header\n",
"import csv\n",
"import numpy as np\n",
"import os \n",
"from os import urandom\n",
"from keras.models import model_from_json"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"#2#Defining Global Variables\n",
"num_rounds = 20\n",
"m = 0\n",
"o = 0\n",
"counter = 0\n",
"k_int = 0\n",
"k_int1 = 0"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"#3#Defining WORDSIZE\n",
"def WORD_SIZE():\n",
" return(16);"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"#4#Defining S-Box\n",
"s_box_mapping_np = np.array([12, 5, 6, 11, 9, 0, 10, 13, 3, 14, 15, 8, 4, 7, 1, 2], dtype=np.uint8)\n",
"\n",
"def s_box(input_bits):\n",
" input_bits_int = int(input_bits)\n",
" output_bits_int = s_box_mapping_np[input_bits_int]\n",
" return output_bits_int"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"#5#Defining P-Box\n",
"def decimal_to_binary_list(value, num_bits=4):\n",
" return np.array([int(x) for x in format(value, f'0{num_bits}b')], dtype=np.uint8)\n",
"\n",
"def p_box(c_decimal, d_decimal, x_decimal, y_decimal):\n",
" c = decimal_to_binary_list(c_decimal)\n",
" d = decimal_to_binary_list(d_decimal)\n",
" x = decimal_to_binary_list(x_decimal)\n",
" y = decimal_to_binary_list(y_decimal)\n",
" \n",
" e = np.zeros(16, dtype=np.uint8)\n",
"\n",
" e[0] = d[3]\n",
" e[1] = y[1]\n",
" e[2] = c[1]\n",
" e[3] = x[0]\n",
" e[4] = x[3]\n",
" e[5] = y[2]\n",
" e[6] = c[2]\n",
" e[7] = d[1]\n",
" e[8] = d[0]\n",
" e[9] = x[2]\n",
" e[10] = y[3]\n",
" e[11] = c[0]\n",
" e[12] = c[3]\n",
" e[13] = d[2]\n",
" e[14] = x[1]\n",
" e[15] = y[0]\n",
"\n",
" return e"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"#6#Defining L-Box\n",
"def l_box(f):\n",
"\n",
" h = np.zeros(16, dtype=np.uint8)\n",
" h[0] = f[0]\n",
" h[1] = f[8]\n",
" h[2] = f[7]\n",
" h[3] = f[15]\n",
" h[4] = f[1]\n",
" h[5] = f[9]\n",
" h[6] = f[6]\n",
" h[7] = f[14]\n",
" h[8] = f[2]\n",
" h[9] = f[10]\n",
" h[10] = f[5]\n",
" h[11] = f[13]\n",
" h[12] = f[3]\n",
" h[13] = f[11]\n",
" h[14] = f[4]\n",
" h[15] = f[12]\n",
" #print(\"H:\", h)\n",
" return h"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"#7#Defining F-function for Right Side of Plaintext\n",
"def binary_array_to_integer(output):\n",
" int_output = ''.join(map(str, output))\n",
" return int(int_output, 2)\n",
"\n",
"def to_binary(value, bits):\n",
" return format(value, f'0{bits}b')\n",
"\n",
"def f_function(x, key, d):\n",
" q=0\n",
" global m, counter, k_int\n",
" #print(\"X:\", x)\n",
" if isinstance(x, int):\n",
" x = [x]\n",
" input_parts = np.zeros((len(x), 4), dtype=np.uint16)\n",
" for i, val in enumerate(x):\n",
" input_parts[i] = np.array([val >> 12, (val >> 8) & 0xF, (val >> 4) & 0xF, val & 0xF])\n",
" #print(\"F_FUNCTION\")\n",
" #print(input_parts)\n",
" s_box_outputs = np.array([[s_box(element) for element in part] for part in input_parts])\n",
" #print(\"S-box:\", s_box_outputs)\n",
" p_box_outputs = np.zeros((len(x), 1, 16), dtype=np.uint8)\n",
" for i in range(len(x)):\n",
" p_box_outputs[i] = np.array(p_box(s_box_outputs[i][0], s_box_outputs[i][1], s_box_outputs[i][2], s_box_outputs[i][3]))\n",
" #print(\"P-box:\", p_box_outputs)\n",
" final_outputs = np.zeros(len(x), dtype=np.uint32)\n",
" #print(len(x))\n",
" for i in range(len(x)):\n",
" #print(len(x))\n",
" final_output = np.array(l_box(p_box_outputs[i][0]))\n",
" k = key[q][(m+1) % 4]\n",
" #print(\"final_output:\", final_output)\n",
" #print(\"Key:\", k)\n",
" if (counter > 1):\n",
" #print(\"counter:\", counter)\n",
" k_bin, k_int = subsequent_key(k_int)\n",
" #print(\"Key in binary:\", k_bin)\n",
" #print(\"k in int\", k_int)\n",
" output = final_output ^ k_bin\n",
" else:\n",
" k = to_binary(k,16)\n",
" k = np.array([int(bit) for bit in k])\n",
" #print(\"k\", k)\n",
" output = final_output ^ k\n",
" #print(\"XORING output:\", output)\n",
" output = binary_array_to_integer(output)\n",
" final_outputs[i] = output\n",
" q +=1 \n",
" #print(\"Final output:\", final_outputs)\n",
" if (m < 2):\n",
" m +=2\n",
" else:\n",
" m = 0\n",
" \n",
" #print(\"_______________________________________________________________\")\n",
" return final_outputs"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"#8#Key Generation Algorithm\n",
"def to_binary(value, bits):\n",
" return format(value, f'0{bits}b')\n",
"\n",
"def binary_array_to_integer(output):\n",
" int_output = ''.join(map(str, output))\n",
" return int(int_output, 2)\n",
"\n",
"def subsequent_key(x):\n",
" #x = [x]\n",
" if isinstance(x, int):\n",
" x = [x]\n",
" #print(\"sub key\", x)\n",
" input_parts = np.zeros((len(x), 4), dtype=np.uint16)\n",
" for i, val in enumerate(x):\n",
" input_parts[i] = np.array([val >> 12, (val >> 8) & 0xF, (val >> 4) & 0xF, val & 0xF])\n",
" #print(\"input_part\", input_parts)\n",
" s_box_outputs = np.array([[s_box(element) for element in part] for part in input_parts])\n",
" #print(\"S-box:\", s_box_outputs)\n",
" p_box_outputs = np.zeros((len(x), 1, 16), dtype=np.uint8)\n",
" for i in range(len(x)):\n",
" p_box_outputs[i] = np.array(p_box(s_box_outputs[i][0], s_box_outputs[i][1], s_box_outputs[i][2], s_box_outputs[i][3]))\n",
" #print(\"P-box:\", p_box_outputs)\n",
" bin_output = np.zeros(len(x), dtype=np.uint16)\n",
" final_output = np.zeros(len(x), dtype=np.uint16)\n",
" for i in range(len(x)):\n",
" bin_output = np.array(l_box(p_box_outputs[i][0]))\n",
" #print(bin_output)\n",
" #final_outputs[i] = final_output\n",
" output = binary_array_to_integer(bin_output)\n",
" #print(output)\n",
" final_output[i] = output\n",
" \n",
" #print(\"final_outputs:\", final_outputs)\n",
" return bin_output, final_output"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"#9#Defining F-function for Left Side of Plaintext\n",
"def binary_array_to_integer(output):\n",
" int_output = ''.join(map(str, output))\n",
" return int(int_output, 2)\n",
"\n",
"def ff_function(x, key, d):\n",
" q=0\n",
" global o, counter, k_int1\n",
" if isinstance(x, int):\n",
" x = [x]\n",
" \n",
" input_parts = np.zeros((len(x), 4), dtype=np.uint16)\n",
" for i, val in enumerate(x):\n",
" input_parts[i] = np.array([val >> 12, (val >> 8) & 0xF, (val >> 4) & 0xF, val & 0xF])\n",
" #print(\"FF_FUNCTION\")\n",
" #print(input_parts)\n",
" s_box_outputs = np.array([[s_box(element) for element in part] for part in input_parts])\n",
" #print(\"S-box:\", s_box_outputs)\n",
" p_box_outputs = np.zeros((len(x), 1, 16), dtype=np.uint8)\n",
" for i in range(len(x)):\n",
" p_box_outputs[i] = np.array(p_box(s_box_outputs[i][0], s_box_outputs[i][1], s_box_outputs[i][2], s_box_outputs[i][3]))\n",
" #print(\"P-box:\", p_box_outputs)\n",
" final_outputs = np.zeros(len(x), dtype=np.uint32)\n",
" #print(len(x))\n",
" for i in range(len(x)):\n",
" #print(len(x))\n",
" final_output = np.array(l_box(p_box_outputs[i][0]))\n",
" k = key[q][o % 4]\n",
" #print(\"final_output:\", final_output)\n",
" #print(\"Key in int:\", k)\n",
" if (counter > 1):\n",
" k_bin, k_int1 = subsequent_key(k_int1)\n",
" #print(\"Key in binary:\", k_bin)\n",
" #print(\"k\", k_int)\n",
" output = final_output ^ k_bin\n",
" else:\n",
" k = to_binary(k,16)\n",
" k = np.array([int(bit) for bit in k])\n",
" #print(\"k\", k)\n",
" output = final_output ^ k\n",
" #print(\"XORING output:\", output)\n",
" output = binary_array_to_integer(output)\n",
" final_outputs[i] = output\n",
" q +=1 \n",
" counter += 1\n",
" #print(\"Final output:\", final_outputs)\n",
" if (o < 2):\n",
" o +=2\n",
" else:\n",
" o = 0\n",
" #print(\"_______________________________________________________________\")\n",
" return final_outputs"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [],
"source": [
"#9#Convert the ciphertext pairs into Binary array\n",
"def convert_to_binary(row):\n",
" bin_array = np.zeros(32, dtype=np.uint8)\n",
" binary_str = format(row[0], '016b') + format(row[1], '016b')\n",
" for i, b in enumerate(binary_str):\n",
" bin_array[i] = int(b)\n",
" return bin_array"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"#10#Encryption Function\n",
"def lcb_encrypt(plaintext, key, rounds, d):\n",
" \n",
" left_plaintext = np.uint16(plaintext[0])\n",
" right_plaintext = np.uint16(plaintext[1])\n",
" L, R = left_plaintext, right_plaintext\n",
"\n",
" n = 0\n",
" \n",
" while n < rounds:\n",
" L, R = f_function(R, key, d), ff_function(L, key, d)\n",
" n += 1\n",
" print(\"Encryption done per round\") \n",
" #print(rounds)\n",
" #print(n)\n",
" return (L, R)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"#11#Function for generation of keys\n",
"import random\n",
"\n",
"def generate_hex_keys(num_keys, length=16):\n",
" hex_chars = \"0123456789ABCDEF\"\n",
" keys_str = [\"\".join(random.choices(hex_chars, k=length)) for _ in range(num_keys)]\n",
"\n",
" return keys_str\n",
"\n",
"def generate_round_keys(num_keys):\n",
" random_keys_hex = generate_hex_keys(num_keys)\n",
" #random_keys_hex = ['D63A529ECC92D353', '563A529ECC92D353', '163A529ECC92D353', 'D67AD296CC92DB53', '76BA569EDC9BD353']\n",
" #random_keys_hex = ['163A529D687529EC']\n",
" round_keys = []\n",
" \n",
" for random_key_hex in random_keys_hex:\n",
" random_key = int(random_key_hex, 16)\n",
"\n",
" K1 = (random_key >> 48) & 0xFFFF\n",
" K2 = (random_key >> 32) & 0xFFFF\n",
" K3 = (random_key >> 16) & 0xFFFF\n",
" K4 = random_key & 0xFFFF\n",
" \n",
" #k1_bin = to_binary(K1, 16)\n",
" #k2_bin = to_binary(K2, 16)\n",
" #k3_bin = to_binary(K3, 16)\n",
" #k4_bin = to_binary(K4, 16)\n",
"\n",
" #k1_np_array = np.array([int(bit) for bit in k1_bin])\n",
" #k2_np_array = np.array([int(bit) for bit in k2_bin])\n",
" #k3_np_array = np.array([int(bit) for bit in k3_bin])\n",
" #k4_np_array = np.array([int(bit) for bit in k4_bin])\n",
"\n",
" round_key = np.array([K1, K2, K3, K4])\n",
" round_keys.append(round_key)\n",
" round_key = np.array(round_keys)\n",
" #print(\"Key generation done:\", round_keys)\n",
" return round_key"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
"#12#Make dataset\n",
"\n",
"def make_train_data(n, nr, var):\n",
" global counter\n",
"\n",
" nonce = np.frombuffer(urandom(2), dtype=np.uint16);\n",
" cownter = np.arange(0,1023, dtype=np.uint16)\n",
" input = (nonce.astype(np.uint32) << 16) | cownter.astype(np.uint32)\n",
" plaintext = np.frombuffer(urandom(4*n), dtype=np.uint32);\n",
" #plaintext = [0xEED4B555]\n",
" #plaintext = [0xCED4B5C6, 0xCED4B5C6, 0xCED4B5C6, 0xCED4B5C6, 0xCED4B5C6]\n",
" plain0l = np.empty(n, dtype=np.uint16)\n",
" plain0r = np.empty(n, dtype=np.uint16)\n",
" plaintext0l = np.frombuffer(urandom(2*n), dtype=np.uint16)\n",
" plaintext0r = np.frombuffer(urandom(2*n), dtype=np.uint16)\n",
" \n",
" for i in range(n):\n",
" plain0l[i] = (input[i] >> 16) & 0xffff\n",
" plain0r[i] = input[i] & 0xffff\n",
" \n",
" round_keys = generate_round_keys(1)\n",
" \n",
" round_key = np.repeat(round_keys, 1024, axis=0)\n",
" \n",
" ctdata0l, ctdata0r = lcb_encrypt((plain0l, plain0r), round_key, nr, n)\n",
" ciphertext0l = ctdata0l ^ plaintext0l\n",
" print(ciphertext0l[0])\n",
" ciphertext0r = ctdata0r ^ plaintext0r\n",
" print(ciphertext0r[0])\n",
" ctdata = np.vstack((ciphertext0l, ciphertext0r)).T\n",
" \n",
" X = np.array([convert_to_binary(row) for row in ctdata])\n",
" print(X[0])\n",
" X = X.reshape(1, -1)\n",
" \n",
" filename = f\"output_{var}.txt\"\n",
"\n",
"# Open the file in write mode (overwrites existing file or creates a new one)\n",
" with open(filename, 'w') as file:\n",
" # Convert the array to a string without spaces\n",
" X_string = ''.join(X.astype(str)[0])\n",
"\n",
" # Write the string to the file\n",
" file.write(X_string)\n",
" \n",
" \"\"\"\n",
" with open(\"Dataset_NewP.csv\", \"w\", newline='') as f:\n",
" writer = csv.writer(f)\n",
" writer.writerow([\"plain0l\", \"plain0r\", \"plain1l\", \"plain1r\",\"Y\"])\n",
" for i in range(n):\n",
" writer.writerow([plain0l[i], plain0r[i], plain1l[i], plain1r[i],Y[i]])\n",
"\n",
" with open(\"Dataset_NewC.csv\", \"w\", newline='') as f:\n",
" writer = csv.writer(f)\n",
" writer.writerow([\"ctdata0l\", \"ctdata0r\", \"ctdata1l\", \"ctdata1r\",\"Y\"])\n",
" for i in range(n):\n",
" writer.writerow([ctdata0l[i], ctdata0r[i], ctdata1l[i], ctdata1r[i],Y[i]])\n",
" \"\"\"\n",
" return(X);"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Encryption done per round\n",
"25812\n",
"59196\n",
"[0 1 1 0 0 1 0 0 1 1 0 1 0 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 1 0 0]\n",
"Encryption done per round\n",
"65099\n",
"11722\n",
"[1 1 1 1 1 1 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 1 0 1 1 1 0 0 1 0 1 0]\n",
"Encryption done per round\n",
"35646\n",
"42735\n",
"[1 0 0 0 1 0 1 1 0 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 1 1 1]\n",
"Encryption done per round\n",
"1385\n",
"17212\n",
"[0 0 0 0 0 1 0 1 0 1 1 0 1 0 0 1 0 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0]\n",
"Encryption done per round\n",
"53116\n",
"9840\n",
"[1 1 0 0 1 1 1 1 0 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 1 0 0 0 0]\n",
"Encryption done per round\n",
"46666\n",
"41535\n",
"[1 0 1 1 0 1 1 0 0 1 0 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1]\n",
"Encryption done per round\n",
"56968\n",
"29415\n",
"[1 1 0 1 1 1 1 0 1 0 0 0 1 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0 0 1 1 1]\n",
"Encryption done per round\n",
"19138\n",
"32283\n",
"[0 1 0 0 1 0 1 0 1 1 0 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 0 1 1]\n",
"Encryption done per round\n",
"29708\n",
"61356\n",
"[0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0]\n",
"Encryption done per round\n",
"60847\n",
"3956\n",
"[1 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0]\n"
]
}
],
"source": [
"for i in range (1000000):\n",
" make_train_data(1023,20,i)"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [],
"source": [
"# Define the number of files and bits per row\n",
"num_files = 1000000\n",
"bits_per_row = 32736\n",
"\n",
"# Initialize the merged array\n",
"merged_array = []\n",
"\n",
"# Iterate over the files and read the content\n",
"for i in range(num_files):\n",
" file_name = f\"output_{i}.txt\" # Adjust the file name pattern if needed\n",
" i +=1\n",
" with open(file_name, 'r') as file:\n",
" content = file.read()\n",
" merged_array.append(content)\n",
"\n",
"# Convert the merged array into a single string with newline characters\n",
"merged_string = '\\n'.join(merged_array)\n",
"\n",
"# Write the merged string to the output file\n",
"output_file = \"merged_output.txt\"\n",
"with open(output_file, 'w') as file:\n",
" file.write(merged_string)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
```
python
#1#Header
import
csv
import
numpy
as
np
import
os
from
os
import
urandom
from
keras.models
import
model_from_json
```
%% Cell type:code id: tags:
```
python
#2#Defining Global Variables
num_rounds
=
20
m
=
0
o
=
0
counter
=
0
k_int
=
0
k_int1
=
0
```
%% Cell type:code id: tags:
```
python
#3#Defining WORDSIZE
def
WORD_SIZE
():
return
(
16
);
```
%% Cell type:code id: tags:
```
python
#4#Defining S-Box
s_box_mapping_np
=
np
.
array
([
12
,
5
,
6
,
11
,
9
,
0
,
10
,
13
,
3
,
14
,
15
,
8
,
4
,
7
,
1
,
2
],
dtype
=
np
.
uint8
)
def
s_box
(
input_bits
):
input_bits_int
=
int
(
input_bits
)
output_bits_int
=
s_box_mapping_np
[
input_bits_int
]
return
output_bits_int
```
%% Cell type:code id: tags:
```
python
#5#Defining P-Box
def
decimal_to_binary_list
(
value
,
num_bits
=
4
):
return
np
.
array
([
int
(
x
)
for
x
in
format
(
value
,
f
'
0
{
num_bits
}
b
'
)],
dtype
=
np
.
uint8
)
def
p_box
(
c_decimal
,
d_decimal
,
x_decimal
,
y_decimal
):
c
=
decimal_to_binary_list
(
c_decimal
)
d
=
decimal_to_binary_list
(
d_decimal
)
x
=
decimal_to_binary_list
(
x_decimal
)
y
=
decimal_to_binary_list
(
y_decimal
)
e
=
np
.
zeros
(
16
,
dtype
=
np
.
uint8
)
e
[
0
]
=
d
[
3
]
e
[
1
]
=
y
[
1
]
e
[
2
]
=
c
[
1
]
e
[
3
]
=
x
[
0
]
e
[
4
]
=
x
[
3
]
e
[
5
]
=
y
[
2
]
e
[
6
]
=
c
[
2
]
e
[
7
]
=
d
[
1
]
e
[
8
]
=
d
[
0
]
e
[
9
]
=
x
[
2
]
e
[
10
]
=
y
[
3
]
e
[
11
]
=
c
[
0
]
e
[
12
]
=
c
[
3
]
e
[
13
]
=
d
[
2
]
e
[
14
]
=
x
[
1
]
e
[
15
]
=
y
[
0
]
return
e
```
%% Cell type:code id: tags:
```
python
#6#Defining L-Box
def
l_box
(
f
):
h
=
np
.
zeros
(
16
,
dtype
=
np
.
uint8
)
h
[
0
]
=
f
[
0
]
h
[
1
]
=
f
[
8
]
h
[
2
]
=
f
[
7
]
h
[
3
]
=
f
[
15
]
h
[
4
]
=
f
[
1
]
h
[
5
]
=
f
[
9
]
h
[
6
]
=
f
[
6
]
h
[
7
]
=
f
[
14
]
h
[
8
]
=
f
[
2
]
h
[
9
]
=
f
[
10
]
h
[
10
]
=
f
[
5
]
h
[
11
]
=
f
[
13
]
h
[
12
]
=
f
[
3
]
h
[
13
]
=
f
[
11
]
h
[
14
]
=
f
[
4
]
h
[
15
]
=
f
[
12
]
#print("H:", h)
return
h
```
%% Cell type:code id: tags:
```
python
#7#Defining F-function for Right Side of Plaintext
def
binary_array_to_integer
(
output
):
int_output
=
''
.
join
(
map
(
str
,
output
))
return
int
(
int_output
,
2
)
def
to_binary
(
value
,
bits
):
return
format
(
value
,
f
'
0
{
bits
}
b
'
)
def
f_function
(
x
,
key
,
d
):
q
=
0
global
m
,
counter
,
k_int
#print("X:", x)
if
isinstance
(
x
,
int
):
x
=
[
x
]
input_parts
=
np
.
zeros
((
len
(
x
),
4
),
dtype
=
np
.
uint16
)
for
i
,
val
in
enumerate
(
x
):
input_parts
[
i
]
=
np
.
array
([
val
>>
12
,
(
val
>>
8
)
&
0xF
,
(
val
>>
4
)
&
0xF
,
val
&
0xF
])
#print("F_FUNCTION")
#print(input_parts)
s_box_outputs
=
np
.
array
([[
s_box
(
element
)
for
element
in
part
]
for
part
in
input_parts
])
#print("S-box:", s_box_outputs)
p_box_outputs
=
np
.
zeros
((
len
(
x
),
1
,
16
),
dtype
=
np
.
uint8
)
for
i
in
range
(
len
(
x
)):
p_box_outputs
[
i
]
=
np
.
array
(
p_box
(
s_box_outputs
[
i
][
0
],
s_box_outputs
[
i
][
1
],
s_box_outputs
[
i
][
2
],
s_box_outputs
[
i
][
3
]))
#print("P-box:", p_box_outputs)
final_outputs
=
np
.
zeros
(
len
(
x
),
dtype
=
np
.
uint32
)
#print(len(x))
for
i
in
range
(
len
(
x
)):
#print(len(x))
final_output
=
np
.
array
(
l_box
(
p_box_outputs
[
i
][
0
]))
k
=
key
[
q
][(
m
+
1
)
%
4
]
#print("final_output:", final_output)
#print("Key:", k)
if
(
counter
>
1
):
#print("counter:", counter)
k_bin
,
k_int
=
subsequent_key
(
k_int
)
#print("Key in binary:", k_bin)
#print("k in int", k_int)
output
=
final_output
^
k_bin
else
:
k
=
to_binary
(
k
,
16
)
k
=
np
.
array
([
int
(
bit
)
for
bit
in
k
])
#print("k", k)
output
=
final_output
^
k
#print("XORING output:", output)
output
=
binary_array_to_integer
(
output
)
final_outputs
[
i
]
=
output
q
+=
1
#print("Final output:", final_outputs)
if
(
m
<
2
):
m
+=
2
else
:
m
=
0
#print("_______________________________________________________________")
return
final_outputs
```
%% Cell type:code id: tags:
```
python
#8#Key Generation Algorithm
def
to_binary
(
value
,
bits
):
return
format
(
value
,
f
'
0
{
bits
}
b
'
)
def
binary_array_to_integer
(
output
):
int_output
=
''
.
join
(
map
(
str
,
output
))
return
int
(
int_output
,
2
)
def
subsequent_key
(
x
):
#x = [x]
if
isinstance
(
x
,
int
):
x
=
[
x
]
#print("sub key", x)
input_parts
=
np
.
zeros
((
len
(
x
),
4
),
dtype
=
np
.
uint16
)
for
i
,
val
in
enumerate
(
x
):
input_parts
[
i
]
=
np
.
array
([
val
>>
12
,
(
val
>>
8
)
&
0xF
,
(
val
>>
4
)
&
0xF
,
val
&
0xF
])
#print("input_part", input_parts)
s_box_outputs
=
np
.
array
([[
s_box
(
element
)
for
element
in
part
]
for
part
in
input_parts
])
#print("S-box:", s_box_outputs)
p_box_outputs
=
np
.
zeros
((
len
(
x
),
1
,
16
),
dtype
=
np
.
uint8
)
for
i
in
range
(
len
(
x
)):
p_box_outputs
[
i
]
=
np
.
array
(
p_box
(
s_box_outputs
[
i
][
0
],
s_box_outputs
[
i
][
1
],
s_box_outputs
[
i
][
2
],
s_box_outputs
[
i
][
3
]))
#print("P-box:", p_box_outputs)
bin_output
=
np
.
zeros
(
len
(
x
),
dtype
=
np
.
uint16
)
final_output
=
np
.
zeros
(
len
(
x
),
dtype
=
np
.
uint16
)
for
i
in
range
(
len
(
x
)):
bin_output
=
np
.
array
(
l_box
(
p_box_outputs
[
i
][
0
]))
#print(bin_output)
#final_outputs[i] = final_output
output
=
binary_array_to_integer
(
bin_output
)
#print(output)
final_output
[
i
]
=
output
#print("final_outputs:", final_outputs)
return
bin_output
,
final_output
```
%% Cell type:code id: tags:
```
python
#9#Defining F-function for Left Side of Plaintext
def
binary_array_to_integer
(
output
):
int_output
=
''
.
join
(
map
(
str
,
output
))
return
int
(
int_output
,
2
)
def
ff_function
(
x
,
key
,
d
):
q
=
0
global
o
,
counter
,
k_int1
if
isinstance
(
x
,
int
):
x
=
[
x
]
input_parts
=
np
.
zeros
((
len
(
x
),
4
),
dtype
=
np
.
uint16
)
for
i
,
val
in
enumerate
(
x
):
input_parts
[
i
]
=
np
.
array
([
val
>>
12
,
(
val
>>
8
)
&
0xF
,
(
val
>>
4
)
&
0xF
,
val
&
0xF
])
#print("FF_FUNCTION")
#print(input_parts)
s_box_outputs
=
np
.
array
([[
s_box
(
element
)
for
element
in
part
]
for
part
in
input_parts
])
#print("S-box:", s_box_outputs)
p_box_outputs
=
np
.
zeros
((
len
(
x
),
1
,
16
),
dtype
=
np
.
uint8
)
for
i
in
range
(
len
(
x
)):
p_box_outputs
[
i
]
=
np
.
array
(
p_box
(
s_box_outputs
[
i
][
0
],
s_box_outputs
[
i
][
1
],
s_box_outputs
[
i
][
2
],
s_box_outputs
[
i
][
3
]))
#print("P-box:", p_box_outputs)
final_outputs
=
np
.
zeros
(
len
(
x
),
dtype
=
np
.
uint32
)
#print(len(x))
for
i
in
range
(
len
(
x
)):
#print(len(x))
final_output
=
np
.
array
(
l_box
(
p_box_outputs
[
i
][
0
]))
k
=
key
[
q
][
o
%
4
]
#print("final_output:", final_output)
#print("Key in int:", k)
if
(
counter
>
1
):
k_bin
,
k_int1
=
subsequent_key
(
k_int1
)
#print("Key in binary:", k_bin)
#print("k", k_int)
output
=
final_output
^
k_bin
else
:
k
=
to_binary
(
k
,
16
)
k
=
np
.
array
([
int
(
bit
)
for
bit
in
k
])
#print("k", k)
output
=
final_output
^
k
#print("XORING output:", output)
output
=
binary_array_to_integer
(
output
)
final_outputs
[
i
]
=
output
q
+=
1
counter
+=
1
#print("Final output:", final_outputs)
if
(
o
<
2
):
o
+=
2
else
:
o
=
0
#print("_______________________________________________________________")
return
final_outputs
```
%% Cell type:code id: tags:
```
python
#9#Convert the ciphertext pairs into Binary array
def
convert_to_binary
(
row
):
bin_array
=
np
.
zeros
(
32
,
dtype
=
np
.
uint8
)
binary_str
=
format
(
row
[
0
],
'
016b
'
)
+
format
(
row
[
1
],
'
016b
'
)
for
i
,
b
in
enumerate
(
binary_str
):
bin_array
[
i
]
=
int
(
b
)
return
bin_array
```
%% Cell type:code id: tags:
```
python
#10#Encryption Function
def
lcb_encrypt
(
plaintext
,
key
,
rounds
,
d
):
left_plaintext
=
np
.
uint16
(
plaintext
[
0
])
right_plaintext
=
np
.
uint16
(
plaintext
[
1
])
L
,
R
=
left_plaintext
,
right_plaintext
n
=
0
while
n
<
rounds
:
L
,
R
=
f_function
(
R
,
key
,
d
),
ff_function
(
L
,
key
,
d
)
n
+=
1
print
(
"
Encryption done per round
"
)
#print(rounds)
#print(n)
return
(
L
,
R
)
```
%% Cell type:code id: tags:
```
python
#11#Function for generation of keys
import
random
def
generate_hex_keys
(
num_keys
,
length
=
16
):
hex_chars
=
"
0123456789ABCDEF
"
keys_str
=
[
""
.
join
(
random
.
choices
(
hex_chars
,
k
=
length
))
for
_
in
range
(
num_keys
)]
return
keys_str
def
generate_round_keys
(
num_keys
):
random_keys_hex
=
generate_hex_keys
(
num_keys
)
#random_keys_hex = ['D63A529ECC92D353', '563A529ECC92D353', '163A529ECC92D353', 'D67AD296CC92DB53', '76BA569EDC9BD353']
#random_keys_hex = ['163A529D687529EC']
round_keys
=
[]
for
random_key_hex
in
random_keys_hex
:
random_key
=
int
(
random_key_hex
,
16
)
K1
=
(
random_key
>>
48
)
&
0xFFFF
K2
=
(
random_key
>>
32
)
&
0xFFFF
K3
=
(
random_key
>>
16
)
&
0xFFFF
K4
=
random_key
&
0xFFFF
#k1_bin = to_binary(K1, 16)
#k2_bin = to_binary(K2, 16)
#k3_bin = to_binary(K3, 16)
#k4_bin = to_binary(K4, 16)
#k1_np_array = np.array([int(bit) for bit in k1_bin])
#k2_np_array = np.array([int(bit) for bit in k2_bin])
#k3_np_array = np.array([int(bit) for bit in k3_bin])
#k4_np_array = np.array([int(bit) for bit in k4_bin])
round_key
=
np
.
array
([
K1
,
K2
,
K3
,
K4
])
round_keys
.
append
(
round_key
)
round_key
=
np
.
array
(
round_keys
)
#print("Key generation done:", round_keys)
return
round_key
```
%% Cell type:code id: tags:
```
python
#12#Make dataset
def
make_train_data
(
n
,
nr
,
var
):
global
counter
nonce
=
np
.
frombuffer
(
urandom
(
2
),
dtype
=
np
.
uint16
);
cownter
=
np
.
arange
(
0
,
1023
,
dtype
=
np
.
uint16
)
input
=
(
nonce
.
astype
(
np
.
uint32
)
<<
16
)
|
cownter
.
astype
(
np
.
uint32
)
plaintext
=
np
.
frombuffer
(
urandom
(
4
*
n
),
dtype
=
np
.
uint32
);
#plaintext = [0xEED4B555]
#plaintext = [0xCED4B5C6, 0xCED4B5C6, 0xCED4B5C6, 0xCED4B5C6, 0xCED4B5C6]
plain0l
=
np
.
empty
(
n
,
dtype
=
np
.
uint16
)
plain0r
=
np
.
empty
(
n
,
dtype
=
np
.
uint16
)
plaintext0l
=
np
.
frombuffer
(
urandom
(
2
*
n
),
dtype
=
np
.
uint16
)
plaintext0r
=
np
.
frombuffer
(
urandom
(
2
*
n
),
dtype
=
np
.
uint16
)
for
i
in
range
(
n
):
plain0l
[
i
]
=
(
input
[
i
]
>>
16
)
&
0xffff
plain0r
[
i
]
=
input
[
i
]
&
0xffff
round_keys
=
generate_round_keys
(
1
)
round_key
=
np
.
repeat
(
round_keys
,
1024
,
axis
=
0
)
ctdata0l
,
ctdata0r
=
lcb_encrypt
((
plain0l
,
plain0r
),
round_key
,
nr
,
n
)
ciphertext0l
=
ctdata0l
^
plaintext0l
print
(
ciphertext0l
[
0
])
ciphertext0r
=
ctdata0r
^
plaintext0r
print
(
ciphertext0r
[
0
])
ctdata
=
np
.
vstack
((
ciphertext0l
,
ciphertext0r
)).
T
X
=
np
.
array
([
convert_to_binary
(
row
)
for
row
in
ctdata
])
print
(
X
[
0
])
X
=
X
.
reshape
(
1
,
-
1
)
filename
=
f
"
output_
{
var
}
.txt
"
# Open the file in write mode (overwrites existing file or creates a new one)
with
open
(
filename
,
'
w
'
)
as
file
:
# Convert the array to a string without spaces
X_string
=
''
.
join
(
X
.
astype
(
str
)[
0
])
# Write the string to the file
file
.
write
(
X_string
)
"""
with open(
"
Dataset_NewP.csv
"
,
"
w
"
, newline=
''
) as f:
writer = csv.writer(f)
writer.writerow([
"
plain0l
"
,
"
plain0r
"
,
"
plain1l
"
,
"
plain1r
"
,
"
Y
"
])
for i in range(n):
writer.writerow([plain0l[i], plain0r[i], plain1l[i], plain1r[i],Y[i]])
with open(
"
Dataset_NewC.csv
"
,
"
w
"
, newline=
''
) as f:
writer = csv.writer(f)
writer.writerow([
"
ctdata0l
"
,
"
ctdata0r
"
,
"
ctdata1l
"
,
"
ctdata1r
"
,
"
Y
"
])
for i in range(n):
writer.writerow([ctdata0l[i], ctdata0r[i], ctdata1l[i], ctdata1r[i],Y[i]])
"""
return
(
X
);
```
%% Cell type:code id: tags:
```
python
for
i
in
range
(
1000000
):
make_train_data
(
1023
,
20
,
i
)
```
%% Output
Encryption done per round
25812
59196
[0 1 1 0 0 1 0 0 1 1 0 1 0 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 1 0 0]
Encryption done per round
65099
11722
[1 1 1 1 1 1 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 1 0 1 1 1 0 0 1 0 1 0]
Encryption done per round
35646
42735
[1 0 0 0 1 0 1 1 0 0 1 1 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 1 1 1]
Encryption done per round
1385
17212
[0 0 0 0 0 1 0 1 0 1 1 0 1 0 0 1 0 1 0 0 0 0 1 1 0 0 1 1 1 1 0 0]
Encryption done per round
53116
9840
[1 1 0 0 1 1 1 1 0 1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 1 0 0 0 0]
Encryption done per round
46666
41535
[1 0 1 1 0 1 1 0 0 1 0 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1]
Encryption done per round
56968
29415
[1 1 0 1 1 1 1 0 1 0 0 0 1 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0 0 1 1 1]
Encryption done per round
19138
32283
[0 1 0 0 1 0 1 0 1 1 0 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 0 1 1]
Encryption done per round
29708
61356
[0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0]
Encryption done per round
60847
3956
[1 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0]
%% Cell type:code id: tags:
```
python
# Define the number of files and bits per row
num_files
=
1000000
bits_per_row
=
32736
# Initialize the merged array
merged_array
=
[]
# Iterate over the files and read the content
for
i
in
range
(
num_files
):
file_name
=
f
"
output_
{
i
}
.txt
"
# Adjust the file name pattern if needed
i
+=
1
with
open
(
file_name
,
'
r
'
)
as
file
:
content
=
file
.
read
()
merged_array
.
append
(
content
)
# Convert the merged array into a single string with newline characters
merged_string
=
'
\n
'
.
join
(
merged_array
)
# Write the merged string to the output file
output_file
=
"
merged_output.txt
"
with
open
(
output_file
,
'
w
'
)
as
file
:
file
.
write
(
merged_string
)
```
%% Cell type:code id: tags:
```
python
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment