远程桌面rdp密码java_java rdp 远程桌面 密码加密和解码 代码

这是一个使用Java实现RDP密码加密和解密的代码示例,利用了JNA库来调用Windows的Crypt32.dll进行操作。程序接收命令行参数,根据'C'或'D'进行加密或解码操作,并将结果输出。
摘要由CSDN通过智能技术生成

用于动态实现对电脑的远程桌面连接。

package tools;

import com.sun.jna.*;

import com.sun.jna.platform.win32.*;

import com.sun.jna.Pointer;

import java.util.Formatter;

import java.io.UnsupportedEncodingException;

import java.nio.charset.Charset;

import java.io.*;

import java.util.Arrays;

import org.apache.commons.cli.*;

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 = args[0];

password = args[1];

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;

}

}

下面是VB的,不知道加密后,无法解码,看不出哪里不对,知道的请指导一下。

Option Explicit

Private Declare Function CryptProtectData Lib "Crypt32.dll" (pDataIn As DATA_BLOB, ByVal szDataDescr As Any, ByRef Entrophy As Any, ByVal pvReserved As Long, ByVal pPromptStruct As Long, ByVal dwFlags As Long, pDataOut As DATA_BLOB) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Declare Sub LocalFree Lib "kernel32.dll" (ByVal M As Long)

'MultiByteToWideChar 是多字节字符到宽字符转换函数

Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long

Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long

'常用的代码页:

Const cpUTF8 = 65001

Const cpGB2312 = 936

Const cpGB18030 = 54936

Const cpUTF7 = 65000

Private Type DATA_BLOB

cbData As Long 'size

pbData As Long 'data

getData() As Byte

End Type

Public Function CryptRDPPassword1(spPassword As String) As String

Dim pDataIn As DATA_BLOB

Dim pDataEncrypted As DATA_BLOB

Dim aDataIn() As Byte

Dim pwdBytes() As Byte

Dim r As Long

Dim s$, i&, s1$

aDataIn = UTF16ToMultiByte(StrConv(spPassword, vbUnicode), cpUTF8)

pDataIn.pbData = StrPtr(aDataIn(0)) '

pDataIn.cbData = UBound(aDataIn) + 1

r = CryptProtectData(pDataIn, "psw", ByVal vbNullString, 0, 0, 1, pDataEncrypted)

If r Then

ReDim pwdBytes(pDataEncrypted.cbData) As Byte '

Call CopyMemory(pwdBytes(0), ByVal pDataEncrypted.pbData, pDataEncrypted.cbData) '

s = ""

For i = 0 To UBound(pwdBytes) - 1

s = s & Format(Hex(pwdBytes(i)), "00") '转换16进制的一定要用2位

Next

CryptRDPPassword1 = s

Else

CryptRDPPassword1 = "Nothing"

End If

LocalFree pDataIn.pbData '释放内存

End Function

Private Sub Command1_Click()

Dim l

l = InputBox("vbWide", , CryptRDPPassword1("111111"))

End Sub

Function UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte()

Dim bufSize As Long

Dim arr() As Byte

bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0)

ReDim arr(bufSize - 1)

WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0

UTF16ToMultiByte = arr

End Function

Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String

Dim bufSize As Long

bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0)

MultiByteToUTF16 = Space(bufSize)

MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize

End Function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值