Java 对密码短语进行编码

Java 对密码短语进行编码,java,encryption,aes,Java,Encryption,Aes,我正在尝试将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.doF

我正在尝试将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“h”-:ùmCŠ

十六,


从属性文件中读取>>Y“h”-:ùmC#Þ

使用密钥生成器生成密钥

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")

这应该行。

您的编码已关闭。如果计算“加密后”显示的字符数,您会注意到只有15个字符,而不是预期的16个字符。请注意,加密不是基于字节的。加密数据几乎总是不可打印为文本,它显示为8位随机字节数组。为了显示(如问题中所述),请对加密数据进行十六进制编码。如果对“passphase”进行加密,您将如何保护该加密密钥?一般建议:始终使用完全限定的密码字符串<代码>Cipher.getInstance(“AES”)可能会产生不同的密码。它很可能会导致添加AES/ECB/PKCS5P“,但不必如此。如果它发生更改,您将失去不同JVM之间的兼容性。请不要使用。它是确定性的,因此在语义上不安全。您至少应该使用随机模式,如或。最好是对密文进行身份验证,这样就不可能进行类似的攻击。这可以通过像GCM或EAX这样的认证模式来实现,也可以通过一个方案来实现。如前所述,它会更干净。我更愿意用59201e683fec2d3af9f86d43160f323de来代替Y“h”:ùmC#Þ。@zaph true。编辑了答案。现在我得到了如下错误:
java.security.InvalidKeyException:com.sun.crypto.provider.CipherCore.init(CipherCore.java:460)中缺少参数,javax.crypto.Cipher.chooseProvider中缺少参数(Cipher.java:864)在javax.crypto.Cipher.init(Cipher.java:1249)在javax.crypto.Cipher.init(Cipher.java:1186)在
@dodger请使用KeyGenerator避免InvalidKeyException错误。更新的答案。我已经能够使用。谢谢。。