Exemplo n.º 1
0
        public string Descriptografar(string criptografia)
        {
            // Cria objeto para criptografia
            System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();
            des.Mode    = System.Security.Cryptography.CipherMode.CBC;
            des.KeySize = 192;

            byte[] chaveBytes;
            byte[] criptografiaBytes;
            byte[] mensagemBytes;
            string mensagem;

            // Transforma chave e mensagem em array de byts
            chaveBytes    = Encoding.UTF8.GetBytes("chavede16digitos");
            mensagemBytes = Convert.FromBase64String(criptografia);

            // Realiza criptografia
            System.Security.Cryptography.ICryptoTransform cryptor = des.CreateDecryptor(chaveBytes, chaveBytes);
            criptografiaBytes = cryptor.TransformFinalBlock(mensagemBytes, 0, mensagemBytes.Length);
            cryptor.Dispose();

            // Transforma criptografia em string
            mensagem = Encoding.UTF8.GetString(criptografiaBytes);
            return(mensagem);
        }
Exemplo n.º 2
0
        // http://www.codeproject.com/KB/aspnet/ASPNET_20_Webconfig.aspx
        // http://www.codeproject.com/KB/database/Connection_Strings.aspx
        public static string DeCrypt(string SourceText)
        {
            string strReturnValue = "";

            if (string.IsNullOrEmpty(SourceText))
            {
                return(strReturnValue);
            } // End if (string.IsNullOrEmpty(SourceText))


            using (System.Security.Cryptography.TripleDES des3 = System.Security.Cryptography.TripleDES.Create())
            {
                using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
                {
                    des3.Key  = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSymmetricKey));
                    des3.Mode = System.Security.Cryptography.CipherMode.ECB;

                    System.Security.Cryptography.ICryptoTransform desdencrypt = des3.CreateDecryptor();
                    byte[] buff = System.Convert.FromBase64String(SourceText);
                    strReturnValue = System.Text.Encoding.UTF8.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
                } // End Using md5
            }     // End Using des3

            return(strReturnValue);
        } // End Function DeCrypt
        public override void init(int mode, byte[] key, byte[] iv)
        {
            triDes         = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            triDes.Mode    = System.Security.Cryptography.CipherMode.CBC;
            triDes.Padding = System.Security.Cryptography.PaddingMode.None;
            //String pad="NoPadding";
            //if(padding) pad="PKCS5Padding";
            byte[] tmp;
            if (iv.Length > ivsize)
            {
                tmp = new byte[ivsize];
                Array.Copy(iv, 0, tmp, 0, tmp.Length);
                iv = tmp;
            }
            if (key.Length > bsize)
            {
                tmp = new byte[bsize];
                Array.Copy(key, 0, tmp, 0, tmp.Length);
                key = tmp;
            }

            try
            {
                //      cipher=javax.crypto.Cipher.getInstance("DESede/CBC/"+pad);

                /*
                 *        // The following code does not work on IBM's JDK 1.4.1
                 *        SecretKeySpec skeySpec = new SecretKeySpec(key, "DESede");
                 *        cipher.init((mode==ENCRYPT_MODE?
                 *                 javax.crypto.Cipher.ENCRYPT_MODE:
                 *                 javax.crypto.Cipher.DECRYPT_MODE),
                 *                skeySpec, new IvParameterSpec(iv));
                 */
                //      DESedeKeySpec keyspec=new DESedeKeySpec(key);
                //      SecretKeyFactory keyfactory=SecretKeyFactory.getInstance("DESede");
                //      SecretKey _key=keyfactory.generateSecret(keyspec);
                //      cipher.init((mode==ENCRYPT_MODE?
                //		   javax.crypto.Cipher.ENCRYPT_MODE:
                //		   javax.crypto.Cipher.DECRYPT_MODE),
                //		  _key, new IvParameterSpec(iv));
                cipher = (mode == ENCRYPT_MODE?
                          triDes.CreateEncryptor(key, iv):
                          triDes.CreateDecryptor(key, iv));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                cipher = null;
            }
        }
