Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Implementing Cryptographic Algorithms in Java, Study notes of Computer Security

Examples of implementing various cryptographic algorithms in java, including des, aes, and rsa. It covers the key generation, encryption, and decryption processes for each algorithm. The document also includes an example of the diffie-hellman key exchange algorithm. The code snippets demonstrate how to use the java cryptography extension (jce) to implement these algorithms, which can be useful for students and developers interested in learning about cryptography and its practical applications in software development.

Typology: Study notes

2023/2024

Uploaded on 05/03/2024

chandni-sikarwar
chandni-sikarwar 🇮🇳

3 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Implement DES Algorithm in java language.
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
class DESExample {
Cipher ecipher;
Cipher dcipher;
DESExample(SecretKey key) throws Exception {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
}
public String encrypt(String str) throws Exception {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return Base64.getEncoder().encodeToString(enc);
}
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Implementing Cryptographic Algorithms in Java and more Study notes Computer Security in PDF only on Docsity!

Implement DES Algorithm in java language. import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.util.Base64; class DESExample { Cipher ecipher; Cipher dcipher; DESExample(SecretKey key) throws Exception { ecipher = Cipher.getInstance("DES"); dcipher = Cipher.getInstance("DES"); ecipher.init(Cipher.ENCRYPT_MODE, key); dcipher.init(Cipher.DECRYPT_MODE, key); } public String encrypt(String str) throws Exception { // Encode the string into bytes using utf- byte[] utf8 = str.getBytes("UTF8"); // Encrypt byte[] enc = ecipher.doFinal(utf8); // Encode bytes to base64 to get a string return Base64.getEncoder().encodeToString(enc); }

public String decrypt(String str) throws Exception { // Decode base64 to get bytes byte[] dec = Base64.getDecoder().decode(str); byte[] utf8 = dcipher.doFinal(dec); // Decode using utf- return new String(utf8, "UTF8"); } public static void main(String[] args) throws Exception { final String secretText = "www.google.com"; System.out.println("SecretText: " + secretText); SecretKey key = KeyGenerator.getInstance("DES").generateKey(); DESExample encrypter = new DESExample(key); String encrypted = encrypter.encrypt(secretText); System.out.println("Encrypted Value: " + encrypted); String decrypted = encrypter.decrypt(encrypted); System.out.println("Decrypted: " + decrypted); } };

Implement AES algorithm in Java code import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.GCMParameterSpec; import java.util.Base64; public class AES_ENCRYPTION { private SecretKey key; private final int KEY_SIZE = 128; private final int DATA_LENGTH = 128; private Cipher encryptionCipher; public void init() throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(KEY_SIZE); key = keyGenerator.generateKey(); } public String encrypt(String data) throws Exception { byte[] dataInBytes = data.getBytes(); encryptionCipher = Cipher.getInstance("AES/GCM/NoPadding"); encryptionCipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptedBytes = encryptionCipher.doFinal(dataInBytes); return encode(encryptedBytes); } public String decrypt(String encryptedData) throws Exception { byte[] dataInBytes = decode(encryptedData);

Cipher decryptionCipher = Cipher.getInstance("AES/GCM/NoPadding"); GCMParameterSpec spec = new GCMParameterSpec(DATA_LENGTH, encryptionCipher.getIV()); decryptionCipher.init(Cipher.DECRYPT_MODE, key, spec); byte[] decryptedBytes = decryptionCipher.doFinal(dataInBytes); return new String(decryptedBytes); } private String encode(byte[] data) { return Base64.getEncoder().encodeToString(data); } private byte[] decode(String data) { return Base64.getDecoder().decode(data); } public static void main(String[] args) { try { AES_ENCRYPTION aes_encryption = new AES_ENCRYPTION(); aes_encryption.init(); String encryptedData = aes_encryption.encrypt("Hello, welcome to the encryption world"); String decryptedData = aes_encryption.decrypt(encryptedData); System.out.println("Encrypted Data : " + encryptedData); System.out.println("Decrypted Data : " + decryptedData); } catch (Exception ignored) { } } }

Implement RSA Algorithm using HTML & Java Script RSA Encryption

RSA Algorithm

Implemented Using HTML & Javascript


Enter P:
Enter Q :

Enter the Message:
[A=1, B=2,...]

Public Key(N):

Exponent(e):

Private Key(d):

Cipher Text(c):

for (i = 0; i < 10; i++) { x = 1 + i * t if (x % e == 0) { d = x / e; break; } } ctt = Math.pow(no, e).toFixed(0); ct = ctt % n; dtt = Math.pow(ct, d).toFixed(0); dt = dtt % n; document.getElementById('publickey(N)').innerHTML = n; document.getElementById('exponent(e)').innerHTML = e; document.getElementById('privatekey(d)').innerHTML = d; document.getElementById('ciphertext(ct)').innerHTML = ct; }

Output

Implement Diffie-Hellman Key Exchange algorithm import java.util.*; // create class DiffieHellmanAlgorithmExample to calculate the key for two persons class DHA { // main() method start public static void main(String[] args) { long P, G, x, a, y, b, ka, kb; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println("Both the users should be agreed upon the public keys G and P"); // take inputs for public keys from the user System.out.println("Enter value for public key G:"); G = sc.nextLong(); System.out.println("Enter value for public key P:"); P = sc.nextLong(); // get input from user for private keys a and b selected by User1 and User System.out.println("Enter value for private key a selected by user1:"); a = sc.nextLong(); System.out.println("Enter value for private key b selected by user2:"); b = sc.nextLong(); // call calculatePower() method to generate x and y keys x = calculatePower(G, a, P); y = calculatePower(G, b, P);

// call calculatePower() method to generate ka and kb secret keys after the exchange of x and y keys // calculate secret key for User ka = calculatePower(y, a, P); // calculate secret key for User kb = calculatePower(x, b, P); // print secret keys of user1 and user System.out.println("Secret key for User1 is:" + ka); System.out.println("Secret key for User2 is:" + kb); } // create calculatePower() method to find the value of x ^ y mod P private static long calculatePower(long x, long y, long P) { long result = 0; if (y == 1){ return x; } else{ result = ((long)Math.pow(x, y)) % P; return result; } } } Output: