Buscar contenidos

miércoles, 8 de noviembre de 2017

Encrypt/Decrypt using Self-signed Certificates

Link artículo




        static void Main(string[] args)
        {
            try
            {
                X509Certificate2 myCert =
                   LoadCertificate(StoreLocation.CurrentUser,
                   "CN=test");

                string myText = "This is the text I wish to encrypt";
                Console.WriteLine("UNENCRYPTED: " + myText);

                string encrypted = Encrypt(myCert, myText);

                Console.WriteLine("ENCRYPTED: " + encrypted);

                string decrypted = Decrypt(myCert, encrypted);

                Console.WriteLine("DECRYPTED: " + decrypted);
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION: {0}", e.Message);
            }

        }

        public static X509Certificate2
   LoadCertificate(StoreLocation storeLocation,
   string certificateName)
        {
            X509Store store = new X509Store(storeLocation);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection certCollection =
               store.Certificates;
            X509Certificate2 cert =
               certCollection.Cast<X509Certificate2>().FirstOrDefault
               (c => c.Subject == certificateName);
            if (cert == null)
                Console.WriteLine("NO Certificate named " +
                   certificateName + " was found in your certificate store");
            store.Close();
            return cert;
        }


        private static string Encrypt(X509Certificate2 x509, string stringToEncrypt)
        {
            if (x509 == null || string.IsNullOrEmpty(stringToEncrypt))
                throw new Exception("A x509 certificate and string for encryption must be provided");

            RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)x509.PublicKey.Key;
            byte[] bytestoEncrypt = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt);
            byte[] encryptedBytes = rsa.Encrypt(bytestoEncrypt, false);
            return Convert.ToBase64String(encryptedBytes);
        }

        private static string Decrypt(X509Certificate2 x509, string stringTodecrypt)
        {
            if (x509 == null || string.IsNullOrEmpty(stringTodecrypt))
                throw new Exception("A x509 certificate and string for decryption must be provided");

            if (!x509.HasPrivateKey)
                throw new Exception("x509 certicate does not contain a private key for decryption");

            RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)x509.PrivateKey;
            byte[] bytestodecrypt = Convert.FromBase64String(stringTodecrypt);
            byte[] plainbytes = rsa.Decrypt(bytestodecrypt, false);
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            return enc.GetString(plainbytes);
        }


jueves, 2 de noviembre de 2017

Browser Cookie Limits


http://browsercookielimits.squawky.net/



Browser Max Cookies Max Size Per Cookie Max Size Per Domain1 Usage2
Chrome 4        
Chrome 5-7 70 4096 bytes NA  
Chrome 8-39* 180 4096 bytes NA 60.10%
* Checked on 8/10/12/13/14/15/17/25/39. Unverified on 9/11/16/18-24, just assumed        
Chrome Mobile Appears same as Chrome. Verified on Chrome Mobile 39.      
FireFox 2/3.6.6 50 4097 characters NA  
FireFox 3.6.13-19* 150 4097 characters NA 1.7% (assuming, versions before 25)
* verified on 3.6.13/4/8/9/10/14/19. unverified 5-8/11-13/15-18, just assumed        
FireFox 24-34* 150 4097 bytes NA 21.70%
* verified on 24/25/34        
IE 6 unpatched 20 4096 characters 4096 characters  
IE 6 50 4096 characters 4096 characters  
IE 7 unpatched 20 4095 characters 4095 characters  
IE 7 50 4095 characters 4095 characters 0.30%
IE 8/9/10/11 50 5117 characters 10234 characters 9.50%
Opera 8/9/10 30 4096 bytes 4096 bytes  
Opera 11 60 4096 bytes 4096 bytes  
Opera 12 60 5117 bytes 12093 bytes  
Opera 25       0.60%
Opera 26 180 4096 bytes N/A 0.20%
Safari 3        
Safari 4       0.10%
Safari 5 600 4096 bytes 4096 bytes  
Safari 6       0.30%
Safari 7 N/A 4097 bytes 8180 bytes 1.40%
Safari 8       1.80%
Safari ? on mac3 6004 4093 bytes4 most likely 4093 bytes4  
Android 2.1/2.3.4 50 4096 bytes NA  
Safari Mobile 5.1 6005 4093 bytes5 4093 bytes