Exemplo n.º 4
0
		public override void init(int mode, byte[] key, byte[] iv) 
		{
			triDes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
			triDes.Mode=System.Security.Cryptography.CipherMode.CBC;
			triDes.Padding=System.Security.Cryptography.PaddingMode.None;
			//String pad="NoPadding";      
			//if(padding) pad="PKCS5Padding";
			byte[] tmp;
			if(iv.Length>ivsize)
			{
				tmp=new byte[ivsize];
				Array.Copy(iv, 0, tmp, 0, tmp.Length);
				iv=tmp;
			}
			if(key.Length>bsize)
			{
				tmp=new byte[bsize];
				Array.Copy(key, 0, tmp, 0, tmp.Length);
				key=tmp;
			}

			try
			{
				//      cipher=javax.crypto.Cipher.getInstance("DESede/CBC/"+pad);
				/*
					  // The following code does not work on IBM's JDK 1.4.1
					  SecretKeySpec skeySpec = new SecretKeySpec(key, "DESede");
					  cipher.init((mode==ENCRYPT_MODE?
						   javax.crypto.Cipher.ENCRYPT_MODE:
						   javax.crypto.Cipher.DECRYPT_MODE),
						  skeySpec, new IvParameterSpec(iv));
				*/
				//      DESedeKeySpec keyspec=new DESedeKeySpec(key);
				//      SecretKeyFactory keyfactory=SecretKeyFactory.getInstance("DESede");
				//      SecretKey _key=keyfactory.generateSecret(keyspec);
				//      cipher.init((mode==ENCRYPT_MODE?
				//		   javax.crypto.Cipher.ENCRYPT_MODE:
				//		   javax.crypto.Cipher.DECRYPT_MODE),
				//		  _key, new IvParameterSpec(iv));
				cipher = (mode==ENCRYPT_MODE? 
					triDes.CreateEncryptor(key, iv):
					triDes.CreateDecryptor(key, iv));
			}
			catch(Exception e)
			{
				Console.WriteLine(e);
				cipher=null;
			}
		}
Exemplo n.º 5
0
        private string DecryptString(string value)
        {
            try
            {
                byte[] resultBA = new byte[value.Length / 2], valueBA = new byte[value.Length / 2];
                byte[] iv       = new byte[] { 0x14, 0xD7, 0x5B, 0xA2, 0x47, 0x83, 0x0F, 0xC4 };
                System.Text.ASCIIEncoding ascEncoding = new System.Text.ASCIIEncoding();
                byte[] key = new byte[24] {
                    0x21, 0x24, 0x25, 0x23, 0x34, 0x32, 0x37, 0x34, 0x38, 0x6A, 0x73, 0x54, 0x54, 0x4C, 0x7A, 0X51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                };

                MemoryStream memStream = new MemoryStream();
                byte[]       tempBA    = HexStringToBytes(value);
                memStream.Write(tempBA, 0, tempBA.Length);
                memStream.Position = 0;

                System.Security.Cryptography.TripleDES
                    cryptoServiceProvider = System.Security.Cryptography.TripleDESCryptoServiceProvider.Create();

                System.Security.Cryptography.ICryptoTransform
                    decryptor = cryptoServiceProvider.CreateDecryptor(key, iv);

                System.Security.Cryptography.CryptoStream
                    cStream = new System.Security.Cryptography.CryptoStream(
                    memStream,
                    decryptor,
                    System.Security.Cryptography.CryptoStreamMode.Read);

                cStream.Read(resultBA, 0, resultBA.Length);
                cStream.Close();

                // Find the first zero
                int i = 0;
                for (; i < resultBA.GetLength(0); i++)
                {
                    if (resultBA[i] == 0)
                    {
                        break;
                    }
                }
                return(ascEncoding.GetString(resultBA, 0, i));
            }
            catch (Exception exc)
            {
                MessageBox.Show("Decryption failure.  " + exc.ToString());
                return(value);
            }
        }
