java实现远程登录RDP
java实现远程登录RDP
package com.clustertech.cloud.gui.utils;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Formatter;
import com.sun.jna.platform.win32.Crypt32;
import com.sun.jna.platform.win32.Crypt32Util;
import com.sun.jna.platform.win32.WinCrypt;
public class RDP {
private static String Str0;
private static String Str;
private static String JJ;
private static String password;
public static void main(String[] args) {
JJ = "C";
password = "lsfadmin";
if ("C".equals (JJ)){//编码
Str0=cryptRdpPassword0(password);
System.out.println(Str0);
Str=cryptRdpPassword(password);
System.out.println(Str);
} else if ("D".equals (JJ)) {//解码
Str=DecodeRdpPassword(password);
System.out.println(Str);
}
}
// ----------------------------------------------------------------------------------------
private static String cryptRdpPassword0(String password) {
WinCrypt.DATA_BLOB pDataIn = new WinCrypt.DATA_BLOB(password.getBytes(Charset.forName("UTF-16LE")));
WinCrypt.DATA_BLOB pDataEncrypted = new WinCrypt.DATA_BLOB();
Crypt32.INSTANCE.CryptProtectData(pDataIn, "psw", null, null, null, 1, 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);
}
return epwsb.toString();
}
private static String cryptRdpPassword(String password) {
try {
return ToHexString(Crypt32Util.cryptProtectData(password.getBytes(("UTF-16LE"))));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "ERROR";
}
}
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 static String DecodeRdpPassword(String password) {
try {
return new String(Crypt32Util.cryptUnprotectData(toBytes(password)), "UTF-16LE");
} catch (Exception e1) {
e1.printStackTrace();
return "ERROR";
}
}
private static byte[] toBytes(String str) {// 去掉0x以后,转整数再转型成字节
if (str == null || str.trim().equals("")) {
return new byte[0];
}
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < str.length() / 2; i++) {
String subStr = str.substring(i * 2, i * 2 + 2);
bytes[i] = (byte) Integer.parseInt(subStr, 16);
}
return bytes;
}
}
authentication level:i:0
prompt for credentials:i:0
negotiate security layer:i:1
shell working directory:s:
promptcredentialonce:i:0
full address:s:myip
username:s:domain\user
domain:s:"domain"
password 51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB01000000595A425AAFDB2341B639E38BA374A754000000000200000000001066000000010000200000008BDADBFD7A90B3C489A1B4E6E03287374894EE6AE543105ADBD469F3DBC1A2D5000000000E80000000020000200000007DBD60C58398DC2EC880CA1DFA6141821F4C7161D7C63290BACFE2BE23909310200000002A9691FD65BB0E1CBE0BE1EC962B680E9D9E673E97E8712269195669F50A785E40000000DADA106405174449CC1FF0C54FF5C3FDD55A045382FF5402D3C8F9D3C405162250D223384B1EC06AD2EF188A453D837FDBDC28A7B21B23D904747F3D6C51A59E
alternate shell:s:tscon 3
无需输入密码连接成功。
https://www.remkoweijnen.nl/blog/2007/10/18/how-rdp-passwords-are-encrypted/
https://blog.csdn.net/u013084748/article/details/81126261