Convert text stream to binary

Cassio

Junior Contributor
Joined
Nov 30, 2002
Messages
276
Location
Rio de Janeiro
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!
 
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?
 
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.
 
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
 
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).

Code:
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):

Code:
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
 
Back
Top