远程桌面,RDP文件密码加密、解密算法(C#)

本文介绍了如何使用C#进行RDP文件的密码加密和解密,以实现远程桌面的自动登录功能。关键在于添加相关引用并应用核心加密算法。转换加密后的字节数组为16进制时需要注意占位问题,否则可能导致错误。作者分享经验,希望对读者有所帮助。
摘要由CSDN通过智能技术生成

背景:由于项目需要,使用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文件的密码里面就会解析不出

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值