如何加密Windows身份值?

发布于 2025-01-23 07:42:00 字数 351 浏览 0 评论 0原文

我正在使用

 x = System.Security.Principal.WindowsIdentity.GetCurrent().User.Value;

输出用户 Windows Identity 的输出。 这是输出外观的示例

S-5-2-51-37518325-1542148351-9136563645-5621

我目前将其发送到.txt文件并阅读它,但是您可能会告诉任何经验丰富的人都可以访问文本文件,所以我如何将这些值争夺到它们不再能够追溯到其原始数字的点。

I'm using

 x = System.Security.Principal.WindowsIdentity.GetCurrent().User.Value;

which outputs the users windows identity.
Here is an example of what the output looks like:

S-5-2-51-37518325-1542148351-9136563645-5621

I'm currently sending this to a .txt file and reading it but as you can probably tell anyone with a bit of experience can access the text file so how would I scramble these values to the point they are no longer able get traced back to their original numbers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与之呼应 2025-01-30 07:42:00

我使用此类:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class Crypto
{
    private static readonly byte[] Salt =
        { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };

    /// <summary>
    /// Cifra una cadena texto con el algoritmo de Rijndael.
    /// </summary>
    /// <param name="text">Texto a cifrar.</param>
    /// <param name="password">Clave de cifrado</param>
    /// <returns>Texto cifrado.</returns>
    public static string Encrypt(string text, string password)
    {
        using (var rijndael = RijndaelCreate(password))
        using (var memoryStream = new MemoryStream())
        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
        {
            // Obtener la representación en bytes de la información a cifrar
            var plainMessageBytes = UTF8Encoding.UTF8.GetBytes(text);

            // Cifrar los datos enviándolos al flujo de cifrado
            cryptoStream.Write(plainMessageBytes, 0, plainMessageBytes.Length);
            cryptoStream.FlushFinalBlock();

            // Obtener los datos datos cifrados como bytes
            var cipherMessageBytes = memoryStream.ToArray();
            return Convert.ToBase64String(cipherMessageBytes);
        }
    }

    /// <summary>
    /// Cifra una cadena texto con el algoritmo de Rijndael.
    /// </summary>
    /// <param name="text">Texto a cifrar.</param>
    /// <param name="key">Clave del cifrado para Rijndael</param>
    /// <param name="iv">Vector de inicio para Rijndael</param>
    /// <returns>Texto cifrado.</returns>
    public static string Encrypt(string text, byte[] key, byte[] iv)
    {
        using (var rijndael = RijndaelCreate())
        using (var memoryStream = new MemoryStream())
        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(key, iv), CryptoStreamMode.Write))
        {
            // Obtener la representación en bytes de la información a cifrar
            var plainMessageBytes = UTF8Encoding.UTF8.GetBytes(text);

            // Cifrar los datos enviándolos al flujo de cifrado
            cryptoStream.Write(plainMessageBytes, 0, plainMessageBytes.Length);
            cryptoStream.FlushFinalBlock();

            // Obtener los datos datos cifrados como bytes
            var cipherMessageBytes = memoryStream.ToArray();
            return Convert.ToBase64String(cipherMessageBytes);
        }
    }

    /// <summary>
    /// Descifra una cadena texto con el algoritmo de Rijndael.
    /// </summary>
    /// <param name="encrypted">Mensaje cifrado.</param>
    /// <param name="password">Clave de cifrado</param>
    /// <returns>Texto descifrado.</returns>
    public static string Decrypt(string encrypted, string password)
    {
        var cipherTextBytes = Convert.FromBase64String(encrypted);

        using (var rijndael = RijndaelCreate(password))
        using (var memoryStream = new MemoryStream(cipherTextBytes))
        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
        {
            var plainTextBytes = new byte[cipherTextBytes.Length];
            var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        }
    }

    /// <summary>
    /// Descifra una cadena texto con el algoritmo de Rijndael.
    /// </summary>
    /// <param name="encrypted">Mensaje cifrado.</param>
    /// <param name="key">Clave del cifrado para Rijndael</param>
    /// <param name="iv">Vector de inicio para Rijndael</param>
    /// <returns>Texto descifrado.</returns>
    public static string Decrypt(string encrypted, byte[] key, byte[] iv)
    {
        var cipherTextBytes = Convert.FromBase64String(encrypted);

        using (var rijndael = RijndaelCreate())
        using (var memoryStream = new MemoryStream(cipherTextBytes))
        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(key, iv), CryptoStreamMode.Read))
        {
            var plainTextBytes = new byte[cipherTextBytes.Length];
            var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        }
    }

    /// <summary>
    /// Cifra una cadena texto con el algoritmo de Rijndael y lo almacena en un archivo.
    /// </summary>
    /// <param name="text">Texto a cifrar.</param>
    /// <param name="filename">Ruta al archivo.</param>
    /// <param name="key">Clave del cifrado para Rijndael</param>
    /// <param name="iv">Vector de inicio para Rijndael</param>
    public static void EncryptToFile(string text, string filename, byte[] key, byte[] iv)
    {
        using (var rijndael = RijndaelCreate())
        using (var fileStream = File.Open(filename, FileMode.OpenOrCreate))
        using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateEncryptor(key, iv), CryptoStreamMode.Write))
        using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
        {
            streamWriter.WriteLine(text);
        }
    }

    /// <summary>
    /// Cifra una cadena texto con el algoritmo de Rijndael y lo almacena en un archivo.
    /// </summary>
    /// <param name="text">Texto a cifrar.</param>
    /// <param name="filename">Ruta al archivo.</param>
    /// <param name="password">Clave de cifrado</param>
    public static void EncryptToFile(string text, string filename, string password)
    {
        using (var rijndael = RijndaelCreate(password))
        using (var fileStream = File.Open(filename, FileMode.OpenOrCreate))
        using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
        using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
        {
            streamWriter.WriteLine(text);
        }
    }

    /// <summary>
    /// Descifra el contenido de un archivo con el algoritmo de Rijndael y lo retorna como una 
    /// cadena de texto plano.
    /// </summary>
    /// <param name="filename">Ruta al archivo.</param>
    /// <param name="key">Clave del cifrado para Rijndael</param>
    /// <param name="iv">Vector de inicio para Rijndael</param>
    /// <returns>Texto descifrado.</returns>
    public static string DecryptFromFile(string filename, byte[] key, byte[] iv)
    {
        using (var rijndael = RijndaelCreate())
        using (var fileStream = File.Open(filename, FileMode.OpenOrCreate))
        using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateDecryptor(key, iv), CryptoStreamMode.Read))
        using (var streamReader = new StreamReader(cryptoStream))
        {
            return streamReader.ReadLine();
        }
    }

    /// <summary>
    /// Descifra el contenido de un archivo con el algoritmo de Rijndael y lo retorna como una 
    /// cadena de texto plano.
    /// </summary>
    /// <param name="filename">Ruta al archivo.</param>
    /// <param name="password">Clave de cifrado</param>
    /// <returns>Texto descifrado.</returns>
    public static string DecryptFromFile(string filename, string password)
    {
        using (var rijndael = RijndaelCreate(password))
        using (var fileStream = File.Open(filename, FileMode.OpenOrCreate))
        using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
        using (var streamReader = new StreamReader(cryptoStream))
        {
            return streamReader.ReadLine();
        }
    }

    private static RijndaelManaged RijndaelCreate(string password = null)
    {
        var rijndael = new RijndaelManaged();

        if (!string.IsNullOrEmpty(password))
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, Salt);
            rijndael.Key = pdb.GetBytes(32);
            rijndael.IV = pdb.GetBytes(16);
        }

        return rijndael;
    }
}

示例:

var textToEncrypt = "Some content to encrypt";
var encoded = Crypto.Encrypt(textToEncrypt, "Some-Key-To-Encrypt");
var decoded = Crypto.Decrypt(encoded, "Some-Key-To-Encrypt")

I use this class:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class Crypto
{
    private static readonly byte[] Salt =
        { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };

    /// <summary>
    /// Cifra una cadena texto con el algoritmo de Rijndael.
    /// </summary>
    /// <param name="text">Texto a cifrar.</param>
    /// <param name="password">Clave de cifrado</param>
    /// <returns>Texto cifrado.</returns>
    public static string Encrypt(string text, string password)
    {
        using (var rijndael = RijndaelCreate(password))
        using (var memoryStream = new MemoryStream())
        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
        {
            // Obtener la representación en bytes de la información a cifrar
            var plainMessageBytes = UTF8Encoding.UTF8.GetBytes(text);

            // Cifrar los datos enviándolos al flujo de cifrado
            cryptoStream.Write(plainMessageBytes, 0, plainMessageBytes.Length);
            cryptoStream.FlushFinalBlock();

            // Obtener los datos datos cifrados como bytes
            var cipherMessageBytes = memoryStream.ToArray();
            return Convert.ToBase64String(cipherMessageBytes);
        }
    }

    /// <summary>
    /// Cifra una cadena texto con el algoritmo de Rijndael.
    /// </summary>
    /// <param name="text">Texto a cifrar.</param>
    /// <param name="key">Clave del cifrado para Rijndael</param>
    /// <param name="iv">Vector de inicio para Rijndael</param>
    /// <returns>Texto cifrado.</returns>
    public static string Encrypt(string text, byte[] key, byte[] iv)
    {
        using (var rijndael = RijndaelCreate())
        using (var memoryStream = new MemoryStream())
        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(key, iv), CryptoStreamMode.Write))
        {
            // Obtener la representación en bytes de la información a cifrar
            var plainMessageBytes = UTF8Encoding.UTF8.GetBytes(text);

            // Cifrar los datos enviándolos al flujo de cifrado
            cryptoStream.Write(plainMessageBytes, 0, plainMessageBytes.Length);
            cryptoStream.FlushFinalBlock();

            // Obtener los datos datos cifrados como bytes
            var cipherMessageBytes = memoryStream.ToArray();
            return Convert.ToBase64String(cipherMessageBytes);
        }
    }

    /// <summary>
    /// Descifra una cadena texto con el algoritmo de Rijndael.
    /// </summary>
    /// <param name="encrypted">Mensaje cifrado.</param>
    /// <param name="password">Clave de cifrado</param>
    /// <returns>Texto descifrado.</returns>
    public static string Decrypt(string encrypted, string password)
    {
        var cipherTextBytes = Convert.FromBase64String(encrypted);

        using (var rijndael = RijndaelCreate(password))
        using (var memoryStream = new MemoryStream(cipherTextBytes))
        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
        {
            var plainTextBytes = new byte[cipherTextBytes.Length];
            var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        }
    }

    /// <summary>
    /// Descifra una cadena texto con el algoritmo de Rijndael.
    /// </summary>
    /// <param name="encrypted">Mensaje cifrado.</param>
    /// <param name="key">Clave del cifrado para Rijndael</param>
    /// <param name="iv">Vector de inicio para Rijndael</param>
    /// <returns>Texto descifrado.</returns>
    public static string Decrypt(string encrypted, byte[] key, byte[] iv)
    {
        var cipherTextBytes = Convert.FromBase64String(encrypted);

        using (var rijndael = RijndaelCreate())
        using (var memoryStream = new MemoryStream(cipherTextBytes))
        using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(key, iv), CryptoStreamMode.Read))
        {
            var plainTextBytes = new byte[cipherTextBytes.Length];
            var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        }
    }

    /// <summary>
    /// Cifra una cadena texto con el algoritmo de Rijndael y lo almacena en un archivo.
    /// </summary>
    /// <param name="text">Texto a cifrar.</param>
    /// <param name="filename">Ruta al archivo.</param>
    /// <param name="key">Clave del cifrado para Rijndael</param>
    /// <param name="iv">Vector de inicio para Rijndael</param>
    public static void EncryptToFile(string text, string filename, byte[] key, byte[] iv)
    {
        using (var rijndael = RijndaelCreate())
        using (var fileStream = File.Open(filename, FileMode.OpenOrCreate))
        using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateEncryptor(key, iv), CryptoStreamMode.Write))
        using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
        {
            streamWriter.WriteLine(text);
        }
    }

    /// <summary>
    /// Cifra una cadena texto con el algoritmo de Rijndael y lo almacena en un archivo.
    /// </summary>
    /// <param name="text">Texto a cifrar.</param>
    /// <param name="filename">Ruta al archivo.</param>
    /// <param name="password">Clave de cifrado</param>
    public static void EncryptToFile(string text, string filename, string password)
    {
        using (var rijndael = RijndaelCreate(password))
        using (var fileStream = File.Open(filename, FileMode.OpenOrCreate))
        using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write))
        using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
        {
            streamWriter.WriteLine(text);
        }
    }

    /// <summary>
    /// Descifra el contenido de un archivo con el algoritmo de Rijndael y lo retorna como una 
    /// cadena de texto plano.
    /// </summary>
    /// <param name="filename">Ruta al archivo.</param>
    /// <param name="key">Clave del cifrado para Rijndael</param>
    /// <param name="iv">Vector de inicio para Rijndael</param>
    /// <returns>Texto descifrado.</returns>
    public static string DecryptFromFile(string filename, byte[] key, byte[] iv)
    {
        using (var rijndael = RijndaelCreate())
        using (var fileStream = File.Open(filename, FileMode.OpenOrCreate))
        using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateDecryptor(key, iv), CryptoStreamMode.Read))
        using (var streamReader = new StreamReader(cryptoStream))
        {
            return streamReader.ReadLine();
        }
    }

    /// <summary>
    /// Descifra el contenido de un archivo con el algoritmo de Rijndael y lo retorna como una 
    /// cadena de texto plano.
    /// </summary>
    /// <param name="filename">Ruta al archivo.</param>
    /// <param name="password">Clave de cifrado</param>
    /// <returns>Texto descifrado.</returns>
    public static string DecryptFromFile(string filename, string password)
    {
        using (var rijndael = RijndaelCreate(password))
        using (var fileStream = File.Open(filename, FileMode.OpenOrCreate))
        using (var cryptoStream = new CryptoStream(fileStream, rijndael.CreateDecryptor(), CryptoStreamMode.Read))
        using (var streamReader = new StreamReader(cryptoStream))
        {
            return streamReader.ReadLine();
        }
    }

    private static RijndaelManaged RijndaelCreate(string password = null)
    {
        var rijndael = new RijndaelManaged();

        if (!string.IsNullOrEmpty(password))
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, Salt);
            rijndael.Key = pdb.GetBytes(32);
            rijndael.IV = pdb.GetBytes(16);
        }

        return rijndael;
    }
}

