我必须将与另一个数据库的连接详细信息存储在数据库表中,我必须加密这些数据库的密码,并且必须能够通过 SQL 脚本“手动”将数据插入该表......
我需要加密和解密它,因为我的应用程序必须能够使用这些数据并连接到其他数据库,所以 MD5 和类似的没有用..
我想到了 Blowfish、AES 等...但是如果我将密码作为 VARCHAR 存储在数据库中,则解密部分不起作用...所以我将其存储为 BYTE,但如果我这样做,没有人可以编写脚本在表上预加载数据..
也许我在这里遗漏了一些东西......
这是我在表中的注册表定义为 VARCHAR 时使用的代码:
package main;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class Prueba {
private static final String keyValue = "fd<[;.7e/OC0W!d|";
private static final String ALG = "Blowfish";
public static void main(String[] args) {
String text = "some random text";
try {
SecretKeySpec key = new SecretKeySpec(keyValue.getBytes(), ALG);
Cipher cipher = Cipher.getInstance(ALG);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(text.getBytes());
String encrypted = new String(encryptedBytes);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes = cipher.doFinal(encrypted.getBytes());
String recovered = new String(recoveredBytes);
} catch (NoSuchAlgorithmException nsa) {
nsa.printStackTrace();
} catch (NoSuchPaddingException nspe) {
nspe.printStackTrace();
} catch (InvalidKeyException ike) {
ike.printStackTrace();
} catch (BadPaddingException bpe) {
bpe.printStackTrace();
} catch (IllegalBlockSizeException ibse) {
ibse.printStackTrace();
}
}
}
我得到了例外:
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at main.Prueba.main(Prueba.java:30)
如果不是:
byte[] recoveredBytes = cipher.doFinal(encrypted.getBytes());
我愿意
byte[] recoveredBytes = cipher.doFinal(encryptedBytes);
我也不例外,但是我必须将密码存储为 byte[] soooo 没有脚本可能......
有任何想法吗?