您当前的位置: 首页 > 

寒冰屋

暂无认证

  • 2浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

基本密码转换

寒冰屋 发布时间:2022-07-19 21:00:00 ,浏览量:2

目录

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

使用代码

下载源代码 - 1.2 KB

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

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

使用代码

这就是abstract类CryptoBlock的运作方式。它是用户定义CryptoBlock的Dictionary,它的块大小必须为256,并且没有键值对可以匹配。

// This is the base CryptoBlock used for the CipherTransform class
using System.Collections.Generic;
namespace Chico.CipherCrypto>
{    
    public abstract class CryptoBlock : Dictionary,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

关注
打赏
1665926880
查看更多评论
立即登录/注册

微信扫码登录

0.0436s