首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#,编码/解码字符串到一些简单的代码,里面有校验和

C#,编码/解码字符串到一些简单的代码,里面有校验和
EN

Stack Overflow用户
提问于 2012-11-21 17:39:47
回答 2查看 5.1K关注 0票数 2

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

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

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

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

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

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

如果您能告诉我C#中最好的库,我将不胜感激,谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-21 19:03:12

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

代码语言:javascript
复制
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;
   }
}
票数 4
EN

Stack Overflow用户

发布于 2012-11-21 18:13:25

我用这段代码和rijndael进行了加密/解密。当触及加密字符串时,它会抛出CryptographicException。

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

代码语言:javascript
复制
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;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13490261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档