Click here to Skip to main content
15,895,823 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
Can anyone tell me how to decrypt the javaSE code. How to do this?
And please Post over here the Source code for JavaSE decrypt.

Thanks in Advance.
Posted
Comments
coolRahul_12 27-Mar-14 3:18am    
Can u please tell few more details

1 solution

Hi,
I got the answer .

package com.evora.practice;


import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class JavaDecrypt {


public static void main(String[] args) throws Exception {

KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
SecretKey secretkey = keygenerator.generateKey();

String plaintextString = "Test Password";
//Print the data as normal
System.out.println("In Normal Format :-"+plaintextString + " " + bytesToHex(plaintextString.getBytes()) + " " + Arrays.toString(plaintextString.getBytes()));

SecretKeySpec key = new SecretKeySpec(secretkey.getEncoded(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");

cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(plaintextString.getBytes());
String encryptedString = bytesToHex(encrypted);
//Print the data in Encrypted format
System.out.println("Encrypted data :-"+new String(encrypted) + " " + encryptedString + " " + Arrays.toString(encrypted));

cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(hexToBytes(encryptedString));
String decryptedString = bytesToHex(decrypted);
//Print the data in decrypted format
System.out.println("Decrypted data :-"+new String(decrypted) + " " + decryptedString + " " + Arrays.toString(decrypted));

}

public static byte[] hexToBytes(String str) {
if (str == null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i = 0; i < len; i++) {
buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
}
return buffer;
}

}

public static String bytesToHex(byte[] data) {
if (data == null) {
return null;
} else {
int len = data.length;
String str = "";
for (int i = 0; i < len; i++) {
if ((data[i] & 0xFF) < 16)
str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
else
str = str + java.lang.Integer.toHexString(data[i] & 0xFF);
}
return str.toUpperCase();
}
}
}
Thanks.. :)
 
Share this answer
 
Comments
RaviRanjanKr 1-Apr-14 1:04am    
Good for you. but you should wrap your code in pre tag

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