آموزش زبان های برنامه نویسی

آموزش زبان های برنامه نویسی

آموزش انواع زبان های برنامه نویسی
آموزش زبان های برنامه نویسی

آموزش زبان های برنامه نویسی

آموزش انواع زبان های برنامه نویسی

نحوه ارسال پیام به گوشی اندروید از طریق Asp.net


نحوه ارسال پیام به گوشی اندروید از طریق Asp.net


برای ارسال پیام به گوشی اندروید با استفاده از یک سرویس گوگل ما این کار را انجام می دهیم به این صورت که ابتدا باید در جی سی ام گوگل ثبت نام کرده باشیم تا یک سری از کد ها را در اختیار داشته باشیم

این روش با استفاده از Google GCM انجام میدهیم که به ما SenderId و همچنین GoogleAppID را در اختیار ما قرار میدهد تا ما با استفاده از این ابزار ها بتوانیم پیام خود را مثل وایبر به یک نرم افزار توی اندروید بفرستیم

حالا کدهای سی شارپ سمت سرور بصورت زیر است.


        public string SendNotification(string deviceId, string message)

        {

            //string GoogleAppID = "google application id";        

            string GoogleAppID = "AIzaSyADMm";


            var SENDER_ID = "1049xxxxxxxxx";



            var value = message;

            value = HttpContext.Current.Server.UrlEncode(value);



            //string x = n.;

            //Notification message1 = new Notification.Builder().addData("alert", "test message" /*notif.getAlert()*/).build();



            WebRequest tRequest;

            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");

            tRequest.Method = "post";

            //tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";

            tRequest.ContentType = " application/x-www-form-urlencoded";

            //tRequest.ContentType = " application/json";


            tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));


            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));


            //string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";

            string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.price=" + value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";

            Console.WriteLine(postData);

            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            tRequest.ContentLength = byteArray.Length;


            Stream dataStream = tRequest.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);

            dataStream.Close();


            WebResponse tResponse = tRequest.GetResponse();


            dataStream = tResponse.GetResponseStream();


            StreamReader tReader = new StreamReader(dataStream);


            String sResponseFromServer = tReader.ReadToEnd();


            tReader.Close();

            dataStream.Close();

            tResponse.Close();

            return sResponseFromServer;

        }



نحوه استفاده از الگوریتم AES برای رمز نگاری داده ها



نحوه استفاده از الگوریتم AES برای رمز نگاری داده ها


در این بخش قصد داریم که با استفاده از الگوریتم رمزنگاری داده ها


 به وسیله ی AES با استفاده از محیط برنامه نویسی C#این الگوریتم را پیاده سازی کنیم که این الگوریتم


 با ارسال یک متن به صورت تکست دریافت میکند و کلیدی که به وسلیه ی آن 


این متن کد شود. و در هنگام بازگشایی باید حتما آن کلید را داشته باشید.


این دو یوسینگ را ابتدا اضافه میکنیم


using System.Security.Cryptography;
using System.IO;
Encryption
public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
    byte[] encryptedBytes = null;

    // Set your salt here, change it to meet your flavor:
    // The salt bytes must be at least 8 bytes.
    byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

    using (MemoryStream ms = new MemoryStream())
    {
        using (RijndaelManaged AES = new RijndaelManaged())
        {
            AES.KeySize = 256;
            AES.BlockSize = 128;

            var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = key.GetBytes(AES.BlockSize / 8);

            AES.Mode = CipherMode.CBC;

            using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                cs.Close();
            }
            encryptedBytes = ms.ToArray();
        }
    }

    return encryptedBytes;
}
و کد های زیر هم برای بازگشایی متن رمز شده به کار گرفته می شود.
Decryption
public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
    byte[] decryptedBytes = null;

    // Set your salt here, change it to meet your flavor:
    // The salt bytes must be at least 8 bytes.
    byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

    using (MemoryStream ms = new MemoryStream())
    {
        using (RijndaelManaged AES = new RijndaelManaged())
        {
            AES.KeySize = 256;
            AES.BlockSize = 128;

            var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = key.GetBytes(AES.BlockSize / 8);

            AES.Mode = CipherMode.CBC;

            using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                cs.Close();
            }
            decryptedBytes = ms.ToArray();
        }
    }

    return decryptedBytes;
}
یک مثال برای کد گذاری به وسیله الگوریتم AES
public string EncryptText(string input, string password)
{
    // Get the bytes of the string
    byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);

    // Hash the password with SHA256
    passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

    byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);

    string result = Convert.ToBase64String(bytesEncrypted);

    return result;
}
و یک نمونه از بازگشت رمز مورد نظر
public string DecryptText(string input, string password)
{
    // Get the bytes of the string
    byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
    passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

    byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);

    string result = Encoding.UTF8.GetString(bytesDecrypted);

    return result;
}
توجه کنید که متن بالا با استفاده از حالت base64 کد کذاری خواهد شد.
و یک مثال برای رمز گذاری یک فایل 
Encrypt File
public void EncryptFile()
{
    string file = "C:\\SampleFile.DLL";
    string password = "abcd1234";

    byte[] bytesToBeEncrypted = File.ReadAllBytes(file);
    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);

    // Hash the password with SHA256
    passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

    byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);

    string fileEncrypted = "C:\\SampleFileEncrypted.DLL";

    File.WriteAllBytes(fileEncrypted, bytesEncrypted);
}
و با استفاده از کد زیر برای بازگشایی فایل استفاده می شود.
Decrypt File
public void DecryptFile()
{
    string fileEncrypted = "C:\\SampleFileEncrypted.DLL";
    string password = "abcd1234";

    byte[] bytesToBeDecrypted = File.ReadAllBytes(fileEncrypted);
    byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
    passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

    byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);

    string file = "C:\\SampleFile.DLL";
    File.WriteAllBytes(file, bytesDecrypted);
}