예제 #1
0
 private void bDecrypted_Click(object sender, EventArgs e)
 {
     if (Check())
     {
         string saveFileName = NewFileName(file, 1);
         if (fileType == FileType.txt)
         {
             //string text = File.ReadAllText(file);
             byte[] bytes   = Convert.FromBase64String(richTextBox1.Text); //File.ReadAllBytes(file);
             byte[] decText = cryptoProvider.Decrypt(bytes);
             string decT    = Encoding.Unicode.GetString(decText);
             richTextBox1.Text = decT;
             //File.WriteAllText(saveFileName, decT, Encoding.Unicode);
             //cryptoProvider.Decrypt(file, NewFileName(file, 1));
         }
         else
         {
             byte[] bytes           = ImageToBytes(imageIn as Bitmap);
             byte[] decryptionBytes = cryptoProvider.Decrypt(bytes);
             imageOut      = BytesToImage(decryptionBytes, imageIn.Size);
             pbImage.Image = imageOut;
             //imageOut.Save(saveFileName);
         }
         labelLog.ForeColor = Color.Green;
         labelLog.Text      = "Дешифровано!";
         StartTimer();
     }
 }
예제 #2
0
 private void bDecrypt_Click(object sender, EventArgs e)
 {
     try
     {
         InitCipher();
         byte[] bytes          = Convert.FromBase64String(tbEnctyptedText.Text);
         byte[] decryptedBytes = cipher.Decrypt(bytes);
         tbPlainText.Text = Encoding.Unicode.GetString(decryptedBytes);
     }
     catch (Exception ex)
     {
         lError.Text = ex.Message;
     }
 }
예제 #3
0
 /// <summary>
 /// Decrypt the encrypted contents of a byte array
 /// </summary>
 /// <param name="cipher">The <see cref="ICipher"/> to use for decrypting the data</param>
 /// <param name="input">The encrypted data to decrypt</param>
 /// <returns>An array containing the cleartext data</returns>
 public static byte[] Decrypt(this ICipher cipher, byte[] input)
 {
     using (var ostream = new MemoryStream()) {
         cipher.Decrypt(input, ostream);
         return(ostream.ToArray());
     }
 }
예제 #4
0
파일: UAC.cs 프로젝트: zuhuizou/DroidLord
        /// <summary>
        /// RPC调用
        /// </summary>
        /// <param name="name"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static object Action(string name, string val, ICipher algorithm = null)
        {
            if (algorithm == null)
            {
                algorithm = Program.AES;
            }

            var encData  = algorithm.Encrypt(val);
            var respJson = session.Post(UAC_API, new
            {
                action = name,
                data   = encData
            });

            if (respJson != null)
            {
                var jObj = respJson as JObject;
                if (jObj["data"][0] == null)
                {
                    return(null);
                }
                var rtData = algorithm.Decrypt(jObj["data"][0].ToString());
                var json   = Program.JsonParse(rtData);
                if (json == null)
                {
                    // 是字符串
                    return(rtData);
                }
                else
                {
                    return(json);
                }
            }
            return(null);
        }
예제 #5
0
        /// <summary>
        /// Decrypts the <paramref name="encrypted"/> data with the <paramref name="cipher"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cipher">The cipher.</param>
        /// <param name="encrypted">The encrypted.</param>
        /// <returns>T.</returns>
        public static Nullable <T> DecryptNullable <T>(
            this ICipher cipher,
            byte[] encrypted) where T : struct
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }
            if (encrypted == null)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }
            if (encrypted.Length < 2)
            {
                throw new ArgumentException(Resources.InvalidArgumentLength, nameof(encrypted));
            }
            if (!EncryptTypedData.ContainsKey(typeof(T)))
            {
                throw new ArgumentException("The specified data type cannot be decrypted.");
            }

            var decrypted = cipher.Decrypt(encrypted);

            if (decrypted.Length < 2)
            {
                throw new ArgumentException("The argument is not a valid encrypted Nullable<T> value.");
            }

            return(FromByteArray.ToNullable <T>(decrypted));
        }
