Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#,将字符串编码/解码为一些简单的代码,内部带有校验和_C#_Asp.net_Encryption - Fatal编程技术网

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

C#,将字符串编码/解码为一些简单的代码,内部带有校验和,c#,asp.net,encryption,C#,Asp.net,Encryption,我需要在C#中实现字符串的简单编码和解码 我不需要“高度机密”加密,所以简单的编码只要得到校验和就可以了 我需要在一个代码实例内进行简单的双向编码和解码。只有一个程序同时进行编码和解码,所以任何“密钥”都可以硬编码 编码结果应为字母数字(文本)。避免使用二进制数据(非字母、非数字),因为字段将参与文本CSV表中的文件 我需要得到一个可以检查一致性的代码(所以它应该包括控制数字/校验和),这样我就可以知道第三方没有更改字段,并且可以有效解码。(类似于信用卡号内的控制数字) 代码长度应近似于原始字符

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

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

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

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

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

代码长度应近似于原始字符串的长度(不是5-10倍)


如果您能告诉我C#内最好的图书馆,我将不胜感激,提前谢谢

我使用此代码对rijndael进行加密/解密。当触碰加密字符串时,它抛出加密异常

方法从


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

公共静态类RC4
{
公共静态字符串加密(字符串密钥、字符串数据)
{
Encoding unicode=Encoding.unicode;
返回Convert.ToBase64String(加密(unicode.GetBytes(key)、unicode.GetBytes(data));
}
公共静态字符串解密(字符串密钥、字符串数据)
{
Encoding unicode=Encoding.unicode;
返回unicode.GetString(加密(unicode.GetBytes(密钥),Convert.FromBase64String(数据));
}
公共静态字节[]加密(字节[]密钥,字节[]数据)
{
返回EncryptOutput(密钥、数据).ToArray();
}
公共静态字节[]解密(字节[]密钥,字节[]数据)
{
返回EncryptOutput(密钥、数据).ToArray();
}
私有静态字节[]EncryptInitialize(字节[]密钥)
{
字节[]s=可枚举范围(0,256)
.选择(i=>(字节)i)
.ToArray();
对于(int i=0,j=0;i<256;i++)
{
j=(j+key[i%key.Length]+s[i])&255;
互换(s、i、j);
}
返回s;
}
专用静态IEnumerable EncryptOutput(字节[]键,IEnumerable数据)
{
字节[]s=加密初始化(密钥);
int i=0;
int j=0;
返回数据。选择((b)=>
{
i=(i+1)和255;
j=(j+s[i])&255;
互换(s、i、j);
返回(字节)(b^s[(s[i]+s[j])&255]);
});
}
专用静态无效交换(字节[]s,int i,int j)
{
字节c=s[i];
s[i]=s[j];
s[j]=c;
}
}

您对“encode”/“decode”的使用-我能说清楚吗:我们这里说的是“encrypt”/“decrypt”,也许是签名?请注意,大多数加密都会输出一个原始字节流,但您可以对其进行base-64编码以将其表示为字符串(它会稍微大一点)。是的,加密和校验和(因此我知道该字符串未被触及)。Base64 bin转换为文本很好。我认为您混淆了编码和加密。你需要的是加密。请参阅,不要重复正确的答案,而依靠填充而不是加密校验和是一个非常糟糕的主意。也不建议使用PasswordDeriveBytes,对于大于哈希函数的输出(SHA-1为160位)是不安全的
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;
    }
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;
   }
}