C#實現(xiàn)Windows Server遠程桌面RDP文件用戶的密碼加密、解密算法并自動登錄
當前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
背景:由于項目需要,使用RDP文件來遠程登錄,需要實現(xiàn)點擊rdp文件就可以自動連接遠程桌面,并且實現(xiàn)自動登錄功能!自動登錄!自動登錄!
自動登錄:密碼需要經(jīng)過加密,本文的核心!??!廢話少說,看代碼!
1、首先添加引用是必須的!
using System;
using System.Security.Cryptography;
2、核心算法
using System;
using System.Security.Cryptography;
static byte[] s_aditionalEntropy = null; //附加的加密因子,自定義
private void test()
{
string plainText = "qweR+-*yuioP0";
byte[] secret = Encoding.Unicode.GetBytes(plainText);
byte[] encryptedSecret = Protect(secret);
Console.WriteLine("The encrypted byte array is:");
string res = string.Empty;
foreach(byte b in encryptedSecret)
{
res += b.ToString("X2"); //炒雞坑爹的,轉(zhuǎn)換16進制的一定要用2位,不然就像我一樣被坑了半個多月
}
Console.WriteLine("加密之后的密碼:" + res);
PrintValues(encryptedSecret);
// Decrypt the data and store in a byte array.
byte[] originalData = Unprotect(encryptedSecret);
Console.WriteLine("{0}The original data is:", Environment.NewLine);
string str = Encoding.Default.GetString(originalData);
Console.WriteLine("解密之后的密碼: " + str);
PrintValues(originalData);
}
//加密方法
public static byte[] Protect(byte[] data)
{
try
{
// Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted
// only by the same current user.
return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
}
catch (CryptographicException e)
{
Console.WriteLine("Data was not encrypted. An error occurred.");
Console.WriteLine(e.ToString());
return null;
}
}
//解密方法
public static byte[] Unprotect(byte[] data)
{
try
{
//Decrypt the data using DataProtectionScope.CurrentUser.
return ProtectedData.Unprotect(data, s_aditionalEntropy, DataProtectionScope.LocalMachine);
}
catch (CryptographicException e)
{
Console.WriteLine("Data was not decrypted. An error occurred.");
Console.WriteLine(e.ToString());
return null;
}
}
public static void PrintValues(Byte[] myArr)
{
foreach (Byte i in myArr)
{
Console.Write("\t{0}", i);
}
Console.WriteLine();
}
3、總結(jié)!
生成的字節(jié)數(shù)組直接轉(zhuǎn)換成16進制輸出,添加到rdp文件的密碼里面就會解析不出來。
坑爹的轉(zhuǎn)換成16進制時需要占位2位,不然就出錯,弄了N久才搞定,發(fā)文共勉!轉(zhuǎn)載請注明出處,謝謝!
4、附RDP文件的一些注釋(轉(zhuǎn))
screen mode id:i:1
desktopwidth:i:1280
desktopheight:i:750
session bpp:i:24
winposstr:s:2,3,188,8,1062,721
full address:s:MyServer //服務(wù)器地址 一般填I(lǐng)P地址
compression:i:1
keyboardhook:i:2
audiomode:i:0
redirectdrives:i:0
redirectprinters:i:0
redirectcomports:i:0
redirectsmartcards:i:0
displayconnectionbar:i:1
autoreconnection enabled:i:1
username:s:MyUserName //用戶名
domain:s:MyDomain //域名 可以不填。
alternate shell:s:
shell working directory:s:
password 51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB0100000052A9E191EA75A948B359790578C9371A0000000008000000700073007700000003660000A8000000100000000A1DCCD2E50775CA25EC3857164B34DC0000000004800000A000000010000000FCE1A645B9B61AA450946BB6F955058108020000D83591CA47562D6DDAA689F050AE145039EBE22E00D1D3AEAA98373C7B63C3E8E7149072DF989EA43EFCE20513AD3D27B11BE7F17066A688E1DCE828AF85460AAC327B38E90776DB962888E4393D19637578984B19A187AAD95F6D2726ADE7DD315FF56C15FF5B3031014EDDCC3C24D1B81779AFDB006EE575F5BEFB8D2D2138D9D9D642BBB251CC5ED7226968764856EC660A646BACE748A13D6002A9A537AA70710615650B9387EED66DE28BD57B304BBDD7B581B943DA628EB0289E30A8BA784B76F7885BECCAB4FEF7820E97EE3C6E036EEAF6EAA669288DF2FCACC9BEC045C907EBBDE87AFB8CC6B07A600BD63AC891B61D95C2265DD9FD5E635D61BFBF5EDC28311375066611C610FB533D64515B643C82F57D9B183B05C156D91BC0974D38E546022B139E82452E6F1EDF76E52F732C3904E5E433F8F3D488DB0698427DBB0791A9F207F8CB6654CB8410BAF4A59C4F9E821E589ABC1E6E6E1D432181B690408F6884FE1007895A4D26D4A5A2C7458EE747DA35D44AC9FB08AB5477EA3E7CCDB3E37EE20FAFD0D0CF9584E420598B7003B347943AC28048F45E0FD21AD08148FFADCE0E7877219259A7BE722FFAE845A429BA2CF0A71F2D19EA7495530FABDB5106E8D404A38A7E6394C38457640EA7398C5D55F0C4D342CC6A39C77E10A2A5145AEA40B14F5C7C3760334D83C9BE748383FADE231248537353817D51F7B44F61B406ABC61400000071C354139F458B02D978015F785B97F7F6B307380 //這么一長串就是密碼了,“password 51:b:”后面改為你自己生成的密碼串
disable wallpaper:i:1
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:0
disable cursor setting:i:0
bitmapcachepersistenable:i:1
該文章在 2021/6/17 22:57:59 編輯過
|
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |