Esempio n. 1
0
 public Data Calculate(Data d, Data salt)
 {
     byte[] nb = new byte[d.Bytes.Length + salt.Bytes.Length];
     salt.Bytes.CopyTo(nb, 0);
     d.Bytes.CopyTo(nb, salt.Bytes.Length);
     return CalculatePrivate(nb);
 }
Esempio n. 2
0
 public string decrypt(string code)
 {
     try
     {
         var sym = new Symmetric(_p);
         sym.Key.Text = _k;
         var d = new Encryption.Data();
         d.Hex = code;
         return sym.Decrypt(d).Text;
     }
     catch (Exception)
     {
         return string.Empty;
     }
 }
 public string decrypt(string code)
 {
     try
     {
         var sym = new Symmetric(_p);
         sym.Key.Text = _k;
         var d = new Encryption.Data();
         d.Hex = code;
         return(sym.Decrypt(d).Text);
     }
     catch (Exception)
     {
         return(string.Empty);
     }
 }
Esempio n. 4
0
 public Data Decrypt(Stream encryptedStream, Data key)
 {
     this.Key = key;
     return Decrypt(encryptedStream);
 }
Esempio n. 5
0
 public Data Decrypt(Data encryptedData, Data key)
 {
     this.Key = key;
     return Decrypt(encryptedData);
 }
Esempio n. 6
0
 public Data Encrypt(Stream s, Data key)
 {
     this.Key = key;
     return Encrypt(s);
 }
Esempio n. 7
0
 private Data EncryptPrivate(Data d)
 {
     try
     {
         return new Data(_rsa.Encrypt(d.Bytes, false));
     }
     catch (CryptographicException ex)
     {
         if (ex.Message.ToLower().IndexOf("bad length") > -1)
         {
             throw new CryptographicException("Your data is too large; RSA encryption is designed to encrypt relatively small amounts of data. The exact byte limit depends on the key size. To encrypt more data, use symmetric encryption and then encrypt that symmetric key with asymmetric RSA encryption.", ex);
         }
         else
         {
             throw;
         }
     }
 }
Esempio n. 8
0
 public Data Encrypt(Data d, PublicKey publicKey)
 {
     _rsa.ImportParameters(publicKey.ToParameters());
     return EncryptPrivate(d);
 }
Esempio n. 9
0
 public Data RandomKey()
 {
     _crypto.GenerateKey();
     Data d = new Data(_crypto.Key);
     return d;
 }
Esempio n. 10
0
 public Data RandomInitializationVector()
 {
     _crypto.GenerateIV();
     Data d = new Data(_crypto.IV);
     return d;
 }
Esempio n. 11
0
 private Data DecryptPrivate(Data encryptedData)
 {
     return new Data(_rsa.Decrypt(encryptedData.Bytes, false));
 }
Esempio n. 12
0
 public Data Decrypt(Data encryptedData, string PrivateKeyXML)
 {
     LoadKeyXml(PrivateKeyXML, true);
     return DecryptPrivate(encryptedData);
 }
Esempio n. 13
0
 public Data Decrypt(Data encryptedData, PrivateKey PrivateKey)
 {
     _rsa.ImportParameters(PrivateKey.ToParameters());
     return DecryptPrivate(encryptedData);
 }
Esempio n. 14
0
 public Data Decrypt(Data encryptedData)
 {
     PrivateKey PrivateKey = new PrivateKey();
     PrivateKey.LoadFromConfig();
     return Decrypt(encryptedData, PrivateKey);
 }
