예제 #1
0
파일: Crypto.cs 프로젝트: fuzzbuck/catbox
        public static string Encrypt(string plainText, string password = "******")
        {
            if (plainText == null)
            {
                throw new ArgumentNullException("plainText");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            // Will return this
            var CryptoJSFormat = new CryptoJSFormat();

            // Generate random 8 byte salt
            Random rnd = new Random();

            byte[] salt = new byte[8];
            rnd.NextBytes(salt);

            CryptoJSFormat.s = Parsers.ByteArrayToHexViaLookup32Unsafe(salt);
            // Convert plain text to bytes
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);

            // create new password derived bytes using password/salt
            using (OpenSslCompatDeriveBytes pdb = new OpenSslCompatDeriveBytes(password, salt))
            {
                using (Rijndael aes = RijndaelManaged.Create())
                {
                    // Generate key and iv from password/salt and pass to aes
                    aes.Key       = pdb.GetBytes(aes.KeySize / 8);
                    aes.IV        = pdb.GetBytes(aes.BlockSize / 8);
                    aes.Mode      = CipherMode.CBC;
                    aes.BlockSize = 128;

                    CryptoJSFormat.iv = Parsers.ByteArrayToHexViaLookup32Unsafe(aes.IV);

                    // Open a new memory stream to write the encrypted data to
                    using (MemoryStream ms = new MemoryStream())
                    {
                        // Create a crypto stream to perform encryption
                        using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            // write encrypted bytes to memory
                            cs.Write(plainBytes, 0, plainBytes.Length);
                        }
                        // get the cipher bytes from memory
                        byte[] cipherBytes = ms.ToArray();

                        // convert cipher array to base 64 string
                        CryptoJSFormat.ct = Convert.ToBase64String(cipherBytes);
                    }
                    aes.Clear();
                }
            }

            return(JsonConvert.SerializeObject(CryptoJSFormat));
        }
예제 #2
0
파일: Crypto.cs 프로젝트: fuzzbuck/catbox
        public static string Decrypt(string json_string, string password)
        {
            CryptoJSFormat json = JsonConvert.DeserializeObject <CryptoJSFormat>(json_string);

            if (json.s == null || json.s.Length < 1)
            {
                return(null);
            }

            try
            {
                var hashKey = new OpenSslCompatDeriveBytes(password,
                                                           parseHex(json.s));

                byte[] key = hashKey.GetBytes(32);

                return(DecryptDirect(json.ct, key, parseHex(json.iv)));
            }catch (Exception)
            {
                return(null);
            }
        }