ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – RijndaelManaged的AES OFB加密

2019-06-29 00:52:29  阅读:291  来源: 互联网

标签:c net aes rijndaelmanaged


我需要通过OFB模式下的加密消息从C#应用程序通信到另一个应用程序.我知道RijndaelManaged不支持AES OFB模式.是否有人比我更了解使用OFB模式加密/解密的任何其他方式?

解决方法:

以下流通过使用由零馈送CBC密码流生成的密钥流来实现OFB.

public class OFBStream : Stream
{
    private const int BLOCKS = 16;
    private const int EOS = 0; // the goddess of dawn is found at the end of the stream

    private Stream parent;
    private CryptoStream cbcStream;
    private CryptoStreamMode mode;
    private byte[] keyStreamBuffer;
    private int keyStreamBufferOffset;
    private byte[] readWriteBuffer;

    public OFBStream (Stream parent, SymmetricAlgorithm algo, CryptoStreamMode mode)
    {
        if (algo.Mode != CipherMode.CBC)
            algo.Mode = CipherMode.CBC;
        if (algo.Padding != PaddingMode.None)
            algo.Padding = PaddingMode.None;
        this.parent = parent;
        this.cbcStream = new CryptoStream (new ZeroStream (), algo.CreateEncryptor (), CryptoStreamMode.Read);
        this.mode = mode;
        keyStreamBuffer = new byte[algo.BlockSize * BLOCKS];
        readWriteBuffer = new byte[keyStreamBuffer.Length];
    }

    public override int Read (byte[] buffer, int offset, int count)
    {
        if (!CanRead) {
            throw new NotSupportedException ();
        }

        int toRead = Math.Min (count, readWriteBuffer.Length);
        int read = parent.Read (readWriteBuffer, 0, toRead);
        if (read == EOS)
            return EOS;

        for (int i = 0; i < read; i++) {
            // NOTE could be optimized (branches for each byte)
            if (keyStreamBufferOffset % keyStreamBuffer.Length == 0) {
                FillKeyStreamBuffer ();
                keyStreamBufferOffset = 0;
            }

            buffer [offset + i] = (byte)(readWriteBuffer [i]
                ^ keyStreamBuffer [keyStreamBufferOffset++]);
        }

        return read;
    }

    public override void Write (byte[] buffer, int offset, int count)
    {
        if (!CanWrite) {
            throw new NotSupportedException ();
        }

        int readWriteBufferOffset = 0;
        for (int i = 0; i < count; i++) {
            if (keyStreamBufferOffset % keyStreamBuffer.Length == 0) {
                FillKeyStreamBuffer ();
                keyStreamBufferOffset = 0;
            }

            if (readWriteBufferOffset % readWriteBuffer.Length == 0) {
                parent.Write (readWriteBuffer, 0, readWriteBufferOffset);
                readWriteBufferOffset = 0;
            }

            readWriteBuffer [readWriteBufferOffset++] = (byte)(buffer [offset + i]
                ^ keyStreamBuffer [keyStreamBufferOffset++]);
        }

        parent.Write (readWriteBuffer, 0, readWriteBufferOffset);
    }

    private void FillKeyStreamBuffer ()
    {
        int read = cbcStream.Read (keyStreamBuffer, 0, keyStreamBuffer.Length);
        // NOTE undocumented feature
        // only works if keyStreamBuffer.Length % blockSize == 0
        if (read != keyStreamBuffer.Length)
            throw new InvalidOperationException ("Implementation error: could not read all bytes from CBC stream");
    }

    public override bool CanRead {
        get { return mode == CryptoStreamMode.Read; }
    }

    public override bool CanWrite {
        get { return mode == CryptoStreamMode.Write; }
    }

    public override void Flush ()
    {
        // should never have to be flushed, implementation empty
    }

    public override bool CanSeek {
        get { return false; }
    }

    public override long Seek (long offset, System.IO.SeekOrigin origin)
    {
        throw new NotSupportedException ();
    }

    public override long Position {
        get { throw new NotSupportedException (); }
        set { throw new NotSupportedException (); }
    }

    public override long Length {
        get { throw new NotSupportedException (); }
    }

    public override void SetLength (long value)
    {
        throw new NotSupportedException ();
    }

}

OFBStream需要额外的类ZeroStream

class ZeroStream : System.IO.Stream
{
    public override int Read (byte[] buffer, int offset, int count)
    {
        for (int i = 0; i < count; i++) {
            buffer [offset + i] = 0;
        }

        return count;
    }

    public override bool CanRead {
        get { return true; }
    }

    ... the rest is not implemented
}

你可以像我对测试向量一样使用它:

// NIST CAVP test vector F.4.1: OFB-AES128.Encrypt from NIST SP 800-38A

RijndaelManaged aes = new RijndaelManaged ();
aes.Key = FromHex ("2b7e151628aed2a6abf7158809cf4f3c");
aes.IV = FromHex ("000102030405060708090A0B0C0D0E0F");
MemoryStream testVectorStream = new MemoryStream (FromHex (
    "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"));
OFBStream testOFBStream = new OFBStream (testVectorStream, aes, CryptoStreamMode.Read);
MemoryStream cipherTextStream = new MemoryStream ();
testOFBStream.CopyTo (cipherTextStream);
Console.WriteLine (ToHex (cipherTextStream.ToArray ()));

请注意,流处理尚未完全测试(尚未).

标签:c,net,aes,rijndaelmanaged
来源: https://codeday.me/bug/20190629/1321215.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有