我創(chuàng)建了一個(gè) RSA 密鑰對(duì)?,F(xiàn)在,我正在嘗試使用 DES 算法加密私鑰,將其格式化為 PKCS#5 并將其打印在控制臺(tái)上。不幸的是,生成的私鑰不起作用。當(dāng)我嘗試使用它時(shí),輸入正確的密碼后,ssh 客戶端返回密碼無(wú)效:加載密鑰“test.key”:用于解密私鑰的密碼不正確可以請(qǐng)有人告訴我我錯(cuò)在哪里嗎?這是代碼:private byte[] iv;public void generate() throws Exception { RSAKeyPairGenerator generator = new RSAKeyPairGenerator(); generator.initialize(2048); KeyPair keyPair = generator.generateKeyPair(); String passphrase = "passphrase"; byte[] encryptedData = encrypt(keyPair.getPrivate().getEncoded(), passphrase); System.out.println(getPrivateKeyPem(Base64.encodeBase64String(encryptedData)));}private byte[] encrypt(byte[] data, String passphrase) throws Exception { String algorithm = "PBEWithMD5AndDES"; salt = new byte[8]; int iterations = 1024; // Create a key from the supplied passphrase. KeySpec ks = new PBEKeySpec(passphrase.toCharArray()); SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm); SecretKey key = skf.generateSecret(ks); // Create the salt from eight bytes of the digest of P || M. MessageDigest md = MessageDigest.getInstance("MD5"); md.update(passphrase.getBytes()); md.update(data); byte[] digest = md.digest(); System.arraycopy(digest, 0, salt, 0, 8); AlgorithmParameterSpec aps = new PBEParameterSpec(salt, iterations); Cipher cipher = Cipher.getInstance(AlgorithmID.pbeWithSHAAnd3_KeyTripleDES_CBC.getJcaStandardName()); cipher.init(Cipher.ENCRYPT_MODE, key, aps); iv = cipher.getIV(); byte[] output = cipher.doFinal(data); ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(salt); out.write(output); out.close(); return out.toByteArray();}
2 回答

森欄
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
先生
我認(rèn)為在調(diào)用 encrypt 之前,出于安全原因,您需要再解密兩次。也可以用胡椒鹽和胡椒代替鹽。不要將算法與 aes256 混合。
添加回答
舉報(bào)
0/150
提交
取消