2013-06-30 96 views
1

我已經嘗試從SO的另一個解決方案,如:加密RDP密碼與Java

String password ="pwd"; 
     WinCrypt.DATA_BLOB pDataIn = new WinCrypt.DATA_BLOB(password.getBytes(Charset.forName("UTF-16LE"))); 
     WinCrypt.DATA_BLOB pDataEncrypted = new WinCrypt.DATA_BLOB(); 
     System.out.println(Crypt32.INSTANCE.CryptProtectData(pDataIn, "psw", 
       null, null, null, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, pDataEncrypted)); 
     StringBuffer epwsb = new StringBuffer(); 
     byte[] pwdBytes= new byte [pDataEncrypted.cbData]; 
     pwdBytes=pDataEncrypted.getData(); 
     Formatter formatter = new Formatter(epwsb); 
     for (final byte b : pwdBytes) { 
      formatter.format("%02X", b); 
     } 
     System.out.println("password 51:b:"+ epwsb.toString()); 

Crypt32Util.cryptProtectData("12345".getBytes("UTF-16LE"), null, 0, "psw", null); 

但他們都得到不同的結果對於我每次運行它們時,並且它們與由MSTSC保存或由RDP Password Hasher實用程序生成的真實密碼不匹配。 有誰知道可以加密密碼的解決方案或CLI實用程序?

+0

「所有的人都給予了我每次運行它們時不同的結果」 < - 你確定你是不是產生這個密碼的_hash_而不是加密的形式?如果它是一個散列,那麼它可以隨着時間改變是非常正常的,這個散列是鹽漬的...... – fge

回答

1

這是我的工作溶液(您需要JNA平臺,得到這個工作):

private static String ToHexString(byte[] bytes) { 
     StringBuilder sb = new StringBuilder(); 
     Formatter formatter = new Formatter(sb); 
     for (byte b : bytes) { 
      formatter.format("%02x", b); 
     } 
     formatter.close(); 
     return sb.toString(); 
    } 

    private String cryptRdpPassword(String pass) { 
     try { 
      return ToHexString(Crypt32Util.cryptProtectData(pass.getBytes("UTF-16LE"), null, 0, "psw", null)); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
      return "ERROR"; 
     } 
    }