gpt4 book ai didi

c# - 简单的不安全双向数据 "obfuscation"?

转载 作者:IT王子 更新时间:2023-10-29 03:27:38 26 4
gpt4 key购买 nike

我正在为某些数据寻找非常简单的混淆(如加密和解密但不一定安全)功能。这不是关键任务。我需要一些东西让诚实的人保持诚实,但比 ROT13 强一点的东西或 Base64 .

我更喜欢 .NET 中已经包含的内容framework 2.0,所以我不必担心任何外部依赖。

我真的不想弄乱公钥/私钥等。我对加密知之甚少,但我知道我写的任何东西都不会毫无值(value)......事实上,我可能会搞砸数学,让它变得微不足道。

最佳答案

这里的其他答案工作正常,但 AES 是一种更安全和最新的加密算法。这是我几年前获得的一个类,用于执行 AES 加密,我随着时间的推移对其进行了修改,以便对 Web 应用程序更加友好(例如,我已经构建了使用 URL 友好字符串的加密/解密方法)。它还具有处理字节数组的方法。

注意:您应该在 Key(32 字节)和 Vector(16 字节)数组中使用不同的值!您不希望别人通过假设您按原样使用此代码来找出您的 key !您所要做的就是更改 Key 和 Vector 数组中的一些数字(必须 <= 255)(我在 Vector 数组中留下了一个无效值以确保您这样做...)。您可以使用 https://www.random.org/bytes/轻松生成新集:

使用它很简单:只需实例化类,然后(通常)调用 EncryptToString(string StringToEncrypt) 和 DecryptString(string StringToDecrypt) 作为方法。一旦您拥有此类,它就不会变得更容易(或更安全)。


using System;
using System.Data;
using System.Security.Cryptography;
using System.IO;


public class SimpleAES
{
// Change these keys
private byte[] Key = __Replace_Me__({ 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 });

// a hardcoded IV should not be used for production AES-CBC code
// IVs should be unpredictable per ciphertext
private byte[] Vector = __Replace_Me__({ 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 2521, 112, 79, 32, 114, 156 });


private ICryptoTransform EncryptorTransform, DecryptorTransform;
private System.Text.UTF8Encoding UTFEncoder;

public SimpleAES()
{
//This is our encryption method
RijndaelManaged rm = new RijndaelManaged();

//Create an encryptor and a decryptor using our encryption method, key, and vector.
EncryptorTransform = rm.CreateEncryptor(this.Key, this.Vector);
DecryptorTransform = rm.CreateDecryptor(this.Key, this.Vector);

//Used to translate bytes to text and vice versa
UTFEncoder = new System.Text.UTF8Encoding();
}

/// -------------- Two Utility Methods (not used but may be useful) -----------
/// Generates an encryption key.
static public byte[] GenerateEncryptionKey()
{
//Generate a Key.
RijndaelManaged rm = new RijndaelManaged();
rm.GenerateKey();
return rm.Key;
}

/// Generates a unique encryption vector
static public byte[] GenerateEncryptionVector()
{
//Generate a Vector
RijndaelManaged rm = new RijndaelManaged();
rm.GenerateIV();
return rm.IV;
}


/// ----------- The commonly used methods ------------------------------
/// Encrypt some text and return a string suitable for passing in a URL.
public string EncryptToString(string TextValue)
{
return ByteArrToString(Encrypt(TextValue));
}

/// Encrypt some text and return an encrypted byte array.
public byte[] Encrypt(string TextValue)
{
//Translates our text value into a byte array.
Byte[] bytes = UTFEncoder.GetBytes(TextValue);

//Used to stream the data in and out of the CryptoStream.
MemoryStream memoryStream = new MemoryStream();

/*
* We will have to write the unencrypted bytes to the stream,
* then read the encrypted result back from the stream.
*/
#region Write the decrypted value to the encryption stream
CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write);
cs.Write(bytes, 0, bytes.Length);
cs.FlushFinalBlock();
#endregion

#region Read encrypted value back out of the stream
memoryStream.Position = 0;
byte[] encrypted = new byte[memoryStream.Length];
memoryStream.Read(encrypted, 0, encrypted.Length);
#endregion

//Clean up.
cs.Close();
memoryStream.Close();

return encrypted;
}

/// The other side: Decryption methods
public string DecryptString(string EncryptedString)
{
return Decrypt(StrToByteArray(EncryptedString));
}

/// Decryption when working with byte arrays.
public string Decrypt(byte[] EncryptedValue)
{
#region Write the encrypted value to the decryption stream
MemoryStream encryptedStream = new MemoryStream();
CryptoStream decryptStream = new CryptoStream(encryptedStream, DecryptorTransform, CryptoStreamMode.Write);
decryptStream.Write(EncryptedValue, 0, EncryptedValue.Length);
decryptStream.FlushFinalBlock();
#endregion

#region Read the decrypted value from the stream.
encryptedStream.Position = 0;
Byte[] decryptedBytes = new Byte[encryptedStream.Length];
encryptedStream.Read(decryptedBytes, 0, decryptedBytes.Length);
encryptedStream.Close();
#endregion
return UTFEncoder.GetString(decryptedBytes);
}

/// Convert a string to a byte array. NOTE: Normally we'd create a Byte Array from a string using an ASCII encoding (like so).
// System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
// return encoding.GetBytes(str);
// However, this results in character values that cannot be passed in a URL. So, instead, I just
// lay out all of the byte values in a long string of numbers (three per - must pad numbers less than 100).
public byte[] StrToByteArray(string str)
{
if (str.Length == 0)
throw new Exception("Invalid string value in StrToByteArray");

byte val;
byte[] byteArr = new byte[str.Length / 3];
int i = 0;
int j = 0;
do
{
val = byte.Parse(str.Substring(i, 3));
byteArr[j++] = val;
i += 3;
}
while (i < str.Length);
return byteArr;
}

// Same comment as above. Normally the conversion would use an ASCII encoding in the other direction:
// System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
// return enc.GetString(byteArr);
public string ByteArrToString(byte[] byteArr)
{
byte val;
string tempStr = "";
for (int i = 0; i <= byteArr.GetUpperBound(0); i++)
{
val = byteArr[i];
if (val < (byte)10)
tempStr += "00" + val.ToString();
else if (val < (byte)100)
tempStr += "0" + val.ToString();
else
tempStr += val.ToString();
}
return tempStr;
}
}

关于c# - 简单的不安全双向数据 "obfuscation"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/165808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com