public static string Encrypt(string data, int key = 0)
        {
            if (data == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(data))
            {
                return("");
            }

            int    keyIndex = key;
            string nonce    = Utility.GenerateNonce(NONCE_LENGTH);

            string toEncrypt = data + nonce;

            byte[] binary = Encoding.UTF8.GetBytes(toEncrypt);
            byte[] encrypted;

            AESCryptor.Encrypt(ref keyIndex, ref binary, out encrypted);

            string beEncrypted = StringTools.ToBase64String(encrypted);

            beEncrypted = keyIndex + Utility.SEP + beEncrypted + Utility.SEP + nonce;

            return(beEncrypted);
        }
        public static string EncryptToString(byte[] data, int key = 0)
        {
            if (data == null)
            {
                return(null);
            }

            if (data.Length == 0)
            {
                return(string.Empty);
            }

            int    keyIndex = key;
            string nonce    = Utility.GenerateNonce(NONCE_LENGTH);

            byte[] binaryNonce = Encoding.UTF8.GetBytes(nonce);

            byte[] dataToEncrypt = Utility.MergeBytes(data, binaryNonce);
            byte[] encrypted;

            AESCryptor.Encrypt(ref keyIndex, ref dataToEncrypt, out encrypted);

            string beEncrypted = StringTools.ToBase64String(encrypted);

            beEncrypted = keyIndex + Utility.SEP + beEncrypted + Utility.SEP + nonce;

            return(beEncrypted);
        }
示例#3
0
 public void Write(Stream xOut, bool enc)
 {
     this.Body.Write(xOut);
     if (xOut.Length % 16 != 0)
     {
         while (xOut.Length % 16 != 0)
         {
             xOut.WriteByte(0x70);
         }
     }
     byte[] xoutBuf = new byte[xOut.Length];
     xOut.Position = 0;
     xOut.Read(xoutBuf, 0, xoutBuf.Length);
     if (enc)
     {
         MemoryStream xMem = new MemoryStream(AESCryptor.Encrypt(xoutBuf, this.key));
         xOut.SetLength(0);
         xOut.Write(xMem.ToArray(), 0, (int)xMem.Length);
     }
     else
     {
         xOut.SetLength(0);
         xOut.Write(xoutBuf, 0, xoutBuf.Length);
     }
 }
示例#4
0
        public void Test_Decrypt()
        {
            var result = AESCryptor.Decrypt("JWm0M2KzKwxe8ylzkfujgQkyEq+kG1ZVirENQqCl8BI=", Key);

            Output.WriteLine(result);
            Assert.Equal("测试一下内容先", result);
        }
示例#5
0
        public void Test_DecryptIv()
        {
            string iv = InitIv(16);

            Output.WriteLine(iv);
            var result = AESCryptor.Decrypt("JWm0M2KzKwxe8ylzkfujgV8i5XUnUaHOpY7MaND0Z3g=", Key, Encoding.UTF8, iv, 256, 128, CipherMode.CBC,
                                            PaddingMode.PKCS7);

            Output.WriteLine(result);
            Assert.Equal("测试一下内容先", result);
        }
        public static byte[] DecryptToBytes(string encryptedData)
        {
            if (encryptedData == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(encryptedData))
            {
                return(new byte[0]);
            }

            string[] values = encryptedData.Split(Utility.SEPARRAY, StringSplitOptions.None);

            if (values.Length != 3)
            {
                return(null);
            }

            int    keyIndex;
            string beEncrypted = values[1];
            string nonce       = values[2];

            if (!int.TryParse(values[0], out keyIndex))
            {
                return(null);
            }

            byte[] encrypted = StringTools.FromBase64String(beEncrypted);
            byte[] binary;

            AESCryptor.Decrypt(ref keyIndex, ref encrypted, out binary);

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

            byte[] binaryNonce = Encoding.UTF8.GetBytes(nonce);
            byte[] data;
            byte[] checkNonceBinary;

            Utility.SeparateBytes(binary, binaryNonce.Length, out data, out checkNonceBinary);

            if (!Utility.IsBytesSame(binaryNonce, checkNonceBinary))
            {
                return(null);
            }

            return(data);
        }
        public static string Decrypt(string encryptedData)
        {
            if (encryptedData == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(encryptedData))
            {
                return(string.Empty);
            }

            string[] values = encryptedData.Split(Utility.SEPARRAY, StringSplitOptions.None);

            if (values.Length != 3)
            {
                return(encryptedData);
            }

            int    keyIndex;
            string beEncrypted = values[1];
            string nonce       = values[2];

            if (!int.TryParse(values[0], out keyIndex))
            {
                return(encryptedData);
            }

            byte[] encrypted = StringTools.FromBase64String(beEncrypted);
            byte[] binary;

            AESCryptor.Decrypt(ref keyIndex, ref encrypted, out binary);

            if (binary == null)
            {
                return(encryptedData);
            }

            string data       = Encoding.UTF8.GetString(binary);
            string checkNonce = data.Substring(data.Length - NONCE_LENGTH);

            if (checkNonce != nonce)
            {
                return(encryptedData);
            }

            return(data.Substring(0, data.Length - NONCE_LENGTH));
        }
