将字节转换为ascii然后将字符串转换为缺少字符

时间:2017-04-29 18:46:29

标签: c# ascii

此程序通过给定密码打开隐藏文件夹。如果您是第一次运行此程序,则用户应输入“' new'创建一个保存其密码和目录路径的帐户,然后,当他再次运行并输入密码时,该目录将在文件资源管理器上打开。我的问题是,当我输入'我尝试通过我的程序打开它,随机目录打开。

发生了什么:
输入: C:\ Users \ joao_.DESKTOP-QMLOLSI \ Desktop \ League of Legend 输出:C:\ Users

会发生什么:
输入: C:\ Users \ joao_.DESKTOP-QMLOLSI \ Desktop \ League of Legend 输出 C:\ Users \ joao_.DESKTOP-QMLOLSI \ Desktop \ League of Legends

我的代码:https://pastebin.com/qHeygbjM
输入:第18~23行 输出第71行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication3
{
    class Program
    {
        static void createAcc() //method to create a account
        {
            string[] a = new string[2];
            Console.Write("Digite uma nova senha(password): ");
            a[0] = Console.ReadLine();//user's password
            Console.Write("Entre com o caminho escondido(folder path): ");
            string s = Console.ReadLine(); user's path
            Console.Write(s);
            byte[] bytes = Encoding.ASCII.GetBytes(s);// converting to bytes, so I can 'encrypt' the path
            long result = BitConverter.ToInt64(bytes, 0);
            a[1] = result.ToString();
            File.WriteAllLines("MyFile.txt", a); //hold the data in a txt file
            Console.Write("Conta criada com sucesso. Feche e abra novamente e insira sua senha.");
            Console.ReadKey();
        }
        static bool checkPassword(string input)
        {
            try
            {
                string[] linesI = File.ReadAllLines("MyFile.txt");
                if (linesI[0] == input)
                {
                    return true;
                }
                else
                {
                    Console.Write("Senha errada(Wrong password)");
                    Console.ReadKey();
                    return false;
                }
            }
            catch (FileNotFoundException e)
            {
                Console.Write("Arquivo 'MyFiles.txt' não existe ou conta não criada. Fecha e abra novamente.");
                Console.ReadKey();
                return false;
            }
        }
        static void Main(string[] args)
        {
            string a;
            Console.WriteLine("Senha(digite new para criar uma nova conta(new to a new account)):");
            a = Console.ReadLine();
            if (a == "new")
            {
                createAcc();
            }
            else
            if (checkPassword(a))
            {
                //    byte[] bytes = Encoding.ASCII.GetBytes(a);
                //int i = BitConverter.ToInt32(bytes, 0);
                //string[] linesI = File.ReadAllLines("MyFile.txt");
                //linesI[1] = i.ToString();
                //File.WriteAllLines("MyFile.txt", linesI);
                string[] linesO = File.ReadAllLines("MyFile.txt"); 
                long output = Convert.ToInt64(linesO[1]);
                byte[] bytes = BitConverter.GetBytes(output);
                string s2 = Encoding.ASCII.GetString(bytes);
                Console.Write(s2);
                Console.ReadKey();
                Process.Start("explorer.exe", s2); //if the password is right, the path should open, but it doesnt
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

没关系,我想出了一种更好的加密用户路径的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace ConsoleApplication3
{
    class Program
    {
        static void createAcc()
        {
            string[] a = new string[2];
            Console.Write("Digite uma nova senha(password): ");
            a[0] = Console.ReadLine();
            Console.Write("Entre com o caminho escondido(folder path): ");
            string s = Console.ReadLine();
            a[1] = encryptsOrDecrypts(s,'e');
            File.WriteAllLines("MyFile.txt", a);
            Console.Write("Conta criada com sucesso. Feche e abra novamente e insira sua senha.");
            Console.ReadKey();
        }

        static string encryptsOrDecrypts(string a, char mode)
        {
            if (mode == 'e')
            {
                string encrypted = "";
                foreach (char s in a)
                {
                    encrypted += (Convert.ToInt32(s) + 1) + ",";

                }
                encrypted = encrypted.Remove(encrypted.Length - 1);
                return encrypted;
            }else{

                string decrypted = "";
                string[] str = a.Split(',');
                for(int i = 0; i < str.Length; i++)
                {
                    decrypted += (char)(Convert.ToInt32(str[i]) - 1);

                }
                return decrypted;
            }

        }
        static bool checkPassword(string input)
        {
            try
            {
                string[] linesI = File.ReadAllLines("MyFile.txt");
                if (linesI[0] == input)
                {
                    return true;
                }
                else
                {
                    Console.Write("Senha errada(Wrong password)");
                    Console.ReadKey();
                    return false;
                }
            }
            catch (FileNotFoundException e)
            {
                Console.Write("Arquivo 'MyFiles.txt' não existe ou conta não criada. Fecha e abra novamente.");
                Console.ReadKey();
                return false;
            }
        }
        static void Main(string[] args)
        {
            string a;
            Console.WriteLine("Senha(digite new para criar uma nova conta(new to a new account)):");
            a = Console.ReadLine();
            if (a == "new")
            {
                createAcc();
            }
            else
            if (checkPassword(a))
            {

                //File.WriteAllLines("MyFile.txt", linesI);
                string[] linesO = File.ReadAllLines("MyFile.txt");
                string output = encryptsOrDecrypts(linesO[1], 'd'); 
                Console.ReadKey();
                Process.Start("explorer.exe", output);
            }
        }
    }
}