65.9K
CodeProject 正在变化。 阅读更多。
Home

一个基本的密码变换

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (1投票)

2022年2月16日

CPOL
viewsIcon

6026

downloadIcon

82

一个简单的用户自定义密码变换,其想法来源于 https://codeproject.org.cn/Articles/5319044/ARC4-Encryption-Library

一个源自 System.Security.Cryptography.ICryptoTransform 的简单密码

这是一个简单的用户自定义密码变换。

Using the Code

这是 abstractCryptoBlock 的工作方式。它是一个 Dictionary<byte, byte>,这是一个用户定义的 CryptoBlock,它必须具有 256 的块大小,并且不能有匹配的键值对。

// This is the base CryptoBlock used for the CipherTransform class
using System.Collections.Generic;
namespace Chico.CipherCrypto>
{    
    public abstract class CryptoBlock : Dictionary<byte>,byte>
    {
        public const int BlockSize = 256;
        protected CryptoBlock() : base(BlockSize)
        {
        }        
    }
}

接下来,我们将看一下 CipherTransform。该类基于 ICryptoTransform,并与 CryptoStream 一起使用。

using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace Chico.CipherCrypto
{
    [DebuggerStepThrough]
    public class CipherTransform : ICryptoTransform
    {
        private CryptoBlock cipher;
        public CipherTransform(CryptoBlock cryptoBlock)
        {            
            var cipher = typeof(CryptoBlock);
            if (cryptoBlock == null)
            {
                throw new NotImplementedException(cipher + " can not be null.");
            }
            if (cryptoBlock.Count != CryptoBlock.BlockSize)
            {
                throw new NotSupportedException(cipher + "is not supported");
            }
            byte[] keys = cryptoBlock.Keys.ToArray();
            byte[] values = cryptoBlock.Values.ToArray();            
            for (int i = 0; i < keys.Length; i++)
            {
                if (keys[i] == values[i])
                {
                    throw new NotSupportedException(cipher + " is not supported.");
                }
            }
            this.cipher = cryptoBlock;
        }
        public void Dispose() => this.cipher.Clear();
        bool ICryptoTransform.CanReuseTransform => true;
        bool ICryptoTransform.CanTransformMultipleBlocks => true;
        int ICryptoTransform.InputBlockSize => CryptoBlock.BlockSize;
        int ICryptoTransform.OutputBlockSize => CryptoBlock.BlockSize;
        private void Cipher(byte[] buffer, int offset, int count)
        {
            for (int i = offset; i < count; i++)
            {
                byte current = buffer[i];
                byte next = this.cipher[current];
                buffer[i] = next;
            }
        }

        public int TransformBlock(byte[] inputBuffer, int inputOffset, 
               int inputCount, byte[] outputBuffer, int outputOffset)
        {
            Array.Copy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
            Cipher(outputBuffer, outputOffset, inputCount);
            return inputCount;
        }

        public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
        {
            byte[] outputBuffer = new byte[inputCount];
            Array.Copy(inputBuffer, inputOffset, outputBuffer, 0, inputCount);
            Cipher(outputBuffer, 0, inputCount);
            return outputBuffer;
        }
    }
}

历史

  • 2022年2月16日:版本 1
© . All rights reserved.