Example #1
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
Example #2
0
        public string Crypt(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));
                    } // End Using md5

                    des3.Mode = System.Security.Cryptography.CipherMode.ECB;
                    using (System.Security.Cryptography.ICryptoTransform cryptoTransform = des3.CreateEncryptor())
                    {
                        byte[] bytes = this.m_encoding.GetBytes(strSourceText);
                        result = System.Convert.ToBase64String(cryptoTransform.TransformFinalBlock(bytes, 0, bytes.Length));
                        System.Array.Clear(bytes, 0, bytes.Length);
                        bytes = null;
                    } // End Using cryptoTransform
                }     // End Using des3

                return(result);
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                // TODO: Log
                throw;
            }
        }
Example #3
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);
        }
Example #4
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
Example #5
0
        } // End Function DeCrypt

        public static string Encrypt(string sourceText)
        {
            string returnValue = "";

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

            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(s_symmetricKey));
                    des3.Mode = System.Security.Cryptography.CipherMode.ECB;
                    using (System.Security.Cryptography.ICryptoTransform desEncryptor = des3.CreateEncryptor())
                    {
                        byte[] buff = System.Text.Encoding.UTF8.GetBytes(sourceText);
                        returnValue = System.Convert.ToBase64String(desEncryptor.TransformFinalBlock(buff, 0, buff.Length));

                        System.Array.Clear(buff, 0, buff.Length);
                        buff = null;
                    } // End Using desEncryptor
                }     // End Using HashMD5
            }         // End Using des3

            return(returnValue);
        } // End Function Crypt
        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;
            }
        }
Example #7
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;
			}
		}
Example #8
0
        } // End Function Crypt

        public static string GenerateKey()
        {
            byte[] bIV  = null;
            byte[] bKey = null;

            using (System.Security.Cryptography.TripleDES des3 = System.Security.Cryptography.TripleDES.Create())
            {
                des3.GenerateKey();
                des3.GenerateIV();
                bIV  = des3.IV;
                bKey = des3.Key;
            } // End Using des3

            return("IV: " + AES.ByteArrayToHexString(bIV) + System.Environment.NewLine + "Key: " + AES.ByteArrayToHexString(bKey));
        } // End Function GenerateKey
Example #9
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);
            }
        }
Example #10
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);
            }
        }
Example #11
0
        public static string GenerateKey()
        {
            string retValue = null;

            using (System.Security.Cryptography.TripleDES des3 = System.Security.Cryptography.TripleDES.Create())
            {
                des3.GenerateKey();
                des3.GenerateIV();

                byte[] bIV  = des3.IV;
                byte[] bKey = des3.Key;

                retValue = "IV: " + AesEncryptionService.ByteArrayToHexString(bIV)
                           + System.Environment.NewLine
                           + "Key: " + AesEncryptionService.ByteArrayToHexString(bKey);
            } // End Using des3

            return(retValue);
        } // End Function GenerateKey
Example #12
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);
        }
Example #13
0
        public static bool _Create_System_String( )
        {
            //Parameters
            System.String str = null;

            //ReturnType/Value
            System.Security.Cryptography.TripleDES returnVal_Real        = null;
            System.Security.Cryptography.TripleDES returnVal_Intercepted = null;

            //Exception
            Exception exception_Real        = null;
            Exception exception_Intercepted = null;

            InterceptionMaintenance.disableInterception( );

            try
            {
                returnValue_Real = System.Security.Cryptography.TripleDES.Create(str);
            }

            catch (Exception e)
            {
                exception_Real = e;
            }


            InterceptionMaintenance.enableInterception( );

            try
            {
                returnValue_Intercepted = System.Security.Cryptography.TripleDES.Create(str);
            }

            catch (Exception e)
            {
                exception_Intercepted = e;
            }


            Return((exception_Real.Messsage == exception_Intercepted.Message) && (returnValue_Real == returnValue_Intercepted));
        }
Example #14
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;
            }
        }
Example #15
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);
        }
Example #16
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);
            }
        }
Example #17
0
        } // End Function Crypt

        public static string GenerateKey()
        {
            string returnValue = null;

            using (System.Security.Cryptography.TripleDES des3 = System.Security.Cryptography.TripleDES.Create())
            {
                des3.GenerateKey();
                des3.GenerateIV();
                byte[] bIV  = des3.IV;
                byte[] bKey = des3.Key;

                returnValue = "IV: " + AES.ByteArrayToHexString(bIV) + System.Environment.NewLine + "Key: " + AES.ByteArrayToHexString(bKey);

                System.Array.Clear(bIV, 0, bIV.Length);
                bIV = null;

                System.Array.Clear(bKey, 0, bKey.Length);
                bKey = null;
            }

            return(returnValue);
        } // End Function GenerateKey