1-
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
# Key and data
key = get_random_bytes(16) # 16-byte key for AES-128
data = b"Hello AES!" # Data to encrypt
# Encrypt
cipher = AES.new(key, AES.MODE_CBC)
ct = cipher.encrypt(pad(data, 16))
# Decrypt
cipher2 = AES.new(key, AES.MODE_CBC, cipher.iv)
pt = unpad(cipher2.decrypt(ct), 16)
print("Original:", data)
print("Encrypted:", ct)
print("Decrypted:", pt)
2-
from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
# Step 1: Key (8 bytes for DES)
key = get_random_bytes(8)
# Step 2: Data to encrypt (must be padded to block size)
data = b"SecretMsg"
# Step 3: Encrypt
cipher = DES.new(key, DES.MODE_CBC)
ct = cipher.encrypt(pad(data, DES.block_size))
# Step 4: Decrypt
cipher2 = DES.new(key, DES.MODE_CBC, cipher.iv)
pt = unpad(cipher2.decrypt(ct), DES.block_size)
# Step 5: Output
print("Original:", data)
print("Encrypted:", ct)
print("Decrypted:", pt)
3-
pip install pycryptodome
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
# Generate RSA key pair
key = RSA.generate(2048)
public_key = key.publickey()
encryptor = PKCS1_OAEP.new(public_key)
decryptor = PKCS1_OAEP.new(key)
# Message
message = b"Hello RSA"
# Encrypt
encrypted = encryptor.encrypt(message)
# Decrypt
decrypted = decryptor.decrypt(encrypted)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
4-
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
# Step 1: Generate key and IV
key = get_random_bytes(16) # AES-128
iv = get_random_bytes(16)
# Step 2: Message to encrypt
data = b"Hello AES!"
# Step 3: Encrypt
cipher_encrypt = AES.new(key, AES.MODE_CBC, iv)
encrypted = cipher_encrypt.encrypt(pad(data, AES.block_size))
# Step 4: Decrypt
cipher_decrypt = AES.new(key, AES.MODE_CBC, iv)
decrypted = unpad(cipher_decrypt.decrypt(encrypted), AES.block_size)
# Step 5: Output
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
5-
import random
# Step 1: Prime number and generator
p = 23 # A small prime number for simplicity
g = 5 # A small generator for simplicity
# Step 2: Alice and Bob's private keys (random numbers)
alice_private = random.randint(1, p-1) # Random private key for Alice
bob_private = random.randint(1, p-1) # Random private key for Bob
# Step 3: Alice and Bob compute their public keys
alice_public = pow(g, alice_private, p)
bob_public = pow(g, bob_private, p)
# Step 4: Alice and Bob exchange public keys and compute the shared secret
alice_shared_secret = pow(bob_public, alice_private, p)
bob_shared_secret = pow(alice_public, bob_private, p)
# Step 5: Both Alice and Bob should now have the same shared secret
print(f"Alice's private key: {alice_private}")
print(f"Bob's private key: {bob_private}")
print(f"Alice's public key: {alice_public}")
print(f"Bob's public key: {bob_public}")
print(f"Alice's shared secret: {alice_shared_secret}")
print(f"Bob's shared secret: {bob_shared_secret}")
6-
from Crypto.Hash import SHA256
from Crypto.Protocol.KDF import PBKDF2
# Step 1: Define the secret key and message
secret_key = b"supersecretkey"
message = b"Hello, this is a secure message!"
# Step 2: Generate the HMAC using SHA-256 hash function
from Crypto.Hash import HMAC
hmac_object = HMAC.new(secret_key, msg=message, digestmod=SHA256)
# Step 3: Print the generated HMAC (the cryptographic checksum)
print("Generated HMAC:", hmac_object.hexdigest())
# Step 4: To verify the message, use the same secret key and check the HMAC
received_hmac = hmac_object.hexdigest()
# Step 5: Verification
# Generate the HMAC again using the received message and key for verification
verify_hmac = HMAC.new(secret_key, msg=message, digestmod=SHA256).hexdigest()
# Verify if the HMACs match
if received_hmac == verify_hmac:
print("The message is authentic and has not been altered.")
else:
print("The message's authenticity or integrity is compromised.")
7-
package p1;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHA1Example {
public static void main(String[] args) {
String message = "Hello, this is a test message!";
try {
// Step 1: Get SHA-1 MessageDigest instance
MessageDigest md = MessageDigest.getInstance("SHA-1");
// Step 2: Convert message to bytes and update the digest
md.update(message.getBytes());
// Step 3: Compute the digest
byte[] digest = md.digest();
// Step 4: Convert byte array to hexadecimal format
StringBuilder hexString = new StringBuilder();
for (byte b : digest) {
hexString.append(String.format("%02x", b));
}
// Step 5: Print the result
System.out.println("Original Message: " + message);
System.out.println("SHA-1 Digest: " + hexString.toString());
} catch (NoSuchAlgorithmException e) {
System.err.println("SHA-1 Algorithm not found.");
}
}
}
8-
from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.hazmat.primitives import hashes
# Generate DSA key pair (DSS standard)
private_key = dsa.generate_private_key(key_size=1024)
public_key = private_key.public_key()
# Message to sign
message = b"Hello, Digital Signature Standard!"
# Sign the message using SHA-256
signature = private_key.sign(message, hashes.SHA256())
# Verify the signature
try:
public_key.verify(signature, message, hashes.SHA256())
print("✅ Signature is valid.")
except Exception:
print("❌ Signature is invalid.")
1-
def encrypt_decrypt(message, key):
result = []
for i in range(len(message)):
result.append(chr(ord(message[i]) ^ ord(key[i % len(key)])))
return ''.join(result)
# Original message and key
message = "SecretMessage"
key = "key"
# Encrypt
cipher_text = encrypt_decrypt(message, key)
print("Encrypted:", cipher_text)
# Decrypt (same function)
decrypted_text = encrypt_decrypt(cipher_text, key)
print("Decrypted:", decrypted_text)
https://drive.google.com/file/d/124733jS9kGXJa_zuS3iRiEfHUFsxiDnN/view?usp=drivesdk
Comments
Post a Comment