2

我需要在 C# 中实现字符串的简单编码和解码。

我不需要“高机密”加密,所以简单的编码只要得到校验和就可以了。

我需要在一个代码实例中进行简单的双向编码解码。只有一个程序同时进行编码和解码,因此任何“密钥”都可以硬编码。

编码结果应该是字母数字(文本)。应避免二进制数据(非字母、非数字),因为字段将参​​与作为文本 CSV 表的文件。

我需要一个可以检查一致性的代码(因此它应该包括控制数字/校验和),所以我可以知道该字段没有被第三方更改并且可以有效解码。(类似于信用卡号码中的控制数字)。

代码长度应与原始字符串的长度大致相似(而不是长 5-10 倍)。

如果您将我指向 C# 中最好的库,我将不胜感激,在此先感谢!

4

2 回答 2

4

RC4 是一种简单的算法,可以很容易地实现加密/解密。它不如 AES 安全,但在您的情况下,您似乎不需要 AES 安全性。下面是来自这个站点的 .NET 中 RC4 的实现

public static class RC4
{
   public static string Encrypt(string key, string data)
   {
      Encoding unicode = Encoding.Unicode;

      return Convert.ToBase64String(Encrypt(unicode.GetBytes(key), unicode.GetBytes(data)));
   }

   public static string Decrypt(string key, string data)
   {
      Encoding unicode = Encoding.Unicode;

      return unicode.GetString(Encrypt(unicode.GetBytes(key), Convert.FromBase64String(data)));
   }

   public static byte[] Encrypt(byte[] key, byte[] data)
   {
      return EncryptOutput(key, data).ToArray();
   }

   public static byte[] Decrypt(byte[] key, byte[] data)
   {
      return EncryptOutput(key, data).ToArray();
   }

   private static byte[] EncryptInitalize(byte[] key)
   {
      byte[] s = Enumerable.Range(0, 256)
        .Select(i => (byte)i)
        .ToArray();

      for (int i = 0, j = 0; i < 256; i++)
      {
         j = (j + key[i % key.Length] + s[i]) & 255;

         Swap(s, i, j);
      }

      return s;
   }

   private static IEnumerable<byte> EncryptOutput(byte[] key, IEnumerable<byte> data)
   {
      byte[] s = EncryptInitalize(key);

      int i = 0;
      int j = 0;

      return data.Select((b) =>
      {
         i = (i + 1) & 255;
         j = (j + s[i]) & 255;

         Swap(s, i, j);

         return (byte)(b ^ s[(s[i] + s[j]) & 255]);
      });
   }

   private static void Swap(byte[] s, int i, int j)
   {
      byte c = s[i];

      s[i] = s[j];
      s[j] = c;
   }
}
于 2012-11-21T11:03:11.650 回答
1

我已使用此代码与 rijndael 进行加密/解密。触摸加密字符串时会引发 CryptographicException。

来自http://www.codeproject.com/Articles/5719/Simple-encrypting-and-decrypting-data-in-C的方法

public string Encrypt(string clearText, string Password)
    {
        //Convert text to bytes
        byte[] clearBytes =
          System.Text.Encoding.Unicode.GetBytes(clearText);

        //We will derieve our Key and Vectore based on following 
        //password and a random salt value, 13 bytes in size.
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
            new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
        0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

        byte[] encryptedData = Encrypt(clearBytes,
                 pdb.GetBytes(32), pdb.GetBytes(16));

        return Convert.ToBase64String(encryptedData);
    }

    //Call following function to decrypt data
    public string Decrypt(string cipherText, string Password)
    {
        //Convert base 64 text to bytes
        byte[] cipherBytes = Convert.FromBase64String(cipherText);

        //We will derieve our Key and Vectore based on following 
        //password and a random salt value, 13 bytes in size.
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
            new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 
        0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
        byte[] decryptedData = Decrypt(cipherBytes,
            pdb.GetBytes(32), pdb.GetBytes(16));

        //Converting unicode string from decrypted data
        return Encoding.Unicode.GetString(decryptedData);
    }

    public byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
    {
        byte[] encryptedData;
        //Create stream for encryption
        using (MemoryStream ms = new MemoryStream())
        {
            //Create Rijndael object with key and vector
            using (Rijndael alg = Rijndael.Create())
            {
                alg.Key = Key;
                alg.IV = IV;
                //Forming cryptostream to link with data stream.
                using (CryptoStream cs = new CryptoStream(ms,
                   alg.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    //Write all data to stream.
                    cs.Write(clearData, 0, clearData.Length);
                }
                encryptedData = ms.ToArray();
            }
        }
        return encryptedData;
    }

    public byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
    {
        byte[] decryptedData;
        //Create stream for decryption
        using (MemoryStream ms = new MemoryStream())
        {
            //Create Rijndael object with key and vector
            using (Rijndael alg = Rijndael.Create())
            {
                alg.Key = Key;
                alg.IV = IV;
                //Forming cryptostream to link with data stream.
                using (CryptoStream cs = new CryptoStream(ms,
                    alg.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    //Write all data to stream.
                    cs.Write(cipherData, 0, cipherData.Length);
                }
                decryptedData = ms.ToArray();
            }
        }
        return decryptedData;
    }
于 2012-11-21T10:13:24.833 回答