Exemple #1
0
        public CryptoToken( IDictionary<string, string> dictionary, EncryptionKey encryptionKey, DateTime expiration )
        {
            DictionaryContents = new Dictionary<string, string>( dictionary );

             _encryptionKey = encryptionKey;
             ExpirationDate = expiration;
        }
Exemple #2
0
        public CryptoToken( NameValueCollection nameValueCollection, EncryptionKey encryptionKey, DateTime expiration )
        {
            DictionaryContents = BuildDictionaryFromNameValueCollection( nameValueCollection );

             _encryptionKey = encryptionKey;
             ExpirationDate = expiration;
        }
Exemple #3
0
        public CryptoToken( string encryptedToken, EncryptionKey encryptionKey, InitializationVector initializationVector = null )
        {
            _encryptionKey = encryptionKey;

             if ( !string.IsNullOrEmpty( encryptedToken ) )
             {
            var encryptor = new SymmetricCrypto( _encryptionKey );
            string plainTokenText = string.Empty;

            try
            {
               if ( initializationVector != null )
               {
                  byte[] encryptedValue = Convert.FromBase64String( encryptedToken );
                  byte[] encryptedValueAndInitializationVector = encryptedValue.Concat( initializationVector.InitializationVectorBytes ).ToArray();
                  plainTokenText = encryptor.Decrypt( Convert.ToBase64String( encryptedValueAndInitializationVector )  );
               }
               else
               {
                  plainTokenText = encryptor.Decrypt( encryptedToken );
               }

            }
            catch ( CryptographicException )
            {
               _isValid = false;
               // CryptoException generally indicates the enc key/padding was wrong and that probably means someone (evil) tried to make up a crypto token.
            }
            catch ( FormatException )
            {
               _isValid = false;
               // FormatException indicates that the encrypted string value provided by the user was not a Base64-encoded string
            }
            catch ( ArgumentNullException )
            {
               _isValid = false;
               // ArgumentNullException indicates that something went wrong when trying to read the Cryptographic token likely due to the length being incorrect.
            }

            if ( _isValid )
            {
                  DictionaryContents = JsonConvert.DeserializeObject<Dictionary<string, string>>( plainTokenText );

                  if ( DictionaryContents.Keys.Contains( "expirationdate" ) )
                  {
                     ExpirationDate = new DateTime( Int64.Parse( DictionaryContents["expirationdate"].ToString() ) );
                     DictionaryContents.Remove( "expirationdate" );
                  }
            }
             }
        }
 public UrlSafeCryptoToken( IDictionary<string, string> dictionary, EncryptionKey encryptionKey, DateTime expiration )
     : base(dictionary, encryptionKey, expiration)
 {
 }
 public UrlSafeCryptoToken( NameValueCollection nameValueCollection, EncryptionKey encryptionKey, DateTime expiration )
     : base(nameValueCollection, encryptionKey, expiration)
 {
 }
 public UrlSafeCryptoToken( string urlSafeTokenString, EncryptionKey encryptionKey )
     : base(ConvertUrlSafeTokenStringToNormalCryptoTokenString( urlSafeTokenString ), encryptionKey)
 {
 }
 public UrlSafeCryptoToken( string urlSafeTokenString, EncryptionKey encryptionKey, InitializationVector initializationVector )
     : base(ConvertUrlSafeTokenStringToNormalCryptoTokenString( urlSafeTokenString ), encryptionKey, initializationVector)
 {
 }
Exemple #8
0
 public SymmetricCrypto( EncryptionKey key )
 {
     _key = key;
 }