Cassio Posted April 2, 2003 Posted April 2, 2003 Hi! Is there a way I can convert a text file into a binary file and then be able to convert it into a text file again? Thanks! Quote Stream of Consciousness (My blog)
Heiko Posted April 2, 2003 Posted April 2, 2003 Textfiles as (in my unimportant opinion) just a special case of binary files. :) What exactly do you mean by binary / converting? Could you give an example? Quote .nerd
Cassio Posted April 2, 2003 Author Posted April 2, 2003 I want to write to a xml file, but I dont want the user to see whats written. So I want to convert it to binary when the program close. When the program starts it converts to xml, use it, and then write to a binary file format again. Quote Stream of Consciousness (My blog)
*Gurus* divil Posted April 2, 2003 *Gurus* Posted April 2, 2003 "Binary" doesn't mean the file is unreadable... it sounds like you want to encrypt it. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Cassio Posted April 2, 2003 Author Posted April 2, 2003 I encrypted it using an example I saw in the SDK, but I have no idea of how to decrypt it. Do you have a sample or something? thanks Quote Stream of Consciousness (My blog)
Eloff Posted April 2, 2003 Posted April 2, 2003 Binary files are quite readable for anybody in the know. Try encrypting them with Rijndael, leave them as text but encrypt them. Without access to your key and init vector, it's practically unbreakable. Here's some code I use (first example is C# but may work in C++ also, second is visual basic). using System; using System.Security.Cryptography; namespace RijndaelManaged { /// <summary> /// This class contains only 2 methods: Encrypt and Decrypt. /// They both require two byte array parameters like the ones below. /// ///byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; ///byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; /// </summary> public class DsCrypt { public DsCrypt() { } public string Encrypt(string s, byte[] key, byte[] iv) { System.Security.Cryptography.RijndaelManaged rij = new System.Security.Cryptography.RijndaelManaged(); rij.Key = key; rij.IV = iv; byte[] input = System.Text.Encoding.UTF8.GetBytes(s); byte[] output = rij.CreateEncryptor().TransformFinalBlock(input, 0, input.Length); return Convert.ToBase64String(output); } public string Decrypt(string s, byte[] key, byte[] iv) { System.Security.Cryptography.RijndaelManaged rij = new System.Security.Cryptography.RijndaelManaged(); rij.Key = key; rij.IV = iv; byte[] input = Convert.FromBase64String(s); byte[] output = rij.CreateDecryptor().TransformFinalBlock(input, 0, input.Length); return System.Text.Encoding.UTF8.GetString(output); } } } VB Implementation (more complete): Imports System.Configuration.ConfigurationSettings Imports System.Security.Cryptography Imports System.Text.Encoding Imports System.Convert Public Module SecurityProcs Public Function ComputeMD5Hash(ByVal s As String, Optional ByRef MD5 As MD5CryptoServiceProvider = Nothing) As String If MD5 Is Nothing Then Return ToBase64String(New MD5CryptoServiceProvider().ComputeHash(ASCII.GetBytes(s))) Else Return ToBase64String(MD5.ComputeHash(ASCII.GetBytes(s))) End If End Function Public Function CompareStringToHash(ByVal sString As String, ByVal sHash As String, Optional ByRef MD5 As MD5CryptoServiceProvider = Nothing) As Boolean If MD5 Is Nothing Then Return Equals(ToBase64String(New MD5CryptoServiceProvider().ComputeHash(ASCII.GetBytes(sString))), sHash) Else Return Equals(ToBase64String(MD5.ComputeHash(ASCII.GetBytes(sString))), sHash) End If End Function Public Function GenerateKey(Optional ByRef Rij As RijndaelManaged = Nothing) As Byte() If Rij Is Nothing Then Rij = New RijndaelManaged() Rij.KeySize = 256 Rij.GenerateKey() Return Rij.Key End Function Public Function GenerateIV(Optional ByRef Rij As RijndaelManaged = Nothing) As Byte() If Rij Is Nothing Then Rij = New RijndaelManaged() Rij.GenerateIV() Return Rij.IV End Function Public Function Encrypt(ByVal s As String, ByVal Key As Byte(), ByVal IV As Byte(), Optional ByVal Rij As RijndaelManaged = Nothing) As String Dim b As Byte() = [unicode].GetBytes(s) If Rij Is Nothing Then Return System.Convert.ToBase64String((New RijndaelManaged().CreateEncryptor(Key, IV).TransformFinalBlock(b, 0, b.Length))) Else Return System.Convert.ToBase64String((Rij.CreateEncryptor(Key, IV).TransformFinalBlock(b, 0, b.Length))) End If End Function Public Function Encrypt(ByVal s As String, ByVal IV As Byte(), ByRef Rij As RijndaelManaged) As String Dim b As Byte() = [unicode].GetBytes(s) Return System.Convert.ToBase64String((Rij.CreateEncryptor(Rij.Key, IV).TransformFinalBlock(b, 0, b.Length))) End Function Public Function Decrypt(ByVal s As String, ByVal Key As Byte(), ByVal IV As Byte(), Optional ByRef Rij As RijndaelManaged = Nothing) As String Dim b As Byte() = System.Convert.FromBase64String(s) If Rij Is Nothing Then Return [unicode].GetString(New RijndaelManaged().CreateDecryptor(Key, IV).TransformFinalBlock(b, 0, b.Length)) Else Return [unicode].GetString((Rij.CreateDecryptor(Key, IV).TransformFinalBlock(b, 0, b.Length))) End If End Function Public Function Decrypt(ByVal s As String, ByVal IV As Byte(), ByVal Rij As RijndaelManaged) As String Dim b As Byte() = System.Convert.FromBase64String(s) Return [unicode].GetString((Rij.CreateDecryptor(Rij.Key, IV).TransformFinalBlock(b, 0, b.Length))) End Function End Module Quote
Cassio Posted April 2, 2003 Author Posted April 2, 2003 Thanks! Ill try that when I get home. Quote Stream of Consciousness (My blog)
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.