Ejemplo n.º 1
0
        //登录信息
        private object LoginRequestDecodeParser(byte[] bytes)
        {
            byte[] messageBytes = bytes;

            messageBytes = SnappySharp.Uncompress(messageBytes);

            messageBytes = AESCrypto.Decrypt(messageBytes, SecretKey, SecretVector);

            string jsonStr = Encoding.UTF8.GetString(messageBytes);

            return(JsonConvert.DeserializeObject(jsonStr, typeof(LoginRequest)));
        }
Ejemplo n.º 2
0
        public void Test()
        {
            using (var aesCrypto = new AESCrypto())
            {
                aesCrypto.Initialize(new Dictionary <string, object>
                {
                    { "Key", "awVFRYPeTTrA9T7OOzaAFUvu8I/ZyYjAtIzEjCmzzYw=" },
                    { "IV", "7cFxoI3/k1wxN9P6rEyR/Q==" }
                });
                var plainText = "SmartSql";

                var cipherText  = aesCrypto.Encrypt(plainText);
                var decryptText = aesCrypto.Decrypt(cipherText);
                Assert.Equal(plainText, decryptText);
                cipherText  = aesCrypto.Encrypt(plainText);
                decryptText = aesCrypto.Decrypt(cipherText);
                Assert.Equal(plainText, decryptText);
                cipherText  = aesCrypto.Encrypt(plainText);
                decryptText = aesCrypto.Decrypt(cipherText);
                Assert.Equal(plainText, decryptText);
            }
        }
Ejemplo n.º 3
0
        public static void Main()
        {
            //pruebasMetodo1();
            string textoPlano = "Texto para encriptar";
            string textoClave = "Esto es una clave";


            string encriptado = AESCrypto.Encrypt(textoPlano, textoClave);

            Console.WriteLine("Texto plano: {0}", textoPlano);
            Console.WriteLine("Encrypted: {0}", encriptado);
            string desencriptado = AESCrypto.Decrypt(encriptado, textoClave);

            Console.WriteLine("Desencriptado: {0}", desencriptado);
            Console.ReadKey();
        }
Ejemplo n.º 4
0
 private Token getToken()
 {
     try
     {
         var now         = Helper.GetSecondTimeStamp();
         var timestamp   = AESCrypto.ToBytes(string.Format("{0}", now));
         var encryptTime = m_crypto.Encrypt(timestamp);
         var param       = new Dictionary <string, object>()
         {
             { "buin", m_buin },
             { "appId", m_appId },
             { "encrypt", encryptTime }
         };
         var client = new HttpClient();
         var rsp    = client.Post(this.apiGetToken(), param, HttpContentTypes.ApplicationJson);
         Helper.CheckHttpStatus(rsp);
         var body = rsp.StaticBody <Dictionary <string, object> >(overrideContentType: HttpContentTypes.ApplicationJson);
         Helper.CheckApiStatus(body);
         var    encrypt   = Helper.GetEncryptJsonValue(body);
         var    buffer    = m_crypto.Decrypt(encrypt);
         var    tokenInfo = new JsonReader().Read <Dictionary <string, object> >(AESCrypto.ToString(buffer));
         object accessToken;
         object expireIn;
         if (!tokenInfo.TryGetValue("accessToken", out accessToken) ||
             accessToken is string == false ||
             ((string)accessToken).Length == 0 ||
             !tokenInfo.TryGetValue("expireIn", out expireIn) ||
             expireIn is int == false ||
             (int)expireIn <= 0)
         {
             throw new ParamParserException("invalid token or expireIn", null);
         }
         return(new Token((string)accessToken, now, (int)expireIn));
     }
     catch (WebException e)
     {
         throw new HttpRequestException(0, e.Message, e);
     }
     catch (Exception e)
     {
         if (e is GeneralEntAppException)
         {
             throw e;
         }
         else
         {
             throw new UnexpectedException(e.Message, e);
         }
     }
 }