예제 #6
0
        public byte[] ToByteArray(bool uhash, ICipher cipher)
        {
            using (MemoryStream data = new MemoryStream())
            {
                using (BinaryWriter data_writer = new BinaryWriter(data))
                {
                    byte[] body = stream.ToArray();
                    if (cipher != null)
                    {
                        cipher.Decrypt(ref body, body.Length);
                    }

                    data_writer.Write(CodePacketType(type, body.Length));
                    data_writer.Write((ushort)body.Length);
                    data_writer.Write((uint)sequence);
                    if (uhash)
                    {
                        data_writer.Write((int)this.uhash);
                    }
                    data_writer.Write(body, 0, body.Length);

                    return(data.ToArray());
                }
            }
        }
예제 #7
0
        public void CipherAndDecipherTextTest(ICipher cipher)
        {
            var plainText  = "Secret Text";
            var passPhrase = "password1";

            var cipherText   = cipher.Encrypt(plainText, passPhrase);
            var decipherText = cipher.Decrypt(cipherText, passPhrase);

            Assert.True(plainText == decipherText);
        }
예제 #8
0
 public AuthenticationInfo ReadTicket(string ticketValue, string userUniqueID)
 {
     try
     {
         return(CipherService.Decrypt <AuthenticationInfo>(SettingsManager.AppPrefix, userUniqueID, ticketValue));
     }
     catch
     {
         return(null);
     }
 }
예제 #9
0
        public string GetPassword(string title, string superPassword)
        {
            var record = vault.Get(title);
            var sign   = cipher.CalculateHash(superPassword);

            if (record.Sign != sign)
            {
                throw new InvalidSuperPasswordException();
            }

            return(cipher.Decrypt(record.CipherPassword, superPassword));
        }
예제 #10
0
        public static TcpPacket Parse(ref byte[] buffer, ICipher cipher)
        {
            if (buffer.Length < 6)
            {
                return(null);
            }
            MemoryStream instream = new MemoryStream(buffer);
            BinaryReader reader   = new BinaryReader(instream);
            TcpPacket    resultPacket;

            try
            {
                ushort type = reader.ReadUInt16();
                int    len  = reader.ReadInt32();
                if (buffer.Length - 6 < len)
                {
                    return(null);
                }

                type = CodePacketType(type, len);

                byte[] body = reader.ReadBytes((int)len);

                if (cipher != null)
                {
                    cipher.Decrypt(ref body, body.Length);
                }

                resultPacket = new TcpPacket(body, type);

                int tailsize = buffer.Length - 6 - (int)len;
                if (tailsize == 0)
                {
                    buffer = new byte[0];
                }
                else
                {
                    buffer = reader.ReadBytes(tailsize);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                reader.Close();
                instream.Close();
            }

            return(resultPacket);
        }
예제 #11
0
        public static UdpPacket Parse(byte[] buffer, bool uhash_e, ICipher cipher)
        {
            int HEADER_SIZE = uhash_e ? 12 : 8; //WITH UHASH OR NOT

            if (buffer.Length < HEADER_SIZE)
            {
                return(null);
            }
            MemoryStream instream = new MemoryStream(buffer);
            BinaryReader reader   = new BinaryReader(instream);
            UdpPacket    resultPacket;

            try
            {
                ushort type     = reader.ReadUInt16();
                ushort len      = reader.ReadUInt16();
                uint   sequence = reader.ReadUInt32();
                int    uhash    = 0;
                if (uhash_e)
                {
                    uhash = reader.ReadInt32();
                }
                if (buffer.Length - HEADER_SIZE < len)
                {
                    return(null);                                   //пакет не полный
                }
                byte[] body = reader.ReadBytes((int)len);

                if (cipher != null)
                {
                    cipher.Decrypt(ref body, (int)len);
                }

                resultPacket          = new UdpPacket(body, CodePacketType(type, len));
                resultPacket.sequence = sequence;
                resultPacket.uhash    = uhash;
            }
            catch
            {
                throw;
            }
            finally
            {
                reader.Close();
                instream.Close();
            }

            return(resultPacket);
        }
예제 #12
0
        public override int Read(byte[] buf, int offset, int count)
        {
            EnsureStreamNotDisposed();
            int      ncount   = count;
            DateTime timeout1 = DateTime.Now.AddMilliseconds(500);

            while (count > 0)
            {
                var nread = _stream.Read(buf, offset, count);

                if (nread != 0 && Cipher != null)
                {
                    var enc = new byte[nread];
                    for (int i = 0; i < nread; i++)
                    {
                        enc[i] = buf[offset + i];
                    }

                    var data = (Cipher != null) ? Cipher.Decrypt(enc) : enc;
                    if (enc.Length != data.Length)
                    {
                        throw new StreamControlException("Invalid Decrypted Block Length");
                    }

                    //if (Logger.IsDebugEnabled)
                    //    Logger.Debug("<< Data: {0}", string.Join(" ", enc.Select(m => $"{m:x2}")));

                    for (int i = 0; i < nread; i++)
                    {
                        buf[offset++] = data[i];
                    }
                }
                if (nread == 0)
                {
                    if (DateTime.Now > timeout1)
                    {
                        throw new SecureChannelException("Timeout");
                    }
                    Thread.Sleep(1);
                }
                else
                {
                    timeout1 = DateTime.Now.AddMilliseconds(500);
                    count   -= nread;
                }
            }
            return(ncount);
        }
예제 #13
0
        public static ushort[] DecryptUInt16Array(
            this ICipher cipher,
            byte[] encrypted)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (encrypted == null)
            {
                return(null);
            }

            return(FromByteArray.ToUInt16Array(cipher.Decrypt(encrypted)));
        }
예제 #14
0
        /// <summary>
        /// Decrypts the <paramref name="encryptedText"/> to a string, provided the original string was encoded with <see cref="T:System.Text.Encoding.UTF8"/>.
        /// </summary>
        /// <param name="cipher">The cipher.</param>
        /// <param name="encryptedText">The crypto text.</param>
        /// <returns>The decrypted text.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="cipher"/> is <see langword="null"/>.</exception>
        public static string DecryptString(
            this ICipher cipher,
            byte[] encryptedText)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (encryptedText == null)
            {
                return(null);
            }

            return(FromByteArray.ToString(cipher.Decrypt(encryptedText)));
        }
예제 #15
0
        /// <summary>
        /// Decrypts the <paramref name="encrypted"/> to an array of <see cref="bool"/> values.
        /// </summary>
        /// <param name="cipher">The cipher.</param>
        /// <param name="encrypted">The data to be decrypted.</param>
        /// <returns>The encrypted text.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="cipher"/> is <see langword="null"/>.</exception>
        public static bool[] DecryptBooleanArray(
            this ICipher cipher,
            byte[] encrypted)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (encrypted == null)
            {
                return(null);
            }

            var decrypted = cipher.Decrypt(encrypted);

            return(FromByteArray.ToBooleanArray(decrypted));
        }
예제 #16
0
        private IEncryptor GetEncryptor(int id, ICipher cipher, Stream input)
        {
            if (id == EncryptorId.ECB)
            {
                return(new ECB(cipher, new PKCS7Padding()));
            }

            if (id == EncryptorId.CBC)
            {
                var iv = new byte[cipher.BlockSize];

                for (int i = 0; i < iv.Length; i++)
                {
                    iv[i] = Convert.ToByte(input.ReadByte());
                }

                return(new CBC(cipher, new PKCS7Padding(), cipher.Decrypt(iv)));
            }

            throw new ArgumentOutOfRangeException();
        }
예제 #17
0
        public static ushort DecryptUInt16(
            this ICipher cipher,
            byte[] encrypted)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }
            if (encrypted == null)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }

            var decrypted = cipher.Decrypt(encrypted);

            if (decrypted.Length < 2)
            {
                throw new ArgumentException(Resources.InvalidEncryptedValue);
            }

            return(FromByteArray.ToChar(decrypted));
        }
