【Delphi】RDP文件中的密码Hash加解密单元
来源:互联网 发布:工程套价软件 编辑:程序博客网 时间:2024/06/11 18:04
RDP文件是微软远程桌面工具mstsc.exe用来记录登录信息的文件。有了RDP文件就可以方便的进行远程桌面连接
RDP文件中,密码需要加密后才可以使用。
这个单元实现了密码的加解密。注意要先安装JwAPI
RDP文件中,密码需要加密后才可以使用。
这个单元实现了密码的加解密。注意要先安装JwAPI
JwAPI的SVN地址:https://jedi-apilib.svn.sourceforge.net/svnroot/jedi-apilib/jwapi/trunk
{******************************************************************}{ Author: Remko Weijnen (r dot weijnen at gmail dot com) }{ Version: 0.1 }{ Date: 21-03-2007 }{ }{ The contents of this file are subject to }{ the Mozilla Public License Version 1.1 (the "License"); you may }{ not use this file except in compliance with the License. You may }{ obtain a copy of the License at }{ http://www.mozilla.org/MPL/MPL-1.1.html }{ }{ Software distributed under the License is distributed on an }{ "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }{ implied. See the License for the specific language governing }{ rights and limitations under the License. }{******************************************************************}unit RDPHash;interfaceuses Windows, Sysutils, JwaWinCrypt;function CryptRDPPassword(sPassword: string): string;function DecryptRDPPassword(sPasswordHash: string): string;function BlobDataToHexStr(P: PByte; I: Integer): string;function PasswordHashToBlobData(sPasswordHash: string): DATA_BLOB;implementation{***********************************************************}{ HexToByte: Converts Hex value to Byte }{ Found this somewhere on the internet }{***********************************************************}function HexToByte(s : String): Byte;const cs = '0123456789ABCDEF';begin result := 0; if (length(s) = 2) and //(s[1] in ['0'..'9','A'..'F']) and CharInSet(s[1], ['0'..'9','A'..'F']) and //(s[2] in ['0'..'9','A'..'F']) then CharInSet(s[2], ['0'..'9','A'..'F']) then result := ((pos(s[1],cs)-1) *16) + (pos(s[2],cs)-1) else raise EConvertError.CreateFmt('%s is not a Hexformatstring',[s]);end;{***********************************************************}{ PasswordHashToBlobData: Converts a RDP password Hash to }{ a DATA_BLOB structure }{ sPasswordHash : RDP Password Hash (HEX String }{***********************************************************}function PasswordHashToBlobData(sPasswordHash: string): DATA_BLOB;var Buf: array of Byte; dwBufSize: Cardinal; i: Cardinal; j: Cardinal; dwHashSize: Cardinal;begin dwBufSize := Length(sPassWordHash) DIV 2; dwHashSize := Length(sPasswordHash); SetLength(Buf, dwBufSize); i := 1; j := 0; while i < dwHashSize do begin Buf[j] := HexToByte(sPassWordHash[i] + sPassWordHash[i+1]); Inc(i, 2); Inc(j); end; GetMem(Result.pbData, dwBufSize); Result.cbData := dwBufSize; Result.pbData := PByte(Buf);end;{***********************************************************}{ BlobDataToHexStr: Converts a PByte from a DATA_BLOB }{ to a Hex String so it can be saved in }{ an RDP file }{ P : PByte (pbData) from DATA_BLOB }{ I : Integer (cbData) from DATA_BLOB }{***********************************************************}function BlobDataToHexStr(P: PByte; I: Integer): string;var HexStr: string;begin HexStr := ''; while (I > 0) do begin Dec(I); HexStr := HexStr + IntToHex(P^, 2); Inc(P); end; Result := HexStr;end;{***********************************************************}{ CryptRDPPassword: Converts a plaintext password to }{ encrypted password hash }{ an RDP file }{ sPassword: plaintext password }{***********************************************************}function CryptRDPPassword(sPassword: string): string;var DataIn: DATA_BLOB; DataOut: DATA_BLOB; pwDescription: PWideChar; PwdHash: string;begin PwdHash := ''; DataOut.cbData := 0; DataOut.pbData := nil; // RDP uses UniCode DataIn.pbData := Pointer(WideString(sPassword)); DataIn.cbData := Length(sPassword) * SizeOf(WChar); // RDP always sets description to psw pwDescription := WideString('psw'); if CryptProtectData(@DataIn, pwDescription, nil, nil, nil, CRYPTPROTECT_UI_FORBIDDEN, // Never show interface @DataOut) then begin PwdHash := BlobDataToHexStr(DataOut.pbData, DataOut.cbData); end; Result := PwdHash; // Cleanup LocalFree(Cardinal(DataOut.pbData)); LocalFree(Cardinal(DataIn.pbData));end;{***********************************************************}{ DecryptRDPPassword: Converts an RDP Password Hash back }{ to it's original password. }{ Note that this only works for the user}{ who encrypted the password (or on the }{ same computer in case it was encrypted}{ with the computerkey }{ sPasswordHash: Password hash (string) }{***********************************************************}function DecryptRDPPassword(sPasswordHash: string): string;var DataIn: DATA_BLOB; DataOut: DATA_BLOB; sPassword: string; pwDecrypted: PWideChar; pwDescription: PWideChar;begin DataIn := PasswordHashToBlobData(sPasswordHash); DataOut.cbData := 0; DataOut.pbData := nil; if CryptUnprotectData(@DataIn, @pwDescription, nil, nil, nil, CRYPTPROTECT_UI_FORBIDDEN, // Never show interface @DataOut) then begin Getmem(pwDecrypted, DataOut.cbData); lstrcpynW(pwDecrypted, PWideChar(DataOut.pbData), (DataOut.cbData DIV 2) + 1); sPassword := pwDecrypted; FreeMem(pwDecrypted); end else raise EConvertError.CreateFmt('Error decrypting: %s',[SysErrorMessage(GetLastError)]); Result := sPassword; // Cleanup if DataOut.cbData > 0 then LocalFree(Cardinal(DataOut.pbData));end;end.
- 【Delphi】RDP文件中的密码Hash加解密单元
- 生成rdp文件中的密码字串
- 远程桌面,RDP文件密码加密、解密算法(C#)
- 维吉尼亚密码加解密(no 文件加解密)
- spring 属性文件加密码及解密
- Delphi DES加密解密单元
- silverlight的MD5、对称加解密及字符串、文件HASH
- delphi中的单元
- weblogic密码加解密
- 培根密码加解密
- Delphi单元文件
- Delphi单元文件
- Delphi单元文件详解
- Delphi单元文件详解
- Delphi 单元文件结构
- Delphi单元文件详解
- Delphi单元文件
- Delphi单元文件详解
- visio中添加表格
- DBus如何使用原始DBus库传送和接收数据
- seajs的spm使用摸索
- Android应用中通过AIDL机制实现进程间的通讯实例
- redhat,ubuntu,Fedora搭建NFS
- 【Delphi】RDP文件中的密码Hash加解密单元
- Linux中查看某个软件的安装路径
- iPhone开发技巧之私有API — 设备相关信息
- SAP 采购订单税码增强检查
- lotus生成excel(一)
- 深度解析Java内存原型
- Android之Adapter用法总结
- SecurityContextHolder.getContext().getAuthentication()为空的解决
- 在UIBarButtonItem上添加UISegmentedControl