Пример #1
0
        public static byte[] XTS(byte[] Key1, byte[] Key2, int SectorSize, byte[] Data, ulong Sector, bool Encrypt)
        {
            byte[]             TransformedBytes, BlockData;
            Xts                XTS128 = XtsAes128.Create(Key1, Key2);
            int                Blocks;
            var                MemStrm = new MemoryStream();
            var                Writer  = new BinaryWriter(MemStrm);
            XtsCryptoTransform CryptoTransform;

            if (Encrypt)
            {
                CryptoTransform = XTS128.CreateEncryptor();
            }
            else
            {
                CryptoTransform = XTS128.CreateDecryptor();
            }
            BlockData = new byte[SectorSize];
            Blocks    = Data.Length / SectorSize;
            for (int i = 0; i < Blocks; i++)
            {
                CryptoTransform.TransformBlock(Data, i * SectorSize, SectorSize, BlockData, 0, Sector++);
                Writer.Write(BlockData);
            }
            TransformedBytes = MemStrm.ToArray();
            return(TransformedBytes);
        }
Пример #2
0
        private void Decrypt_Click(object sender, RoutedEventArgs e)
        {
            var             inputString     = EncryptInputTextBox.Text;
            CryptoTransform cryptoTransform = new CryptoTransform(Helper.PASSPHRASE, Helper.INITVECTOR);

            var outputString = cryptoTransform.Decrypt(inputString);

            DecryptOutputTextBox.Text = outputString;
        }
Пример #3
0
 public unsafe override void Transform(byte[] input)
 {
     byte[] buffer = new byte[BlockSize];
     for (int i = 0; i < input.Length; i += BlockSize)
     {
         CryptoTransform.TransformBlock(x, 0, BlockSize, buffer, 0);
         input.Xor(i, buffer, 0, BlockSize);
         x.Increment();
     }
 }
        protected CryptoTransform GetTransform(string peerId)
        {
            CryptoTransform transform;

            lock (_transforms)
            {
                if (!_transforms.TryGetValue(peerId, out transform))
                {
                    // we prepend RijndaelV1 to peerId to make possible future changes to RijndaelEncryptionProvider logic (for example increasing key or IV size).
                    // if we use unchanged peerId (user id) then it will be impossilbe to store ecnryption keys of different sizes and encryption provider versioning will be broken. 
                    var key =_encryptionEncryptionKeyProvider.GetEncryptionKey("RijndaelV1." + peerId, CryptoProvider.KeySize / 8, CryptoProvider.BlockSize / 8);

                    transform = new CryptoTransform(CryptoProvider.CreateEncryptor(key.Key, key.IV), 
                                                    CryptoProvider.CreateDecryptor(key.Key, key.IV));
                    _transforms.Add(peerId, transform);
                }
            }

            return transform;
        }
Пример #5
0
 public void Transform(byte[] data, int offset, int length, byte[] output, int outputOffset)
 {
     for (var i = 0; i < length; i++)
     {
         if (Number == 0)
         {
             CryptoTransform.TransformBlock(Iv, 0, Iv.Length, Xor, 0);
             for (var j = 15; j >= 0; j--)
             {
                 Iv[j]++;
                 if (Iv[j] != 0)
                 {
                     break;
                 }
             }
         }
         output[i + outputOffset] = (byte)(data[i + offset] ^ Xor[Number]);
         Number = (Number + 1) & 0xF;
     }
 }
Пример #6
0
        /// <summary>
        /// Calls <see cref="Finish"/> and closes the underlying
        /// stream when <see cref="IsStreamOwner"></see> is true.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!isClosed)
            {
                isClosed = true;

                try
                {
                    Finish();
                    if (CryptoTransform != null)
                    {
                        CryptoTransform.Dispose();
                        CryptoTransform = null;
                    }
                }
                finally
                {
                    if (IsStreamOwner)
                    {
                        BaseOutputStream.Dispose();
                    }
                }
            }
        }
Пример #7
0
        private void InputToOutput(Encoding encode)
        {
            otp = OneTimePadAlgorithm.Create();
            otp.setInputLength(txtInput.Text.Length);
            if (rbEncrypt.Checked)
            {
                bool isNumeric = int.TryParse(txtSeed.Text, out _);
                if (isNumeric)
                {
                    otp.setStringSeed(int.Parse(txtSeed.Text));
                }
                else
                {
                    otp.setStringSeed(txtSeed.Text);
                }

                otp.GenerateKey();
                CryptoTransform ict = (CryptoTransform)otp.CreateEncryptor(otp.Key, null);


                byte[] inputText = encode.GetBytes(txtInput.Text);

                txtOTP.Text    = encode.GetString(otp.Key);
                txtOutput.Text = encode.GetString(ict.TransformFinalBlock(inputText, 0, txtInput.Text.Length));
            }
            else
            {
                otp.Key = encode.GetBytes(txtOTP.Text);
                CryptoTransform ict = (CryptoTransform)otp.CreateDecryptor(otp.Key, null);

                byte[] inputText = encode.GetBytes(txtInput.Text);


                txtOutput.Text = encode.GetString(ict.TransformFinalBlock(inputText, 0, txtInput.Text.Length));
            }
        }
Пример #8
0
 /// <summary>
 /// Encrypt a block of data.
 /// </summary>
 /// <param name="buffer">
 /// Data to encrypt.  NOTE the original contents of the buffer are lost.
 /// </param>
 /// <param name="offset">
 /// Offset of first byte in buffer to encrypt.
 /// </param>
 /// <param name="length">
 /// Number of bytes in buffer to encrypt.
 /// </param>
 protected void EncryptBlock(byte[] buffer, int offset, int length)
 {
     CryptoTransform.TransformBlock(buffer, offset, length, buffer, 0);
 }
Пример #9
0
 public override byte[] TransformFinal(byte[] input)
 {
     return(CryptoTransform.TransformFinalBlock(input, 0, input.Length));
 }
Пример #10
0
 public override void Transform(byte[] input)
 {
     CryptoTransform.TransformBlock(input, 0, input.Length, input, 0);
 }
Пример #11
0
 public void Dispose()
 {
     CryptoTransform.Dispose();
 }
Пример #12
0
 /// <summary>
 /// 创建签名秘钥
 /// </summary>
 /// <param name="lenght">签名秘钥字节长度</param>
 /// <returns>签名Key Base64字符串</returns>
 public String CreatedSignKey(int lenght = 64)
 {
     return(CryptoTransform.CreateKey(lenght));
 }