示例#8
0
        /// <summary>
        /// New GTA V GameSave
        /// </summary>
        /// <param name="xIn">Gamesave stream</param>
        /// <param name="key">Encryption key</param>
        public V(Stream xIn, byte[] key, bool dec)
        {
            this.key = key;
            MemoryStream xMem;

            byte[] xInBuf = StreamUtils.ReadBytes(xIn, (int)xIn.Length);
            if (dec)
            {
                byte[] deced = AESCryptor.Decrypt(xInBuf, key);
                xMem = new MemoryStream(deced);
            }
            else
            {
                xMem = new MemoryStream(xInBuf);
            }
            this.Body = new Body(xMem);
        }
        public static byte[] BinaryDecrypt(byte[] encrypted, int you_know_what_is_this_key = 0)
        {
            if (encrypted == null)
            {
                return(null);
            }

            if (encrypted.Length == 0)
            {
                return(encrypted);
            }

            int keyIndex = you_know_what_is_this_key;

            byte[] data;

            AESCryptor.Decrypt(ref keyIndex, ref encrypted, out data);

            return(data);
        }
        public static byte[] BinaryEncrypt(byte[] data, int you_know_what_is_this_key = 0)
        {
            if (data == null)
            {
                return(null);
            }

            if (data.Length == 0)
            {
                return(data);
            }

            int keyIndex = you_know_what_is_this_key;

            byte[] encrypted;

            AESCryptor.Encrypt(ref keyIndex, ref data, out encrypted);

            return(encrypted);
        }
示例#11
0
        public Task Handle(string eventName, dynamic eventData)
        {
            var publishedEvent = _IExternalSubscriptionsManager.GetPublishedEvent(eventName);

            if (publishedEvent.EventThresholdSeconds > 0)
            {
                var cachekey       = eventName + "_LastHandleTime";
                var lastHandleTime = _IDataCacheManager.GetCache <DateTime?>(cachekey);
                if (lastHandleTime != null && DateTime.Now.Subtract(lastHandleTime.Value).TotalSeconds < publishedEvent.EventThresholdSeconds)
                {
                    return(Task.FromResult(0));
                }

                _IDataCacheManager.SetCache(cachekey, DateTime.Now);
            }

            var subscribers = _IExternalSubscriptionsManager.GetPublicSubscribersByEventName(eventName);

            var resilientHttpClient = new ResilientHttpClient((c) => CreatePolicies(), _logger);

            foreach (var subscriber in subscribers)
            {
                string strNewAddress = subscriber.Address + (subscriber.Address.IndexOf("?") == -1 ? "?" : "&");
                strNewAddress += "eventname=" + eventName;
                if (String.IsNullOrEmpty(subscriber.PrivateKey))
                {
                    resilientHttpClient.PostAsync(strNewAddress, eventData);
                }
                else
                {
                    var message        = JsonConvert.SerializeObject(eventData);
                    var encryptMessage = AESCryptor.EncryptStringAES(message, subscriber.PrivateKey);
                    resilientHttpClient.PostAsync(strNewAddress, encryptMessage);
                }
            }
            return(Task.FromResult(1));
        }