Esempio n. 15
0
        public Data Decrypt(Data encryptedData)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(encryptedData.Bytes, 0, encryptedData.Bytes.Length);
            byte[] b = new byte[encryptedData.Bytes.Length];
            Key.Text = symmetricKey;
            ValidateKeyAndIv(false);
            CryptoStream cs = new CryptoStream(ms, _crypto.CreateDecryptor(), CryptoStreamMode.Read);

            try
            {
                cs.Read(b, 0, encryptedData.Bytes.Length - 1);
            }
            catch (CryptographicException ex)
            {
                string ex2 = ex.ToString();
                // throw new CryptographicException("Unable to decrypt data. The provided key may be invalid.", ex);
            }
            finally
            {
                cs.Close();
            }
            return new Data(b);
        }
Esempio n. 16
0
 public Data Calculate(Data d)
 {
     return CalculatePrivate(d.Bytes);
 }
Esempio n. 17
0
 private void ValidateKeyAndIv(bool isEncrypting)
 {
     if (_key.IsEmpty)
     {
         if (isEncrypting)
         {
             _key = RandomKey();
         }
         else
         {
             throw new CryptographicException("No key was provided for the decryption operation!");
         }
     }
     if (_iv.IsEmpty)
     {
         if (isEncrypting)
         {
             _iv = RandomInitializationVector();
         }
         else
         {
             throw new CryptographicException("No initialization vector was provided for the decryption operation!");
         }
     }
     _crypto.Key = _key.Bytes;
     _crypto.IV = _iv.Bytes;
 }
Esempio n. 18
0
 public Data Encrypt(Data d)
 {
     PublicKey PublicKey = DefaultPublicKey;
     return Encrypt(d, PublicKey);
 }
Esempio n. 19
0
 public Data Encrypt(Data d, Data key)
 {
     this.Key = key;
     return Encrypt(d);
 }
Esempio n. 20
0
 public Data Encrypt(Data d, string publicKeyXML)
 {
     LoadKeyXml(publicKeyXML, false);
     return EncryptPrivate(d);
 }
Esempio n. 21
0
        public Data Encrypt(Data d)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Key.Text = symmetricKey;
            ValidateKeyAndIv(true);

            CryptoStream cs = new CryptoStream(ms, _crypto.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(d.Bytes, 0, d.Bytes.Length);
            cs.Close();
            ms.Close();

            return new Data(ms.ToArray());
        }
 public string DecryptData(string encryptedString, EncryptType encryptType)
 {
     string data = "";
     if (encryptedString != null && encryptedString != string.Empty && data.ToLower() != "null")
     {
         switch (encryptType)
         {
             case EncryptType.Symmetric:
                 Symmetric.Provider p = Symmetric.Provider.TripleDES;
                 Data symdecryptedData = null;
                 p = Symmetric.Provider.TripleDES;
                 Symmetric sym2 = new Symmetric(p);
                 //sym2.Key.Text = symmetricKey;
                 System.Text.ASCIIEncoding symEncoding = new System.Text.ASCIIEncoding();
                 Byte[] symBytes = symEncoding.GetBytes(encryptedString);
                 Data symencryptedData = null;
                 symencryptedData = new Data();
                 symencryptedData.Base64 = encryptedString;
                 symdecryptedData = sym2.Decrypt(symencryptedData);
                 data = symdecryptedData.Text;
                 break;
             case EncryptType.Asymmetric:
                 System.Text.ASCIIEncoding asymEncoding = new System.Text.ASCIIEncoding();
                 Data asymdecryptedData = null;
                 Asymmetric asym2 = new Asymmetric();
                 Byte[] asymBytes = asymEncoding.GetBytes(encryptedString);
                 Data asymencryptedData = null;
                 asymencryptedData = new Data();
                 asymencryptedData.Base64 = encryptedString;
                 asymdecryptedData = asym2.Decrypt(asymencryptedData);
                 data = asymdecryptedData.Text;
                 break;
             case EncryptType.Hash:
                 //hashing is one way
                 break;
         }
     }
     return data;
 }
Esempio n. 23
0
 public Data Encrypt(Stream s, Data key, Data iv)
 {
     this.IntializationVector = iv;
     this.Key = key;
     return Encrypt(s);
 }