I have one already created project.
In this they are using one method for encrypting the String. In this project now I have to create a method which I can used for decrypting the encrypted string.
For encrypting a string the below method is used
private String EncryptPwd(String pwd) {
String encryptPwd = "";
if (!pwd.isEmpty()) {
byte[] sha1Bytes = EncryptionUtils.getSha1(pwd);
StringBuilder sb = new StringBuilder();
for (byte b : sha1Bytes) {
sb.append(b);
}
encryptPwd = sb.toString();
}
return encryptPwd;
}
EncryptionUtils class code is given below
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.log4j.Logger;
public final class EncryptionUtils {
private static QueueCacheValueCloner<byte[]> byteArrayCloner = new QueueCacheValueCloner() {
public byte[] cloneBean(byte[] original) {
return (byte[]) original.clone();
}
};
private static Logger logger = MiscUtils.getLogger();
private static final MessageDigest messageDigest = initMessageDigest();
private static final QueueCache<String, byte[]> sha1Cache = new QueueCache(4, 2048, byteArrayCloner);
private static final int MAX_SHA_KEY_CACHE_SIZE = 2048;
private static MessageDigest initMessageDigest() {
try {
return MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
logger.error("Error", e);
}
return null;
}
public static byte[] getSha1(String s) {
byte[] b = (byte[]) sha1Cache.get(s);
if (b == null) {
b = getSha1NoCache(s);
if (s.length() < 2048) {
sha1Cache.put(s, b);
}
}
return b;
}
protected static byte[] getSha1NoCache(String s) {
}
public static SecretKey generateEncryptionKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey;
}
public static SecretKeySpec generateEncryptionKey(String seed) {
byte[] sha1 = getSha1(seed);
SecretKeySpec secretKey = new SecretKeySpec(sha1, 0, 16, "AES");
return secretKey;
}
public static byte[] encrypt(SecretKey secretKey, byte[] plainData) throws IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
if (secretKey == null) {
return plainData;
}
Cipher cipher = Cipher.getInstance("AES");
cipher.init(1, secretKey);
byte[] results = cipher.doFinal(plainData);
return results;
}
public static byte[] decrypt(SecretKey secretKey, byte[] encryptedData) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
if (secretKey == null) {
return encryptedData;
}
Cipher cipher = Cipher.getInstance("AES");
cipher.init(2, secretKey);
byte[] results = cipher.doFinal(encryptedData);
return results;
}
}
Now I have to create a method for decrypting the encrypted string.
So can anyone suggest me how can I decrypt a String which is encrypted by above EncryptPwd
method.