0% found this document useful (0 votes)
23 views3 pages

Java RSA Algorithm Implementation

The document provides a Java program that implements the RSA algorithm for encryption and decryption. It generates public and private keys, allows the user to input a message for encryption, and then decrypts the message back to its original form. The program utilizes BigInteger for mathematical operations and Base64 for encoding the encrypted message.

Uploaded by

csecgirls0203
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)
23 views3 pages

Java RSA Algorithm Implementation

The document provides a Java program that implements the RSA algorithm for encryption and decryption. It generates public and private keys, allows the user to input a message for encryption, and then decrypts the message back to its original form. The program utilizes BigInteger for mathematical operations and Base64 for encoding the encrypted message.

Uploaded by

csecgirls0203
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

EXPERIMENT 8

AIM: Write a Java program to implement RSA Algorithm

import [Link];

import [Link];

import [Link].Base64;

import [Link];

class RSAAlgorithm {

private BigInteger n, e, d;

private int bitLength = 1024;

private SecureRandom random;

public RSAAlgorithm() {

random = new SecureRandom();

BigInteger p = [Link](bitLength / 2, random);

BigInteger q = [Link](bitLength / 2, random);

n = [Link](q);

BigInteger phi = ([Link]([Link])).multiply([Link]([Link]));

e = new BigInteger("65537"); // Commonly used public exponent

d = [Link](phi);

public String encrypt(String message) {

BigInteger messageBigInt = new BigInteger(1, [Link]());

BigInteger encrypted = [Link](e, n);

return [Link]().encodeToString([Link]());

}
public String decrypt(String encryptedMessage) {

BigInteger encryptedBigInt = new BigInteger(1,


[Link]().decode(encryptedMessage));

BigInteger decrypted = [Link](d, n);

return new String([Link]());

public void displayKeys() {

[Link]("Public Key (e, n): (" + e + ", " + n + ")");

[Link]("Private Key (d, n): (" + d + ", " + n + ")");

public static void main(String[] args) {

RSAAlgorithm rsa = new RSAAlgorithm();

Scanner scanner = new Scanner([Link]);

[Link]();

[Link]("Enter a message to encrypt: ");

String message = [Link]();

String encryptedMessage = [Link](message);

[Link]("Encrypted Message (Base64): " + encryptedMessage);

String decryptedMessage = [Link](encryptedMessage);

[Link]("Decrypted Message: " + decryptedMessage);

[Link]();
}

OUTPUT:

Common questions

Powered by AI

The computational complexity of the RSA algorithm in terms of encryption and decryption primarily involves exponentiation operations, which have a complexity of O(log e * log n), where e is the exponent and n is the modulus. Both encryption and decryption involve modular exponentiation, which is typically performed using the square-and-multiply method to increase efficiency. The choice of `e = 65537` reduces the number of multiplications needed during encryption, optimizing computational workload. However, decryption often involves a more substantial computation because it typically uses a large private exponent 'd', impacting performance if not optimized via Chinese Remainder Theorem or other methods .

The RSAAlgorithm class displays both the public and private keys by printing them to the console. While displaying the public key is standard practice since it's meant for public distribution, exposing the private key in this manner poses a significant security risk. In real-world applications, the private key should be stored securely and never exposed publicly. This programmatic practice shows the importance of understanding what should remain confidential within cryptographic implementations, highlighting the risks of inadvertent exposure that could lead to unauthorized access or decryption of sensitive data .

The public exponent 'e' is often set to '65537' in RSA implementations because it is a compromise between security and efficiency. This value is a prime number, which makes it unlikely to introduce vulnerabilities through small-cycle attacks or other similar methods. It's large enough to ensure sufficient security, while still being small enough to allow efficient encryption and signature verification due to the relatively low computational cost associated with raising numbers to this power during encryption .

In the RSA key generation process within the Java program, the SecureRandom class is used to generate cryptographic-quality random numbers for finding probable prime numbers 'p' and 'q'. These primes are crucial for generating the modulus 'n' and subsequently calculating the totient, which is used to determine the private key exponent 'd'. SecureRandom ensures that the generated primes offer sufficient cryptographic strength due to their unpredictable nature .

The RSAAlgorithm class uses BigInteger to handle large numbers necessary for encryption and decryption in the RSA algorithm. When encrypting, it converts the message into a BigInteger object and then uses the modPow method with the public exponent 'e' and modulus 'n' to perform modular exponentiation, which results in the encrypted BigInteger representation of the message. Similarly, during decryption, the encrypted message is decoded from Base64, parsed into a BigInteger, and modPow is used with the private exponent 'd' and modulus 'n' to decrypt the message back to its original form, which is then converted to a String .

BigInteger provides advantages in the RSA algorithm's implementation because it supports arbitrary-precision arithmetic operations, which are essential for handling the large numbers involved in RSA encryption and decryption. This class includes operations for modular arithmetic, finding modular inverses, and calculating powers with mod, which are all crucial for RSA operations. BigInteger's ability to handle vast numerical values beyond standard primitive data types ensures that the algorithm remains effective and secure, even as cryptographic key sizes increase .

Calculating the modular inverse of the public exponent 'e' with respect to the totient is crucial because it provides the private exponent 'd', which is necessary for decrypting messages. The modular inverse satisfies the equation d * e ≡ 1 (mod φ(n)), ensuring that decryption can reverse the encryption process, and thus maintain the integrity of the cryptographic system. Without a correct modular inverse calculation, decryption would be impossible, as the relation between 'e' and 'd' is essential for RSA's functioning .

During encryption, the RSAAlgorithm class first converts the plain message string into a byte array, which is then transformed into a BigInteger. The BigInteger representation of the message is encrypted using modular exponentiation and converted back to a byte array, which is finally encoded into a Base64 string for easy representation and transmission. For decryption, the process is reversed: the Base64 encoded string is decoded back to a byte array, then a BigInteger, and decrypted using modular exponentiation with the private key. The resulting byte array is converted into a string to retrieve the original message .

Using a 1024-bit key length in RSA implementations is increasingly regarded as insecure by current cryptographic standards. As computational power has significantly advanced, keys of this length are susceptible to being broken through advanced brute-force attacks and factoring algorithms. Modern recommendations suggest using at least a 2048-bit key length to ensure adequate security against such threats, balancing performance and security. Thus, while a 1024-bit key was considered sufficient in the past, the need for larger keys reflects evolving capabilities and the importance of future-proofing cryptographic systems .

Base64 encoding is used in the RSA implementation to ensure that the encrypted message, which is a binary data represented in bytes, can be transmitted as text without data corruption or interpretation issues. Base64 translates binary data into an ASCII string format, which can easily be printed or transferred over systems that handle text data. This encoding ensures that the encrypted message remains intact across various communication protocols that may not be binary-friendly, facilitating safe and reliable transfer of cryptographic information .

You might also like