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

Ciorna

Uploaded by

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

Ciorna

Uploaded by

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

// ------------- 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));

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy