CSharp 中的加密代码类似于 Go 中的代码(AES、CFB、XorKeyStream)

我在 Go 中有加密代码,但在 CSharp 中很难找到类似的代码。我正在讨论如何实现我自己的 XorKeyStream,但有人告诉我,如果我编写自己的加密代码,则存在法律问题。我相信 CSharp 中一定有类似的代码。


package main


import (

    "crypto/aes"

    "crypto/cipher"

    "fmt"

)


func main() {


    k1 := []byte("0123456789abcdef")

    r1 := []byte("1234567890abcdef")

    data := []byte("0123456789")

    fmt.Printf("original %x %s\n", data, string(data))


    {

        block, _ := aes.NewCipher(k1)

        stream := cipher.NewCFBEncrypter(block, r1)

        stream.XORKeyStream(data, data)

        fmt.Printf("crypted %x\n", data)

    }

    {

        block, _ := aes.NewCipher(k1)

        stream := cipher.NewCFBDecrypter(block, r1)

        stream.XORKeyStream(data, data)

        fmt.Printf("decrypted %x %s\n", data, string(data))

    }


}

http://play.golang.org/p/EnJ56dYX_-


输出


original 30313233343536373839 0123456789

crypted 762b6dcea9c2a7460db7

decrypted 30313233343536373839 0123456789

PS 有些人将该问题标记为可能与问题重复:“C# AES:加密文件导致“要加密的数据长度无效”。错误”我在 CSharp 中寻找与 Go 中现有代码相同的代码。这个问题是关于填充的。该算法需要将异或文本的“密钥流”。这是不同的问题。


当年话下
浏览 158回答 2
2回答

PIPIONE

这是你的代码using System;using System.Text;using System.Security.Cryptography;using System.IO;class AES_CFB_XorKeyStream{    static void Main(string[] args)    {        byte[] data = Encoding.UTF8.GetBytes("0123456789");        byte [] k1 = Encoding.UTF8.GetBytes("0123456789abcdef");        byte [] r1 = Encoding.UTF8.GetBytes("1234567890abcdef");        Console.WriteLine("original " + BitConverter.ToString(data));        using (RijndaelManaged Aes128 = new RijndaelManaged())        {            Aes128.BlockSize = 128;            Aes128.KeySize = 128;            Aes128.Mode = CipherMode.CFB;            Aes128.FeedbackSize = 128;            Aes128.Padding = PaddingMode.None;            Aes128.Key = k1;            Aes128.IV = r1;            using (var encryptor = Aes128.CreateEncryptor())            using (var msEncrypt = new MemoryStream())            using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))            using (var bw = new BinaryWriter(csEncrypt, Encoding.UTF8))            {                bw.Write(data);                bw.Close();                data = msEncrypt.ToArray();                Console.WriteLine("crypted " + BitConverter.ToString(data));            }        }        using (RijndaelManaged Aes128 = new RijndaelManaged())        {            Aes128.BlockSize = 128;            Aes128.KeySize = 128;            Aes128.Mode = CipherMode.CFB;            Aes128.FeedbackSize = 128;            Aes128.Padding = PaddingMode.None;            Aes128.Key = k1;            Aes128.IV = r1;            using (var decryptor = Aes128.CreateDecryptor())            using (var msEncrypt = new MemoryStream())            using (var csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Write))            using (var bw = new BinaryWriter(csEncrypt, Encoding.UTF8))            {                bw.Write(data);                bw.Close();                data = msEncrypt.ToArray();                Console.WriteLine("decrypted " + BitConverter.ToString(data));            }        }    }}输出original 30-31-32-33-34-35-36-37-38-39crypted 76-2B-6D-CE-A9-C2-A7-46-0D-B7decrypted 30-31-32-33-34-35-36-37-38-39

千万里不及你

我遇到了完全相同的问题,只有每个解密块的第一个字节是正确的,但我没有能够在 Go 程序上更改源代码的奢侈。我最终实现了自己的填充。只需用 0 字节填充加密字节,使其可被 128 的块大小整除,然后在运行解密例程后,从末尾切掉该字节数。示例代码:using System;using System.Text;using System.Security.Cryptography;using System.Linq;public static class Program{&nbsp; &nbsp; static RijndaelManaged aes = new RijndaelManaged(){&nbsp; &nbsp; &nbsp; &nbsp; Mode = CipherMode.CFB,&nbsp; &nbsp; &nbsp; &nbsp; BlockSize = 128,&nbsp; &nbsp; &nbsp; &nbsp; KeySize = 128,&nbsp; &nbsp; &nbsp; &nbsp; FeedbackSize = 128,&nbsp; &nbsp; &nbsp; &nbsp; Padding = PaddingMode.None&nbsp; &nbsp; };&nbsp; &nbsp; public static void Main(){&nbsp; &nbsp; &nbsp; &nbsp; byte[] key = Encoding.UTF8.GetBytes("0123456789abcdef");&nbsp; &nbsp; &nbsp; &nbsp; byte[] iv = Encoding.UTF8.GetBytes("1234567890abcdef");&nbsp; &nbsp; &nbsp; &nbsp; byte[] encryptedBytes = new byte[]{0x76, 0x2b, 0x6d, 0xce, 0xa9, 0xc2, 0xa7, 0x46, 0x0d, 0xb7};&nbsp; &nbsp; &nbsp; &nbsp; // Custom pad the bytes&nbsp; &nbsp; &nbsp; &nbsp; int padded;&nbsp; &nbsp; &nbsp; &nbsp; encryptedBytes = PadBytes(encryptedBytes, aes.BlockSize, out padded);&nbsp; &nbsp; &nbsp; &nbsp; // Decrypt bytes&nbsp; &nbsp; &nbsp; &nbsp; byte[] decryptedBytes = DecryptBytesAES(encryptedBytes, key, iv, encryptedBytes.Length);&nbsp; &nbsp; &nbsp; &nbsp; // Check for successful decrypt&nbsp; &nbsp; &nbsp; &nbsp; if(decryptedBytes != null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Unpad&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decryptedBytes = UnpadBytes(decryptedBytes, padded);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Decrypted: " + Encoding.UTF8.GetString(decryptedBytes));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Just an elegant way of initializing an array with bytes&nbsp; &nbsp; public static byte[] Initialize(this byte[] array, byte value, int length)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < array.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i] = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return array;&nbsp; &nbsp; }&nbsp; &nbsp; // Custom padding to get around the issue of how Go uses CFB mode without padding differently than C#&nbsp; &nbsp; public static byte[] PadBytes(byte[] encryptedBytes, int blockSize, out int numPadded)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; numPadded = 0;&nbsp; &nbsp; &nbsp; &nbsp; // Check modulus of block size&nbsp; &nbsp; &nbsp; &nbsp; int mod = encryptedBytes.Length % blockSize;&nbsp; &nbsp; &nbsp; &nbsp; if (mod != 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Calculate number to pad&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numPadded = blockSize - mod;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Build array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return encryptedBytes.Concat(new byte[numPadded].Initialize(0, numPadded)).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // No padding needed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return encryptedBytes;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static byte[] UnpadBytes(byte[] decryptedBytes, int numPadded)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(numPadded != 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] unpaddedBytes = new byte[decryptedBytes.Length - numPadded];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(decryptedBytes, unpaddedBytes, unpaddedBytes.Length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return unpaddedBytes;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return decryptedBytes;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static byte[] DecryptBytesAES(byte[] cipherText, byte[] Key, byte[] IV, int size)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; byte[] array = new byte[size];&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aes.Key = Key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aes.IV = IV;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(cipherText))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cryptoStream.Read(array, 0, size);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return array;&nbsp; &nbsp; }}.NET 小提琴:https : //dotnetfiddle.net/NPHKN3
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go