Example:

var textToEncrypt = "Some content to encrypt";
var encoded = Crypto.Encrypt(textToEncrypt, "Some-Key-To-Encrypt");
var decoded = Crypto.Decrypt(encoded, "Some-Key-To-Encrypt")
听你说爱我 2025-01-30 07:42:00

我的解决方案:

首先创建两个字符串:

 string HWID;
 string encodedData = String.Empty;

然后使您的“ hwid”等于某种形式的Windows硬件/身份。

 HWID = System.Security.Principal.WindowsIdentity.GetCurrent().User.Value;
       

在它的下方,您将需要使用此信息进行加密:

 byte[] data_byte = Encoding.UTF8.GetBytes(HWID);
            encodedData = HttpUtility.UrlEncode(Convert.ToBase64String(data_byte));

要查看它是否有效,

Console.WriteLine(encodedData);

请让我知道是否有任何效率更高或不风险曝光的方法?谢谢 :)

My solution:

Create two strings first called:

 string HWID;
 string encodedData = String.Empty;

Then make your "HWID" equal some form of windows hardware/identity.

 HWID = System.Security.Principal.WindowsIdentity.GetCurrent().User.Value;
       

Underneath it you will want to encrypt using this:

 byte[] data_byte = Encoding.UTF8.GetBytes(HWID);
            encodedData = HttpUtility.UrlEncode(Convert.ToBase64String(data_byte));

And to see your if it worked just type:

Console.WriteLine(encodedData);

Please let me know if there is any ways that are either more efficient or not risk exposure? thanks :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文