Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been trying to do decryption in java for exact same decryption descibed in C# AES 256 bits Encryption Library with Salt[^] which is C# . But i am not able to get the same output , Could anybody assist.

What I have tried:

byte[] salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

			SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
			
			MessageDigest md = MessageDigest.getInstance("SHA-256" );
			
			md.update(KEY_IV.getBytes( StandardCharsets.UTF_8 ) );
			byte[] hashDigest = md.digest();
			
		
			String encryptedHashDigest = Base64.encodeBase64String(hashDigest);
			
			PBEKeySpec pbeKeySpec = new PBEKeySpec(encryptedHashDigest.toCharArray(),
					salt, 1000, 384);

			Key generatedKey = factory.generateSecret(pbeKeySpec);

			byte[] extractedKey = new byte[32];
			byte[] iv = new byte[16];


			System.arraycopy(generatedKey.getEncoded(), 0, extractedKey, 0, 32);
			System.arraycopy(generatedKey.getEncoded(), 32, iv, 0, 16);

			
			byte[] base64Decoded = Base64.decodeBase64(toBeDecrypted);

			Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");

			c.init(2, new SecretKeySpec(extractedKey, "AES"), new IvParameterSpec(iv));

			byte[] decryptedBytes = c.doFinal(base64Decoded);

			System.out.println("Decrypted " + new String(Base64.encodeBase64(decryptedBytes), StandardCharsets.UTF_8));
Posted
Updated 24-Feb-18 11:20am
v2
Comments
Richard MacCutchan 22-Feb-18 4:15am    
Assist how?
David_Wimbley 22-Feb-18 11:37am    
Instead of trying to port something that I am sure has been done numerous times in java, why not just use a java library or do a google search for java aes256 encryption? This seems pointless to me

aes256 encryption in java

Plenty of links on first page of google result would get you the same result and probably much quicker.
Shameer.Abdul 22-Feb-18 23:32pm    
Hi David,
Encryption technique to decrypt the text is as above. Will decrypting we need IV and Key for AES/CBC decryption but only one 48 lenght key is provided which has both key and iv and we are requested to fetch the same from the provided key. therefore above way of decryption is mandatory. Anyway thanks for your suggestion

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900