0% found this document useful (0 votes)
22 views8 pages

Cloud Computing Lab Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Cloud Computing Lab Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Aes encryption

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import java.util.Base64;

public class AESEncryptionUtil {

private static final String AES = "AES";

private static final String AES_KEY = "1234567890123456";

public static SecretKey getAESKey() {

return new SecretKeySpec(AES_KEY.getBytes(), AES);

public static String encrypt(String message, SecretKey key) throws Exception {

Cipher cipher = Cipher.getInstance(AES);

cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] encryptedBytes = cipher.doFinal(message.getBytes());

return Base64.getEncoder().encodeToString(encryptedBytes);

public static String decrypt(String encryptedMessage, SecretKey key) throws Exception {

Cipher cipher = Cipher.getInstance(AES);

cipher.init(Cipher.DECRYPT_MODE, key);

byte[] decodedBytes = Base64.getDecoder().decode(encryptedMessage);

byte[] decryptedBytes = cipher.doFinal(decodedBytes);

return new String(decryptedBytes);

}
server

import java.io.*;

import java.net.*;

import javax.crypto.SecretKey;

public class Server {

public static void main(String[] args) {

try {

ServerSocket serverSocket = new ServerSocket(8080);

System.out.println("Server is running");

while (true) {

Socket socket = serverSocket.accept();

System.out.println("Client connected");

BufferedReader in = new BufferedReader(new


InputStreamReader(socket.getInputStream()));

String encryptedMessage = in.readLine();

System.out.println("Received encrypted message: " + encryptedMessage);

try {

SecretKey key = AESEncryptionUtil.getAESKey();

String decryptedMessage = AESEncryptionUtil.decrypt(encryptedMessage, key);

System.out.println("Decrypted message: " + decryptedMessage);

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

out.println("Message received and decrypted successfully");


} catch (Exception e) {

System.out.println("Failed to decrypt message");

socket.close();

} catch (Exception e) {

e.printStackTrace();

client:
import java.io.*;

import java.net.*;

import javax.crypto.SecretKey;

public class Client {

public static void main(String[] args) {

try {

Socket socket = new Socket("localhost", 8080);

SecretKey key = AESEncryptionUtil.getAESKey();

String message = "Hello server...";

String encryptedMessage = AESEncryptionUtil.encrypt(message, key);

System.out.println("Encrypted message: " + encryptedMessage);

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

out.println(encryptedMessage);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

System.out.println("Server response: " + in.readLine());

in.close();

out.close();

socket.close();

} catch (Exception e) {

e.printStackTrace();

def gcd(a, b):


while b != 0:
a, b = b, a % b
return a

def modinv(e, phi):


d, x1, x2, y1, temp_phi = 0, 0, 1, 1, phi
while e > 0:
temp1, temp2 = divmod(temp_phi, e)
temp_phi, e = e, temp2
x = x2 - temp1 * x1
y = y1 - temp1 * y
x2, x1 = x1, x
y1, y = y, y1
if temp_phi == 1:
return d + phi

def generate_keypair(p, q):


n=p*q
phi = (p - 1) * (q - 1)
e = 17
while gcd(e, phi) != 1:
e += 1
d = modinv(e, phi)
return ((e, n), (d, n))

def encrypt(pk, plaintext):


key, n = pk
cipher = [pow(ord(char), key, n) for char in plaintext]
return cipher

def decrypt(pk, ciphertext):


key, n = pk
plain = [chr(pow(char, key, n)) for char in ciphertext]
return ''.join(plain)

if __name__ == '__main__':
print("Provide two prime numbers greater than 10")
p = int(input("Enter a prime number: "))
q = int(input("Enter another prime number: "))

public, private = generate_keypair(p, q)


print(f"Public key: {public}")
print(f"Private key: {private}")

message = input("Enter the message to encrypt: ")


encrypted_msg = encrypt(public, message)
print(f"Encrypted message: {encrypted_msg}")

decrypted_msg = decrypt(private, encrypted_msg)

print(f"Decrypted message: {decrypted_msg}")

7th program
from cryptography.hazmat.primitives import hashes

from cryptography.hazmat.primitives.asymmetric import ec

from cryptography.hazmat.primitives import serialization

private_key = ec.generate_private_key(ec.SECP256R1())

public_key = private_key.public_key()

public_pem = public_key.public_bytes(

encoding=serialization.Encoding.PEM,

format=serialization.PublicFormat.SubjectPublicKeyInfo

print("Public key PEM format:\n", public_pem.decode())

message = input("Enter a message to be signed: ").encode()

signature = private_key.sign(message, ec.ECDSA(hashes.SHA256()))


print("Signature:", signature)

try:

public_key.verify(signature, message, ec.ECDSA(hashes.SHA256()))

print("Signature is valid")

except:

print("Signature is failed")

You might also like