コード例 #1
0
        private static EncryptionResultViewModel EncryptStringToBytes(string plainText)
        {
            EncryptionResultViewModel result = new EncryptionResultViewModel();

            try
            {
                //   ********************************************************************
                //   ******   Encryption Key must be 256 bits long (32 bytes)      ******
                //   ******   If it is longer than 32 bytes it will be truncated.  ******
                //   ******   If it is shorter than 32 bytes it will be padded     ******
                //   ******   with upper-case Xs.                                  ******
                //   ********************************************************************
                var randomKey = RandomString();
                var key       = MyKey + randomKey;
                var intLength = key.Length;
                if (intLength >= 32)
                {
                    key = Left(key, 32);
                }
                else
                {
                    var intRemaining = 32 - intLength;
                    key = key + new string(Convert.ToChar("X"), intRemaining);
                }
                var byteKey = Encoding.ASCII.GetBytes(key.ToCharArray());

                // Create an RijndaelManaged object
                // with the specified key and IV.
                using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
                {
                    // Create an encryptor to perform the stream transform.
                    ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(byteKey, ByteIv);

                    // create the streams used for encryption
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter sw = new StreamWriter(cs))
                            {
                                // write all data to the stream
                                sw.Write((plainText));
                            }

                            result.EncryptedString = ConvertArrayToB64String(ms.ToArray());
                        }
                    }

                    result.UniqueKey = randomKey;
                    result.Success   = true;
                }
            }
            catch (Exception e)
            {
                // ignored
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Encrypt sensitive data with Rijndael
        /// </summary>
        /// <param name="oInput"></param>
        /// <returns></returns>
        public static EncryptionResultViewModel Encrypt(object oInput)
        {
            EncryptionResultViewModel result = new EncryptionResultViewModel();

            string cInput = null;

            if (oInput != null)
            {
                cInput = Convert.ToString(oInput);
                if (!string.IsNullOrEmpty(cInput))
                {
                    result = EncryptStringToBytes(cInput);
                }
            }

            return(result);
        }