// ------------- Exercițiul 1: AES Criptare și Decriptare --------------
System.out.println("---- Exercițiul 1: AES Criptare și Decriptare ----");
// Generare keyBytes prin PRNG (doar pentru prima parte)
byte[] keyBytes = new byte[16];
SecureRandom myPRNG = new SecureRandom();
myPRNG.nextBytes(keyBytes);
// Alternativă: Generarea cheii AES folosind o parolă (PBKDF2)
char[] password = "short_password".toCharArray(); // Parola definită
byte[] salt = new byte[16]; // Salt random pentru PBKDF2
int iteration_count = 10000; // Număr de iterații PBKDF2
int key_size = 128; // Dimensiunea cheii AES în biți
//myPRNG.nextBytes(salt); // Generare salt
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,
key_size);
SecretKey myAESPBKey = new
SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
System.out.println("AES key: " + new
BigInteger(1,myAESPBKey.getEncoded()).toString(16));
keyBytes = myAESPBKey.getEncoded(); // KeyBytes este cheia derivată din
parolă
// Criptare AES
SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
Cipher myAES = Cipher.getInstance("AES/CBC/PKCS5Padding");
// Key myKey;
myAES.init(Cipher.ENCRYPT_MODE, myKey, new IvParameterSpec(new byte[16]));
// // extra thingy added by us !
String plain = "acasaecelmaibine";
// initialize plaintext - modified by us !
byte[] plaintext = plain.getBytes();
//initialize ciphertext - we modified this, because we introduced
"AES/CBC/PKCS5Padding"
byte[] ciphertext = new byte[32];
int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext, 0);
myAES.doFinal(ciphertext, cLength);
System.out.println("plaintext: " + new
BigInteger(1,plaintext).toString(16));
System.out.println("ciphertext: " + new
BigInteger(1,ciphertext).toString(16));
myAES.init(Cipher.DECRYPT_MODE, myKey, new IvParameterSpec(new byte[16]));
byte[] dec_plaintext = new byte[16];
cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext, 0);
cLength += myAES.doFinal(dec_plaintext, cLength);
System.out.println("decrypted: " + new
BigInteger(1,dec_plaintext).toString(16));
System.out.println("decrypted: " + new String(dec_plaintext, 0, cLength));
// ------------- Exercițiul 2: RSA Criptare și Decriptare --------------
// System.out.println("\n---- Exercițiul 2: RSA Criptare și Decriptare
----");
//
// // Generare pereche de chei RSA
// Cipher myRSA = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// KeyPairGenerator myRSAKeyGen = KeyPairGenerator.getInstance("RSA");
// myRSAKeyGen.initialize(1024, myPRNG);
// KeyPair myRSAKeyPair = myRSAKeyGen.generateKeyPair();
// Key publicKey = myRSAKeyPair.getPublic();
// Key privateKey = myRSAKeyPair.getPrivate();
//
// // Criptare cu RSA
// myRSA.init(Cipher.ENCRYPT_MODE, publicKey, myPRNG);
// byte[] rsaCiphertext = myRSA.doFinal(keyBytes);
//
// System.out.println("RSA Ciphertext (Hex): " + new BigInteger(1,
rsaCiphertext).toString(16));
//
// // Decriptare cu RSA
// myRSA.init(Cipher.DECRYPT_MODE, privateKey);
// byte[] rsaDecryptedKeyBytes = myRSA.doFinal(rsaCiphertext);
//
// System.out.println("Decrypted AES Key (Hex): " + new BigInteger(1,
rsaDecryptedKeyBytes).toString(16));
//
//System.out.println("Original AES Key (Hex): " + new BigInteger(1,
keyBytes).toString(16));