Exemplo n.º 6
0
        static internal string DecryptString(string value)
        {
            try
            {
                byte[] resultBA = new byte[value.Length / 2], valueBA = new byte[value.Length / 2];
                byte[] iv       = new byte[] { 0x14, 0xD7, 0x5B, 0xA2, 0x47, 0x83, 0x0F, 0xC4 };
                System.Text.ASCIIEncoding ascEncoding = new System.Text.ASCIIEncoding();
                byte[] key = new byte[24] {
                    0x21, 0x24, 0x25, 0x23, 0x34, 0x32, 0x37, 0x34, 0x38, 0x6A, 0x73, 0x54, 0x54, 0x4C, 0x7A, 0X51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                };

                MemoryStream memStream = new MemoryStream();
                byte[]       tempBA    = InternalMethods.HexStringToBytes(value);
                memStream.Write(tempBA, 0, tempBA.Length);
                memStream.Position = 0;

                System.Security.Cryptography.TripleDES
                    cryptoServiceProvider = System.Security.Cryptography.TripleDESCryptoServiceProvider.Create();

                System.Security.Cryptography.ICryptoTransform
                    decryptor = cryptoServiceProvider.CreateDecryptor(key, iv);

                System.Security.Cryptography.CryptoStream
                    cStream = new System.Security.Cryptography.CryptoStream(
                    memStream,
                    decryptor,
                    System.Security.Cryptography.CryptoStreamMode.Read);

                cStream.Read(resultBA, 0, resultBA.Length);
                cStream.Close();

                // Find the first zero
                int i = 0;
                for (; i < resultBA.GetLength(0); i++)
                {
                    if (resultBA[i] == 0)
                    {
                        break;
                    }
                }
                return(ascEncoding.GetString(resultBA, 0, i));
            }
            catch (Exception exc)
            {
                EPSEventLog.WriteEntry("Decryption failure.  Returning original value" + Environment.NewLine + exc.Source, EventLogEntryType.Error);
                return(value);
            }
        }
Exemplo n.º 7
0
        public string DecryptString(string value, string keyString)
        {
            try
            {
                byte[] resultBA = new byte[value.Length / 2], valueBA = new byte[value.Length / 2];
                byte[] iv       = new byte[] { 0x14, 0xD7, 0x5B, 0xA2, 0x47, 0x83, 0x0F, 0xC4 };
                System.Text.ASCIIEncoding ascEncoding = new System.Text.ASCIIEncoding();
                byte[] key = new byte[24];
                ascEncoding.GetBytes(keyString, 0, keyString.Length < 24?keyString.Length:24, key, 0);

                MemoryStream memStream = new MemoryStream();
                byte[]       tempBA    = InternalMethods.HexStringToBytes(value);
                memStream.Write(tempBA, 0, tempBA.Length);
                memStream.Position = 0;

                System.Security.Cryptography.TripleDES
                    cryptoServiceProvider = System.Security.Cryptography.TripleDESCryptoServiceProvider.Create();

                System.Security.Cryptography.ICryptoTransform
                    decryptor = cryptoServiceProvider.CreateDecryptor(key, iv);

                System.Security.Cryptography.CryptoStream
                    cStream = new System.Security.Cryptography.CryptoStream(
                    memStream,
                    decryptor,
                    System.Security.Cryptography.CryptoStreamMode.Read);

                cStream.Read(resultBA, 0, resultBA.Length);
                cStream.Close();

                return(ascEncoding.GetString(resultBA));
            }
            catch (Exception exc)
            {
                LogEvent("Decryption failure.  Returning original value" +
                         Environment.NewLine + exc.Source, "DecryptString()", exc.ToString(), 3);
                return(value);
            }
        }
