Encrypting and decrypting password results in all asterisks "*"
843811Apr 14 2010 — edited Apr 14 2010Hi,
New to using the javax.crypto.* so I assume I doing something rather silly that I cannot spot.
Platform : Developed on a Windows XP OS [32 bit] [Eventually distributed to Windows 2008 Server 64 bit]
Application : SWING UI using NetBeans 6.8
Debugging via NetBeans IDE
What I am attempting to do is:
Read in a password from a SWING application
Encrypt and then store the encrypted password in a Windows registry
Result: I can read the registry fine and retrieve the encrypted pwd but when I decrypt it all I get back are "**************"
I have written to a file as well and read it back as byte[] to see if reading from the registry was causing the issue but it is the decrypt phase that the **** are returned ... any ideas what I am doing wrong ?
Thanks in advance
Eoin
Code being used to encrypt
public static String encrypt(String text, String factor) throws IOException, GeneralSecurityException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//setup key
byte[] keyBytes= new byte[16];
byte[] b= factor.getBytes("UTF-8");
int len= b.length;
if (len > keyBytes.length) len = keyBytes.length;
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte [] results = cipher.doFinal(text.getBytes("UTF-8"));
return Base64Utility.encode(results);
}
public static String decrypt(String text, String factor) throws IOException, GeneralSecurityException{
<snip some null checks,etc>
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//setup key
byte[] keyBytes= new byte[16];
byte[] b= factor.getBytes("UTF-8");
int len= b.length;
if (len > keyBytes.length) len = keyBytes.length;
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
byte[] deText = null;
try {
deText = Base64Utility.decode(text);
}
catch (Base64Exception e) {
throw new GeneralSecurityException("Cannot decrypt text [" + e.getMessage() + "]");
}
byte [] results = cipher.doFinal(deText);
return new String(results,"UTF-8");
}