Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

.nerd
Posted
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.
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...