Exemplo n.º 8
0
        public string Descriptografar(string chave, string criptografia)
        {
            System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();
            des.Mode    = System.Security.Cryptography.CipherMode.CBC;
            des.KeySize = 192;

            byte[] chaveBytes;
            byte[] criptografiaBytes;
            byte[] mensagemBytes;
            string mensagem;

            chaveBytes    = Encoding.UTF8.GetBytes(chave);
            mensagemBytes = Convert.FromBase64String(criptografia);


            System.Security.Cryptography.ICryptoTransform cryptor = des.CreateDecryptor(chaveBytes, chaveBytes);
            criptografiaBytes = cryptor.TransformFinalBlock(mensagemBytes, 0, mensagemBytes.Length);
            cryptor.Dispose();

            mensagem = Encoding.UTF8.GetString(criptografiaBytes);
            return(mensagem);
        }
Exemplo n.º 9
0
        // ----- Decrypt the 3DES encrypted RSA private key ----------

        static byte[] DecryptKey(byte[] cipherData, byte[] desKey, byte[] IV)
        {
            System.IO.MemoryStream memst = new System.IO.MemoryStream();
            System.Security.Cryptography.TripleDES alg = System.Security.Cryptography.TripleDES.Create();
            alg.Key = desKey;
            alg.IV  = IV;
            try
            {
                System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(memst, alg.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                cs.Write(cipherData, 0, cipherData.Length);
                cs.Close();
            }
            catch (System.Exception exc)
            {
                System.Console.WriteLine(exc.Message);
                return(null);
            }
            byte[] decryptedData = memst.ToArray();
            return(decryptedData);
        }
Exemplo n.º 10
0
        }   // End Constructor

        // http://www.codeproject.com/KB/aspnet/ASPNET_20_Webconfig.aspx
        // http://www.codeproject.com/KB/database/Connection_Strings.aspx
        public string DeCrypt(string strSourceText)
        {
            string result = null;

            try
            {
                using (System.Security.Cryptography.TripleDES des3 = System.Security.Cryptography.TripleDES.Create())
                {
                    using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
                    {
                        des3.Key  = md5.ComputeHash(this.m_encoding.GetBytes(this.m_symmetricKey));
                        des3.Mode = System.Security.Cryptography.CipherMode.ECB;
                    } // End Using md5

                    if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(strSourceText, "", false) != 0)
                    {
                        using (System.Security.Cryptography.ICryptoTransform cryptoTransform = des3.CreateDecryptor())
                        {
                            byte[] array = System.Convert.FromBase64String(strSourceText);
                            des3.Clear();
                            result = this.m_encoding.GetString(cryptoTransform.TransformFinalBlock(array, 0, array.Length));
                            System.Array.Clear(array, 0, array.Length);
                            array = null;
                        } // End Using cryptoTransform
                    }
                    else
                    {
                        result = "";
                    }
                } // End Using des3

                return(result);
            } // End Try
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                // TODO: Log
                throw;
            }
        }
Exemplo n.º 11
0
        //Protected Shared strSymmetricKey As String = "Als symmetrischer Key kann irgendein Text verwendet werden. äöü'"

        // http://www.codeproject.com/KB/aspnet/ASPNET_20_Webconfig.aspx
        // http://www.codeproject.com/KB/database/Connection_Strings.aspx
        public static string Decrypt(string sourceText)
        {
            string returnValue = "";

            if (sourceText == null)
            {
                return(sourceText);
            }

            if (string.IsNullOrEmpty(sourceText))
            {
                return(returnValue);
            }


            using (System.Security.Cryptography.TripleDES Des = System.Security.Cryptography.TripleDES.Create())
            {
                using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
                {
                    Des.Key  = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(s_symmetricKey));
                    Des.Mode = System.Security.Cryptography.CipherMode.ECB;

                    using (System.Security.Cryptography.ICryptoTransform desDecryptor = Des.CreateDecryptor())
                    {
                        byte[] buff = System.Convert.FromBase64String(sourceText);
                        returnValue = System.Text.Encoding.UTF8.GetString(desDecryptor.TransformFinalBlock(buff, 0, buff.Length));

                        System.Array.Clear(buff, 0, buff.Length);
                        buff = null;
                    } // End Using desDecryptor
                }     // End Using md5
            }         // End Using Des

            return(returnValue);
        } // End Function DeCrypt