Ejemplo n.º 5
0
        public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData)
        {
            Console.WriteLine("POST request: {0}", p.http_url);

            var compnents = p.http_url.Split('?');

            if (compnents.Length != 2)
            {
                Console.WriteLine("invalid url compnents: {0}", compnents.ToString());
                p.writeFailure();
                return;
            }

            var reqUri = compnents[0];

            if (!reqUri.Equals(Uri))
            {
                p.writeFailure();
                return;
            }
            var queryPath = compnents[1];
            var queryDict = new Dictionary <string, string>();

            foreach (var statement in queryPath.Split("&&".ToCharArray()))
            {
                var elements = statement.Split('=');
                if (elements.Length == 2)
                {
                    queryDict[elements[0]] = elements[1];
                }
            }

            var    data       = inputData.ReadToEnd();
            var    jsonReader = new JsonReader();
            var    reqJson    = jsonReader.Read <Dictionary <string, object> >(data);
            object toBuin;
            object toApp;
            object encrypt;

            if (!reqJson.TryGetValue("toBuin", out toBuin) ||
                toBuin is int == false ||
                (int)toBuin <= 0 ||
                !reqJson.TryGetValue("toApp", out toApp) ||
                toApp is string == false ||
                ((string)toApp).Length == 0 ||
                !reqJson.TryGetValue("encrypt", out encrypt) ||
                encrypt is string == false ||
                ((string)encrypt).Length == 0)
            {
                Console.WriteLine("invalid toBuin or toApp or encrypt");
                p.writeFailure();
                return;
            }

            var toBuinValue  = (int)toBuin;
            var toAppValue   = (string)toApp;
            var encryptValue = (string)encrypt;

            string timeStamp;
            string nonce;
            string signature;

            if (!queryDict.TryGetValue("timestamp", out timeStamp) ||
                timeStamp == null ||
                !queryDict.TryGetValue("nonce", out nonce) ||
                nonce == null ||
                !queryDict.TryGetValue("msg_signature", out signature) ||
                signature == null)
            {
                Console.WriteLine("invalid timestamp or nonce or msg_signature");
                p.writeFailure();
                return;
            }

            if (toBuinValue != Buin || !toAppValue.Equals(AppId))
            {
                Console.WriteLine("buin or appId is not matched");
                p.writeFailure();
                return;
            }

            var mySignature = Signature.GenerateSignature(Token, timeStamp, nonce, encryptValue);

            if (!signature.Equals(mySignature))
            {
                Console.WriteLine("signature is not matched");
                p.writeFailure();
                return;
            }

            var decryptContent = m_crypto.Decrypt(encryptValue);
            //var msg = new SessionMessage().FromJson(AESCrypto.ToString(decryptContent));
            var msg = new ReceiveMessage().FromJson(AESCrypto.ToString(decryptContent));

            switch (msg.MsgType)
            {
            case Message.MessageTypeImage:
            {
                var msgBody = msg.MsgBody.ToImageBody();
                m_appClient.DownloadFile(msgBody.MediaId, OutDir);
            }
            break;

            case Message.MessageTypeFile:
            {
                var msgBody = msg.MsgBody.ToFileBody();
                m_appClient.DownloadFile(msgBody.MediaId, OutDir);
            }
            break;

            default:
                break;
            }

            Console.WriteLine(msg.ToString());
            Console.WriteLine("packageId: {0}", msg.PackageId);

            p.writeSuccess();
            p.outputStream.WriteLine(msg.PackageId);
        }
Ejemplo n.º 6
0
 public String EncryptToo(String plainText)
 {
     AESCrypto aes = new AESCrypto();
     var cipherText = aes.Encrypt(plainText);
     return cipherText + " | " + aes.Decrypt(cipherText);
 }
Ejemplo n.º 7
0
        public void Decrypt_Test()
        {
            var decryptedStr = AESCrypto.Decrypt("WDDJcMk/+qc0CJ2QYjqXlg==", scrambledKey);

            Assert.IsTrue(decryptedStr == "gang.yang", decryptedStr);
        }