C#,将字符串编码/解码为一些带有校验和的简单代码

时间:2012-11-21 09:39:47

标签: c# asp.net encryption

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

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

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

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

我需要获得一个可以检查一致性的代码(因此它应该包括控制数字/校验和),因此我可以告诉该字段由第三方保持不变并且可以有效地解码。 (像信用卡号码内的控制数字一样)。

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

如果您指出我在C#内最好的图书馆,我感谢您,提前感谢!

2 个答案:

答案 0 :(得分:4)

RC4是一种简单的算法,可以轻松实现加密/解密。它不如AES安全,但似乎在您的情况下您不需要AES安全性。以下是来自this site

的.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;
   }
}

答案 1 :(得分: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;
    }