C#实现Windows Server远程桌面RDP文件用户的密码加密、解密算法并自动登录
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
背景:由于项目需要,使用RDP文件来远程登录,需要实现点击rdp文件就可以自动连接远程桌面,并且实现自动登录功能!自动登录!自动登录!
自动登录:密码需要经过加密,本文的核心!!!废话少说,看代码!
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"); //炒鸡坑爹的,转换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、总结!
生成的字节数组直接转换成16进制输出,添加到rdp文件的密码里面就会解析不出来。
坑爹的转换成16进制时需要占位2位,不然就出错,弄了N久才搞定,发文共勉!转载请注明出处,谢谢!
4、附RDP文件的一些注释(转)
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 //服务器地址 一般填IP地址
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 编辑过
|
关键字查询
相关文章
正在查询... |