Cassio
Junior Contributor
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!
Thanks!
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);
}
}
}
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