基本密码转换

目录

从System.Security.Cryptography.ICryptoTransform派生的简单密码

使用代码


下载源代码 - 1.2 KB

System.Security.Cryptography.ICryptoTransform派生的简单密码

这是用户定义的简单密码转换。

使用代码

这就是abstractCryptoBlock的运作方式。它是用户定义CryptoBlockDictionary<byte, byte>,它的块大小必须为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;
        }
    }
}

https://www.codeproject.com/Tips/5325293/A-Basic-Cipher-Transform

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值