Code2pdf 668e16a114ef8
Code2pdf 668e16a114ef8
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "boRk-HrrZbLK",
"outputId": "82df0864-c9f1-4796-efc6-0df93a52b04a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"SHA-256 hash of 'Hello, world!': 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3\n"
]
}
],
"source": [
"# SHA-256 Hashing\n",
"import hashlib\n",
"\n",
"# Function to hash a string using SHA-256\n",
"def sha256_hash_string(input_string):\n",
" # Create a new sha256 hash object\n",
" sha256 = hashlib.sha256()\n",
"\n",
" # Update the hash object with the bytes of the input string\n",
" sha256.update(input_string.encode('utf-8'))\n",
"\n",
" # Get the hexadecimal representation of the hash\n",
" hex_digest = sha256.hexdigest()\n",
"\n",
" return hex_digest\n",
"\n",
"# Example usage\n",
"input_string = \"Hello, world!\"\n",
"hash_result = sha256_hash_string(input_string)\n",
"print(f\"SHA-256 hash of '{input_string}': {hash_result}\")\n"
]
},
{
"cell_type": "code",
"source": [
"#RSA\n",
"import math\n",
"\n",
"# step 1\n",
"p = 3\n",
"q = 7\n",
"\n",
"# step 2\n",
"n = p*q\n",
"print(\"n =\", n)\n",
"\n",
"# step 3\n",
"phi = (p-1)*(q-1)\n",
"\n",
"# step 4\n",
"e = 2\n",
"while(e<phi):\n",
" if (math.gcd(e, phi) == 1):\n",
" break\n",
" else:\n",
" e += 1\n",
"\n",
"print(\"e =\", e)\n",
"# step 5\n",
"k = 2\n",
"d = ((k*phi)+1)/e\n",
"print(\"d =\", d)\n",
"print(f'Public key: {e, n}')\n",
"print(f'Private key: {d, n}')\n",
"\n",
"# plain text\n",
"msg = 11\n",
"print(f'Original message:{msg}')\n",
"\n",
"# encryption\n",
"C = pow(msg, e)\n",
"C = math.fmod(C, n)\n",
"print(f'Encrypted message: {C}')\n",
"\n",
"# decryption\n",
"M = pow(C, d)\n",
"M = math.fmod(M, n)\n",
"\n",
"print(f'Decrypted message: {M}')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3wRwsvN2Zb83",
"outputId": "7ec2247a-2af6-4669-9734-8745e68f354b"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"n = 21\n",
"e = 5\n",
"d = 5.0\n",
"Public key: (5, 21)\n",
"Private key: (5.0, 21)\n",
"Original message:11\n",
"Encrypted message: 2.0\n",
"Decrypted message: 11.0\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Vernam\n",
"def Vernam(text, key):\n",
" cipher_text = ''\n",
" for i in range(len(text)):\n",
" character = chr(ord(text[i]) ^ ord(key[i]))\n",
" cipher_text += character\n",
" return cipher_text\n",
"\n",
"if __name__ == \"__main__\":\n",
" text = \"My Secret text\"\n",
" key = 'mjcipuqszsymzp' # Ensure key is at least as long as text\n",
" cipher_text = Vernam(text, key)\n",
" print(\"Cipher text:\", repr(cipher_text))\n",
" original_text = Vernam(cipher_text, key)\n",
" print(\"Original text:\", original_text)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ChTQXT9eIYLy",
"outputId": "a4f54966-c85e-4445-ac7a-8b8a14851edd"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Cipher text: ' \\x13C:\\x15\\x16\\x03\\x16\\x0eS\\r\\x08\\x02\\x04'\n",
"Original text: My Secret text\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Vignere\n",
"def vigenere(text, key, mode='encrypt'):\n",
" key = (key * (len(text) // len(key) + 1))[:len(text)]\n",
" res = []\n",
" for t, k in zip(text, key):\n",
" if mode == 'encrypt':\n",
" res.append(chr((ord(t) + ord(k) - 2 * ord('A')) % 26 + ord('A')))\n",
" else:\n",
" res.append(chr((ord(t) - ord(k) + 26) % 26 + ord('A')))\n",
" return ''.join(res)\n",
"\n",
"if __name__ == \"__main__\":\n",
" text = \"HELLOVIGENERECIPHER\"\n",
" key = \"KEY\"\n",
" cipher_text = vigenere(text, key, mode='encrypt')\n",
" print(\"Cipher Text: \", cipher_text)\n",
" original_text = vigenere(cipher_text, key, mode='decrypt')\n",
" print(\"Original Text: \", original_text)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Q_V9-LL1IpMI",
"outputId": "f8c4974a-2086-4f6c-a561-094e59b037b1"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Cipher Text: RIJVSTSKCXIPOGGZLCB\n",
"Original Text: HELLOVIGENERECIPHER\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#rc4\n",
"def rc4(key, data):\n",
" S = list(range(256))\n",
" j = 0\n",
" out = []\n",
"\n",
" # Key scheduling algorithm (KSA)\n",
" for i in range(256):\n",
" j = (j + S[i] + key[i % len(key)]) % 256\n",
" S[i], S[j] = S[j], S[i]\n",
"\n",
" # Pseudo-random generation algorithm (PRGA)\n",
" i = j = 0\n",
" for char in data:\n",
" i = (i + 1) % 256\n",
" j = (j + S[i]) % 256\n",
" S[i], S[j] = S[j], S[i]\n",
" out.append(char ^ S[(S[i] + S[j]) % 256])\n",
"\n",
" return bytes(out)\n",
"\n",
"# Main program\n",
"if __name__ == \"__main__\n",
"\":\n",
" key = b'secretkey' # Key for encryption/decryption\n",
" plain_text = \"This is a secret message.\"\n",
"\n",
" # Convert plaintext to bytes\n",
" plain_text_bytes = plain_text.encode('utf-8')\n",
"\n",
" # Encrypt\n",
" encrypted = rc4(key, plain_text_bytes)\n",
" print(f\"Encrypted text: {encrypted}\")\n",
"\n",
" # Decrypt (RC4 encryption and decryption are symmetric)\n",
" decrypted = rc4(key, encrypted)\n",
" print(f\"Decrypted text: {decrypted.decode('utf-8')}\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sKjN_Is5Ja3W",
"outputId": "09e1ec24-abe0-4cf0-ddf0-eaf71d9b5660"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Encrypted text: b'\\xf1\\xc0\\xb5ol\\xd8\\xe6hI\\x8c8\\xe1\\xe1UP_T:l\\x13\\xed\\x12IQ\\xb3'\n",
"Decrypted text: This is a secret message.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#vigenere\n",
"def vigenere(text, key, mode='encrypt'):\n",
" key = (key * (len(text) // len(key) + 1))[:len(text)]\n",
" res = []\n",
" for t, k in zip(text, key):\n",
" if mode == 'encrypt':\n",
" res.append(chr((ord(t) + ord(k) - 2 * ord('A')) % 26 + ord('A')))\n",
" else:\n",
" res.append(chr((ord(t) - ord(k) + 26) % 26 + ord('A')))\n",
" return ''.join(res)\n",
"\n",
"if __name__ == \"__main__\":\n",
" text = \"HELLOVIGENERECIPHER\"\n",
" key = \"KEY\"\n",
" cipher_text = vigenere(text, key, mode='encrypt')\n",
" print(\"Cipher Text: \", cipher_text)\n",
" original_text = vigenere(cipher_text, key, mode='decrypt')\n",
" print(\"Original Text: \", original_text)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rVYALWG4K-LF",
"outputId": "c32b9157-4f4a-4580-fda0-3e67540cbcf3"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Cipher Text: RIJVSTSKCXIPOGGZLCB\n",
"Original Text: HELLOVIGENERECIPHER\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#hill cipher\n",
"keyMatrix = [[0] * 3 for i in range(3)]\n",
"\n",
"\n",
"messageVector = [[0] for i in range(3)]\n",
"\n",
"\n",
"cipherMatrix = [[0] for i in range(3)]\n",
"\n",
"def getKeyMatrix(key):\n",
"\tk = 0\n",
"\tfor i in range(3):\n",
"\t\tfor j in range(3):\n",
"\t\t\tkeyMatrix[i][j] = ord(key[k]) % 65\n",
"\t\t\tk += 1\n",
"\n",
"def encrypt(messageVector):\n",
"\tfor i in range(3):\n",
"\t\tfor j in range(1):\n",
"\t\t\tcipherMatrix[i][j] = 0\n",
"\t\t\tfor x in range(3):\n",
"\t\t\t\tcipherMatrix[i][j] += (keyMatrix[i][x] *\n",
"\t\t\t\t\t\t\t\t\tmessageVector[x][j])\n",
"\t\t\tcipherMatrix[i][j] = cipherMatrix[i][j] % 26\n",
"\n",
"def HillCipher(message, key):\n",
"\n",
"\tgetKeyMatrix(key)\n",
"\n",
"\tfor i in range(3):\n",
"\t\tmessageVector[i][0] = ord(message[i]) % 65\n",
"\n",
"\n",
"\tencrypt(messageVector)\n",
"\n",
"\n",
"\tCipherText = []\n",
"\tfor i in range(3):\n",
"\t\tCipherText.append(chr(cipherMatrix[i][0] + 65))\n",
"\n",
"\tprint(\"Ciphertext: \", \"\".join(CipherText))\n",
"\n",
"def main():\n",
"\n",
"\n",
"\tmessage = \"ACT\"\n",
"\n",
"\n",
"\tkey = \"GYBNQKURP\"\n",
"\n",
"\tHillCipher(message, key)\n",
"\n",
"if __name__ == \"__main__\":\n",
"\tmain()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "N40mm5dnVryU",
"outputId": "72eb3c22-0ebb-4e64-9829-9490ab903f9b"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Ciphertext: POH\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#feistel\n",
"def xor_strings(s1, s2):\n",
" return ''.join(chr(ord(a) ^ ord(b)) for a, b in zip(s1, s2))\n",
"\n",
"def feistel_round(left, right, key):\n",
" new_left = right\n",
" new_right = xor_strings(left, xor_strings(right, key))\n",
" return new_left, new_right\n",
"\n",
"def feistel(text, keys, encrypt=True):\n",
" left, right = text[:len(text)//2], text[len(text)//2:]\n",
" if not encrypt:\n",
" keys = reversed(keys)\n",
" for key in keys:\n",
" left, right = feistel_round(left, right, key)\n",
" return left + right\n",
"\n",
"if __name__ == \"__main__\":\n",
" text = \"HELLO123\" # Text length should be even\n",
" keys = [\"K1\", \"K2\", \"K3\", \"K4\"] # Example round keys\n",
"\n",
" # Ensure text length is even\n",
" if len(text) % 2 != 0:\n",
" text += \"X\"\n",
"\n",
" encrypted_text = feistel(text, keys, encrypt=True)\n",
" print(\"Encrypted Text:\", encrypted_text)\n",
"\n",
" decrypted_text = feistel(encrypted_text, keys, encrypt=False)\n",
" print(\"Decrypted Text:\", decrypted_text)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "82CQ1ccQV7OM",
"outputId": "2fc50482-218f-443d-a5d3-004dee5b7a63"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Encrypted Text: O0LB\n",
"Decrypted Text: LCHE\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"pip install pycryptodome"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "t9WUt1dhX4yi",
"outputId": "95d9749c-b8e8-42c7-887b-16615fef9eac"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Collecting pycryptodome\n",
" Downloading pycryptodome-3.20.0-cp35-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.1/2.1 MB\u001b[0m \u001b[31m7.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hInstalling collected packages: pycryptodome\n",
"Successfully installed pycryptodome-3.20.0\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#aes\n",
"from Crypto.Cipher import AES\n",
"from Crypto.Util.Padding import pad, unpad\n",
"from Crypto.Random import get_random_bytes\n",
"\n",
"def aes_encrypt(plaintext, key):\n",
" cipher = AES.new(key, AES.MODE_CBC)\n",
" ciphertext = cipher.encrypt(pad(plaintext.encode('utf-8'), AES.block_size))\n",
" return cipher.iv + ciphertext\n",
"\n",
"def aes_decrypt(ciphertext, key):\n",
" iv = ciphertext[:AES.block_size]\n",
" cipher = AES.new(key, AES.MODE_CBC, iv)\n",
" plaintext = unpad(cipher.decrypt(ciphertext[AES.block_size:]), AES.block_size)\n",
" return plaintext.decode('utf-8')\n",
"\n",
"if __name__ == \"__main__\":\n",
" plaintext = \"Hello, AES!\"\n",
" key = get_random_bytes(16) # AES-128 key (16 bytes)\n",
"\n",
" ciphertext = aes_encrypt(plaintext, key)\n",
" print(\"Ciphertext:\", ciphertext)\n",
"\n",
" decrypted_text = aes_decrypt(ciphertext, key)\n",
" print(\"Decrypted Text:\", decrypted_text)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1TT1OeSUX-NP",
"outputId": "e171c48a-44c6-47d9-a539-2c96d24d015d"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Ciphertext: b'\\xd3(\\x0b\\xd1\\xeem\\xd6\\x184\\xfdFE5\\x08v\\x80\\x85H\\x8ck;c7$\\xb7\\x9fW\\xf6:\\x0cdZ'\n",
"Decrypted Text: Hello, AES!\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#2 des\n",
"from Crypto.Cipher import DES\n",
"from Crypto.Random import get_random_bytes\n",
"from Crypto.Util.Padding import pad, unpad\n",
"\n",
"def des_encrypt(plaintext, key):\n",
" cipher = DES.new(key, DES.MODE_ECB)\n",
" return cipher.encrypt(plaintext)\n",
"\n",
"def des_decrypt(ciphertext, key):\n",
" cipher = DES.new(key, DES.MODE_ECB)\n",
" return cipher.decrypt(ciphertext)\n",
"\n",
"def double_des_encrypt(plaintext, key1, key2):\n",
" padded_plaintext = pad(plaintext, DES.block_size)\n",
" encrypted = des_encrypt(padded_plaintext, key1)\n",
" encrypted = des_encrypt(encrypted, key2)\n",
" return encrypted\n",
"\n",
"def double_des_decrypt(ciphertext, key1, key2):\n",
" decrypted = des_decrypt(ciphertext, key2)\n",
" decrypted = des_decrypt(decrypted, key1)\n",
" return unpad(decrypted, DES.block_size)\n",
"\n",
"if __name__ == \"__main__\":\n",
" plaintext = b\"Hello, 2DES!\" # Example plaintext (as bytes)\n",
" key1 = get_random_bytes(8) # First DES key (8 bytes)\n",
" key2 = get_random_bytes(8) # Second DES key (8 bytes)\n",
"\n",
" ciphertext = double_des_encrypt(plaintext, key1, key2)\n",
" print(\"Ciphertext:\", ciphertext)\n",
"\n",
" decrypted_text = double_des_decrypt(ciphertext, key1, key2)\n",
" print(\"Decrypted Text:\", decrypted_text.decode('utf-8'))\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2UcBbE3vYFtP",
"outputId": "2fc79b6c-1f6a-4a05-cb52-1901503bb6d3"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Ciphertext: b'9\\xff\\xdd&2\\xa0,\\x17\\x02\\xed\\x18\\xad\\xbf\\x8f\\xdaw'\n",
"Decrypted Text: Hello, 2DES!\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "WyhNeYmJYWk3"
},
"execution_count": null,
"outputs": []
}
]
}