예제 #18
0
        /// <summary>
        /// Decrypts the properties and fields of the <see cref="_instance"/>.
        /// </summary>
        void Decrypt(
            IGrouping <string, MemberInfo> g)
        {
            // if it has been decrypted already - return
            if (_decryptedJsonStrings.ContainsKey(g.Key))
            {
                return;
            }

            // get the value of the EncryptedIn property or field
            var piEncrypted = _type.GetProperty(g.Key, EncryptedPartsFlags);
            var fiEncrypted = piEncrypted != null ? null : _type.GetField(g.Key, EncryptedPartsFlags);
            var value       = piEncrypted != null
                                    ? (byte[])piEncrypted.GetValue(_instance)
                                    : (byte[])fiEncrypted.GetValue(_instance);

            // decrypt it
            var json = _cipher.Decrypt <string>(value);

            // cache it
            _decryptedJsonStrings[g.Key] = json;

            // parse into JObject
            var jo = JObject.Parse(json);

            // set the properties and fields
            foreach (var p in jo.Properties())
            {
                var pi = _type.GetProperty(p.Name, EncryptedPartsFlags);
                var fi = pi != null ? null : _type.GetField(p.Name, EncryptedPartsFlags);

                if (pi != null)
                {
                    pi.SetValue(_instance, pi != null
                                                ? p.ToObject(pi.PropertyType)
                                                : p.ToObject(fi.FieldType));
                }
            }
        }
예제 #19
0
        /// <summary>
        /// Decrypts the <paramref name="encrypted"/> text to a <see cref="decimal"/> value.
        /// </summary>
        /// <param name="cipher">The cipher.</param>
        /// <param name="encrypted">The encrypted text.</param>
        /// <returns>The decrypted value.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="cipher"/> or <paramref name="encrypted"/> are <see langword="null"/>.</exception>
        public static Guid DecryptGuid(
            this ICipher cipher,
            byte[] encrypted)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }
            if (encrypted == null)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }

            var decrypted = cipher.Decrypt(encrypted);

            if (decrypted.Length != 16)
            {
                throw new ArgumentException(Resources.InvalidEncryptedValue);
            }

            return(FromByteArray.ToGuid(decrypted));
        }
예제 #20
0
        public static byte[] DecryptData64(
            this ICipher cipher,
            string encryptedData64)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (encryptedData64 == null)
            {
                return(null);
            }

            var base64 = cipher.Base64Encoded;

            cipher.Base64Encoded = false;

            var decryptedData = cipher.Decrypt(Convert.FromBase64String(encryptedData64));

            cipher.Base64Encoded = base64;

            return(decryptedData);
        }
예제 #21
0
 /// <summary>
 /// Thread-safe.
 /// </summary>
 public static string DecryptToString(this ICipher cipher, byte[] data)
 {
     return(Encoding.UTF8.GetString(cipher.Decrypt(data)));
 }
예제 #22
0
        public IActionResult Decrypt([FromBody] string payload)
        {
            var result = _cipher.Decrypt(payload);

            return(Ok(result));
        }
예제 #23
0
파일: Text.cs 프로젝트: pylasnier/ciphers
 public void Decrypt(List <dynamic> key)
 {
     PlainText = cipher.Decrypt(CipherText, key);
 }
예제 #24
0
        public void Decrypt(int cipherText, int key)
        {
            int message = _cipher.Decrypt(cipherText, key);

            _writer.WriteLine(string.Format(format, cipherText, key, message, nameof(Decrypt)));
        }
예제 #25
0
 public EntryKeyValuePair ToEntryKeyValuePair(ICipher cipher)
 {
     return(new EntryKeyValuePair(cipher.Decrypt(Key), cipher.Decrypt(Value), IsMultiline, IsMandatory, IsProtected));
 }
예제 #26
0
 private void PerformDecrypt(ICipher cipher)
 {
     textBoxOutput.Text = cipher.Decrypt(textBoxInput.Text.PrepareText()).ToLower();
 }
예제 #27
0
 /// <summary>
 /// Decrypt the encrypted contents of a byte array and writes it to an output stream
 /// </summary>
 /// <param name="cipher">The <see cref="ICipher"/> to use for decrypting the data</param>
 /// <param name="input">The encrypted data to decrypt</param>
 /// <param name="output">The stream to write the cleartext data to</param>
 /// <remarks>This method leave the <paramref name="output"/> stream open</remarks>
 public static void Decrypt(this ICipher cipher, byte[] input, Stream output)
 {
     using (var istream = new MemoryStream(input))
         cipher.Decrypt(istream, output);
 }
예제 #28
0
        public void Decrypd_RightApp_RightClient_RightTime_Should_Work()
        {
            string ticket = cipher.Encrypt(appPrefix, userUniqueID, secret, TimeSpan.FromMinutes(1));

            Assert.AreEqual(secret, cipher.Decrypt <string>(appPrefix, userUniqueID, ticket));
        }