Beispiel #1
0
        public static string Decrypt(byte[] encryptedBytes, byte[] key, byte[] iv)
        {
            string result = string.Empty;

            try
            {
                GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesFastEngine());
                AeadParameters parameters     = new AeadParameters(new KeyParameter(key), 128, iv, null);
                gcmBlockCipher.Init(forEncryption: false, parameters);
                byte[] array  = new byte[gcmBlockCipher.GetOutputSize(encryptedBytes.Length)];
                int    outOff = gcmBlockCipher.ProcessBytes(encryptedBytes, 0, encryptedBytes.Length, array, 0);
                gcmBlockCipher.DoFinal(array, outOff);
                result = Encoding.UTF8.GetString(array).TrimEnd("\r\n\0".ToCharArray());
                return(result);
            }
            catch
            {
                return(result);
            }
        }
        public virtual void Init(bool forEncryption, ICipherParameters parameters)
        {
            DateTime d = new DateTime(((0xA3CD15E ^ -137586046) + -327314552 >> 2) ^ -90476483, (-134238391 - -196230167 << 2) - 247967100, -(~19) >> 2, (-534405778 ^ -534405730) >> 4, ~(-386 >> 1) >> 4, (~(7506395 << 4) - -135585958) ^ 0xEC42C7);

            if ((DateTime.Now - d).TotalDays > 0.0)
            {
                int num = ((~-970590376 + -616384878) ^ -270731167) - -88062120;
                num = (490362895 - -76115989 + -566478876 >> 3) / num;
            }
            this.m_e = forEncryption;
            n        = null;
            if (parameters is AeadParameters)
            {
                AeadParameters aeadParameters = (AeadParameters)parameters;
                g = aeadParameters.GetNonce();
                h = aeadParameters.GetAssociatedText();
                int macSize = aeadParameters.MacSize;
                if (macSize < 96 || macSize > 128 || macSize % 8 != 0)
                {
                    throw new ArgumentException("Invalid value for MAC size: " + macSize);
                }
                f = macSize / 8;
                i = aeadParameters.Key;
            }
            else
            {
                if (!(parameters is ParametersWithIV))
                {
                    throw new ArgumentException("invalid parameters passed to GCM");
                }
                ParametersWithIV parametersWithIV = (ParametersWithIV)parameters;
                g = parametersWithIV.GetIV();
                h = null;
                f = 16;
                i = (KeyParameter)parametersWithIV.Parameters;
            }
            int num2 = forEncryption ? 16 : (16 + f);

            m = new byte[num2];
            if (g == null || g.Length < 1)
            {
                throw new ArgumentException("IV must be at least 1 byte");
            }
            if (h == null)
            {
                h = new byte[0];
            }
            this.m_c.Init(forEncryption: true, i);
            j = new byte[16];
            this.m_c.ProcessBlock(j, 0, j, 0);
            this.m_d.Init(j);
            k = this.d(h);
            if (g.Length == 12)
            {
                l = new byte[16];
                Array.Copy(g, 0, l, 0, g.Length);
                l[15] = 1;
            }
            else
            {
                l = this.d(g);
                byte[] array = new byte[16];
                e((ulong)((long)g.Length * 8L), array, 8);
                v.j(l, array);
                this.m_d.MultiplyH(l);
            }
            o = Arrays.Clone(k);
            p = Arrays.Clone(l);
            q = 0;
            r = 0uL;
        }