[英]Java - Cannot encrypt Windows Remote Desktop password in RDP file
我正在嘗試以編程方式在計算機上創建 RDP 文件。 我從 PROPERTIES 文件中獲取用戶名和密碼,並嘗試使用CryptProtectData()
將其加密為 vlaid 格式。 然后我生成字符串password 51:b:<encrypted password>
並將其存儲在 .RDP 文件中。
當我查看 RDP 文件時,我得到類似於以下內容的 output: password 51:b:[B@3fd83fd8
看這里: http://www.remkoweijnen.nl/blog/2007/10/18/how-rdp-passwords-are-encrypted/你可以看到密碼格式不正確。
順便說一句,為了進行加密,我正在使用導入: import com.sun.jna.platform.win32.Crypt32Util;
從這段代碼中可以看出訪問Crypt32Util.cryptProtectData(passwordBytes)
:
FileWriter fstream = new FileWriter(rdpFile);
BufferedWriter out = new BufferedWriter(fstream);
out.write("full address:s:"+remoteServerIP);
out.write("\nusername:s:"+username);
byte[] passwordBytes = password.getBytes();
out.write("\npassword 51:b:"+Crypt32Util.cryptProtectData(passwordBytes));
如果有人能幫助我正確加密密碼,我將不勝感激。
謝謝你。
附言,我使用的是 Windows XP
編輯:我發現了這個關於使用 C/C++ 加密的信息,我查看了 wincrypt.h 但我無法識別任何有用的東西: http://blogs.msdn.com/b/rds/archive/2007/01/22/vista -remote-desktop-connection-authentication-faq.aspx
通過查看您的鏈接,您似乎缺少將字節數組(將 CryptUtil 應用於密碼的結果)轉換為十六進制字符串表示的步驟:
out.write("\npassword 51:b:"+ ToHexString(Crypt32Util.cryptProtectData(passwordBytes)));
ToHexString(byte[] barray):String 看起來像這樣:
public static String ToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
for (byte b : bytes) {
formatter.format("%02x", b);
}
return sb.toString();
}
String paasword ="pwd";
DATA_BLOB pDataIn = new DATA_BLOB(password.getBytes(Charset.forName("UTF-16LE")));
DATA_BLOB pDataEncrypted = new 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());
這是我的工作解決方案(你需要 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";
}
}
我昨天剛剛找到解決這個問題的方法。這是錯誤的,因為windows(c ++)和java之間的差異。我可以將密碼加密並自動填充到rdp文件中,然后登錄遠程桌面而無需再次填寫密碼.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.