Rijndael的AES OFB加密 [英] AES OFB encryption for RijndaelManaged

查看:122
本文介绍了Rijndael的AES OFB加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过OFB模式下的加密消息从C#应用程序与另一个应用程序进行通信。我知道 RijndaelManaged 不支持AES OFB模式。有没有比我更有经验的人知道使用OFB模式进行加密/解密的其他方法?

I need to communicate from a C# application to another application via encrypted messages in OFB mode. I know that RijndaelManaged does not have support for AES OFB mode. Is there anybody more experienced than me aware of any other way to encrypt/decrypt using OFB mode?

推荐答案

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

The following stream implements OFB by using a key stream generated by a zero-fed CBC cipher stream.

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

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
}

您可以像对待测试向量一样使用它:

And you can use it as I do for a test vector:

// 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 ()));






请注意,流处理尚未经过全面测试(还)。


Note that the stream handling has not been fully tested (yet).

这篇关于Rijndael的AES OFB加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