我正在尝试将passphase编码为属性文件,这样我就不必输入passphase来建立SSH连接。但我面临以下错误:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at com.test.ssh.SSH_Public_Private.deCryptPwd(SSH_Public_Private.java:191)
at com.test.ssh.SSH_Public_Private.checkHostName(SSH_Public_Private.java:227)
at com.test.ssh.SSH_Public_Private.checkHostName(SSH_Public_Private.java:223)
at com.test.ssh.SSH_Public_Private.connectToSSH(SSH_Public_Private.java:64)
at com.test.ssh.SSH_Public_Private.main(SSH_Public_Private.java:124)
我的代码如下:
private String checkHostName(String hostUserName) throws IOException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String deCrypted = null;
FileInputStream is = new FileInputStream(new File("C:\\test\\SSH\\PrivateKey\\keystore.properties"));
Properties properties = new Properties();
properties.load(is);
ssh_Public_Private = new SSH_Public_Private();
boolean isHostNameExist = false;
if (properties.getProperty(hostUserName) == null) {
OutputStream outputStream = new FileOutputStream(
"C:\\test\\SSH\\PrivateKey\\keystore.properties");
String passPhraseStored = new String(enCryptPwd());
properties.setProperty(hostUserName,passPhraseStored );
properties.store(outputStream, null);
outputStream.close();
is.close();
return checkHostName(hostUserName);
}else{
System.out.println(properties.getProperty(hostUserName));
String passPhrase = properties.getProperty(hostUserName);
deCrypted = deCryptPwd(passPhrase); //isHostNameExist = true;
}
return deCrypted;
}
My encryption and decryption piece of code is as follow :
private static String enCryptPwd() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
String decrypted = null;
byte[] encrypted = null;
try {
String text = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter your passphrase : " );
text = sc.next();
String key = "Bar12345Bar12345"; // 128 bit key
//String key = "AesSEcREtkeyABCD";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
System.out.println(aesKey.getFormat());
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
encrypted = cipher.doFinal(text.getBytes("UTF-8"));
System.err.println(new String(encrypted));
System.err.println(encrypted.length);
} catch (Exception e) {
e.printStackTrace();
}
return new String(encrypted);
}
private static String deCryptPwd(String encrypted) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String originalString = "";
try {
String key = "Bar12345Bar12345"; // 128 bit key
//String key = "AesSEcREtkeyABCD";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES");
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] encryptBytes = new byte[encrypted.length()];
encryptBytes = encrypted.getBytes();
byte[] decrypted = cipher.doFinal(encryptBytes);
originalString = new String(decrypted, "UTF-8");
System.out.println(originalString);
System.err.println(decrypted);
} catch (Exception e) {
e.printStackTrace();
}
return originalString;
}
我一直在尝试阅读,并且我已经通过不包括填充算法尝试了许多其他方法。我的代码将输出写入属性文件:
abced = Y \ u201Eh \ uFFFD \ u00EC - :\ u00F9 \ u00F8mC \ u0160 \ U0002 \ u00F3#\ u00DE
我的控制台输出是:
输入您的密码: ABC!@#
加密后>> Y“ħI-:ùømCŠó#Þ
16
从属性文件中读取>> Y“ħI-:ùømCŠó#Þ
答案 0 :(得分:2)
使用KeyGenerator生成密钥
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128, new SecureRandom());
SecretKeySpec aesKey = new SecretKeySpec(kg.generateKey().getEncoded(), "AES");
使用AES / CBC / PKCS5Padding获取密码实例。
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
这应该有用。