0% found this document useful (0 votes)
39 views2 pages

Encryption

The document outlines a server-side and client-side implementation for generating and encrypting a symmetric AES key using RSA public key encryption. The server generates an AES key and encrypts it with the client's public RSA key, then sends the encrypted key to the client. The client decrypts the AES key using its private RSA key for further use in encryption and decryption processes.

Uploaded by

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

Encryption

The document outlines a server-side and client-side implementation for generating and encrypting a symmetric AES key using RSA public key encryption. The server generates an AES key and encrypts it with the client's public RSA key, then sends the encrypted key to the client. The client decrypts the AES key using its private RSA key for further use in encryption and decryption processes.

Uploaded by

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

/////======server side===============/////////

// Generate symmetric key (AES)


public static SecretKey generateAESKey() throws Exception {
KeyGenerator keyGen = [Link]("AES");
[Link](256);
return [Link]();
}

// Encrypt the symmetric key with client's public RSA key


public static String encryptSymmetricKey(SecretKey secretKey, PublicKey
clientPublicKey) throws Exception {
Cipher cipher = [Link]("RSA");
[Link](Cipher.ENCRYPT_MODE, clientPublicKey);
byte[] encryptedKeyBytes = [Link]([Link]());
return [Link]().encodeToString(encryptedKeyBytes);
}

public static void main(String[] args) throws Exception {


// Generate server RSA key pair (for demo; in practice, load from keystore)
KeyPair serverKeyPair = generateRSAKeyPair();

// Generate client RSA key pair (simulate client-side)


KeyPair clientKeyPair = generateRSAKeyPair();

// Generate symmetric AES key


SecretKey aesKey = generateAESKey();

// Encrypt AES key with client's public key


String encryptedAESKeyBase64 = encryptSymmetricKey(aesKey,
[Link]());

// Output the encrypted key (to send to client)


[Link]("Encrypted AES Key (Base64): " + encryptedAESKeyBase64);

// For demonstration, send this string to the client


}
}

//========================Client Side==============////
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].PKCS8EncodedKeySpec;
import [Link].Base64;

public class ClientDecryption {

// Decrypt the received encrypted AES key using client's private RSA key
public static SecretKey decryptAESKey(String encryptedKeyBase64, PrivateKey
clientPrivateKey) throws Exception {
byte[] encryptedKeyBytes = [Link]().decode(encryptedKeyBase64);
Cipher cipher = [Link]("RSA");
[Link](Cipher.DECRYPT_MODE, clientPrivateKey);
byte[] aesKeyBytes = [Link](encryptedKeyBytes);
return new SecretKeySpec(aesKeyBytes, "AES");
}

public static void main(String[] args) throws Exception {


// Assume client has its RSA key pair (simulate generation)
KeyPair clientKeyPair = generateRSAKeyPair();

// Encrypted key received from server (simulate)


String encryptedAESKeyBase64 = "<Received from server>";

// Decrypt the AES key


SecretKey aesKey = decryptAESKey(encryptedAESKeyBase64,
[Link]());

// Use the AES key for further encryption/decryption


[Link]("Decrypted AES Key: " +
[Link]().encodeToString([Link]()));
}

// Reuse RSA key pair generator


public static KeyPair generateRSAKeyPair() throws Exception {
KeyPairGenerator keyGen = [Link]("RSA");
[Link](2048);
return [Link]();
}
}

You might also like