我正在使用BouncyCastle安全提供程序来加密我的密码。然后将加密的密码存储在数据库表中。任何人都知道如何从数据库中获取密码时解密此密码。这是我的示例代码,仅在它们位于一个jframe中时才有效。
public String encrypt(String textToEncrypt) {
String encryptedpassword = null;
try {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
password1 = textToEncrypt.getBytes();
SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
cipherpass = Cipher.getInstance("DES/CTR/NoPadding", "BC");
cipherpass.init(Cipher.ENCRYPT_MODE, key, ivSpec);
cipherTextPassword = new byte[cipherpass.getOutputSize(password1.length)];
passLength = cipherpass.update(password1, 0, password1.length, cipherTextPassword, 0);
passLength += cipherpass.doFinal(cipherTextPassword, passLength);
encryptedpassword = new String(cipherTextPassword);
} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException ex) {
}
return encryptedpassword;
}
byte[] password1;
byte[] emailpass1;
byte[] keyBytes = "12345678".getBytes();
byte[] ivBytes = "input123".getBytes();
SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipherEmailpass, cipherpass;
byte[] cipherTextPassword;
byte[] cipherEmailTextPassword;
int passLength, emailpassLength;