Friday, December 21, 2007

Unicode String to Base64 String And Vice Versa

My colleague wanted to write chinese characters to cookie. So I gave him this solution

I copied and modified from this article : http://www.aspfree.com/c/a/C-Sharp/Strings-and-Characters-Part-1/10/

Public Function StringToBase64(ByVal Value As String) As String
Dim inputBytes() As Byte = (New System.Text.UnicodeEncoding).GetBytes(Value)
Dim arrLEngth As Long = CLng(4D * inputBytes.Length / 3D)
If arrLEngth Mod 4 <> 0 Then
arrLEngth += 4 - (arrLEngth Mod 4)
End If
Dim encodedCharArray(arrLEngth - 1) As Char
Convert.ToBase64CharArray(inputBytes, 0, inputBytes.Length, encodedCharArray, 0)
Return New String(encodedCharArray)
End Function

Public Function Base64ToString(ByVal Value As String) As String
Dim decodedByteArray As Byte() = Convert.FromBase64CharArray(Value.ToCharArray, 0, Value.Length)
Return System.Text.UnicodeEncoding.Unicode.GetString(decodedByteArray)
End Function

Wednesday, December 12, 2007

Very Simple Encryption/Decryption Function

This version works for me after few tests (28 dec 2007)

Public Function Decrypt(ByVal myString As String, ByVal myKey As String) As String
myString = myString.Replace("@@@", " ").Replace("$$$", "+")
Dim cryptDES3 As New TripleDESCryptoServiceProvider()
Dim cryptMD5Hash As New MD5CryptoServiceProvider()
cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey))
cryptDES3.Mode = CipherMode.ECB
Dim desdencrypt As ICryptoTransform = cryptDES3.CreateDecryptor()
Dim buff() As Byte = Convert.FromBase64String(myString)
Decrypt = ASCIIEncoding.ASCII.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length))
End Function

Public Function Encrypt(ByVal myString As String, ByVal myKey As String) As String
Dim cryptDES3 As New TripleDESCryptoServiceProvider()
Dim cryptMD5Hash As New MD5CryptoServiceProvider()
cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey))
cryptDES3.Mode = CipherMode.ECB
Dim desdencrypt As ICryptoTransform = cryptDES3.CreateEncryptor()
Dim MyASCIIEncoding = New ASCIIEncoding()
Dim buff() As Byte = ASCIIEncoding.ASCII.GetBytes(myString)
Encrypt = Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length)).Replace(" ", "@@@").Replace("+", "$$$")
End Function

Doesn't work ! I'll have to look for another as I need to use it (27 dec 2007)


Hi I found this workable simple function. Credits to :

Minas Casiou
Information Systems Architect, Senior Consultant, Enabling Technologies Delivery Centre,
Global Transformation Solutions (GTS),
CSC Australia Pty Limited





Public Function EncryptString128Bit(ByVal vstrTextToBeEncrypted As String, ByVal vstrEncryptionKey As String) As String
Dim bytValue() As Byte
Dim bytKey() As Byte
Dim bytEncoded() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62}
Dim intLength As Integer
Dim intRemaining As Integer
Dim objMemoryStream As New MemoryStream()
Dim objCryptoStream As System.Security.Cryptography.CryptoStream
Dim objRijndaelManaged As System.Security.Cryptography.RijndaelManaged
vstrTextToBeEncrypted = StripNullCharacters(vstrTextToBeEncrypted)
bytValue = Encoding.ASCII.GetBytes(vstrTextToBeEncrypted.ToCharArray)
intLength = Len(vstrEncryptionKey)
If intLength >= 32 Then
vstrEncryptionKey = Strings.Left(vstrEncryptionKey, 32)
Else
intLength = Len(vstrEncryptionKey)
intRemaining = 32 - intLength
vstrEncryptionKey = vstrEncryptionKey & Strings.StrDup(intRemaining, "X")
End If
bytKey = Encoding.ASCII.GetBytes(vstrEncryptionKey.ToCharArray)
objRijndaelManaged = New System.Security.Cryptography.RijndaelManaged()
Try
objCryptoStream = New System.Security.Cryptography.CryptoStream(objMemoryStream, _
objRijndaelManaged.CreateEncryptor(bytKey, bytIV), _
System.Security.Cryptography.CryptoStreamMode.Write)
objCryptoStream.Write(bytValue, 0, bytValue.Length)
objCryptoStream.FlushFinalBlock()
bytEncoded = objMemoryStream.ToArray
objMemoryStream.Close()
objCryptoStream.Close()
Catch
End Try
Return Convert.ToBase64String(bytEncoded)
End Function

Public Function DecryptString128Bit(ByVal vstrStringToBeDecrypted As String, ByVal vstrDecryptionKey As String) As String
Dim bytDataToBeDecrypted() As Byte
Dim bytTemp() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62}
Dim objRijndaelManaged As New System.Security.Cryptography.RijndaelManaged()
Dim objMemoryStream As MemoryStream
Dim objCryptoStream As System.Security.Cryptography.CryptoStream
Dim bytDecryptionKey() As Byte
Dim intLength As Integer
Dim intRemaining As Integer
Dim strReturnString As String = String.Empty
bytDataToBeDecrypted = Convert.FromBase64String(vstrStringToBeDecrypted)
intLength = Len(vstrDecryptionKey)
If intLength >= 32 Then
vstrDecryptionKey = Strings.Left(vstrDecryptionKey, 32)
Else
intLength = Len(vstrDecryptionKey)
intRemaining = 32 - intLength
vstrDecryptionKey = vstrDecryptionKey & Strings.StrDup(intRemaining, "X")
End If
bytDecryptionKey = Encoding.ASCII.GetBytes(vstrDecryptionKey.ToCharArray)
ReDim bytTemp(bytDataToBeDecrypted.Length)
objMemoryStream = New MemoryStream(bytDataToBeDecrypted)
Try
objCryptoStream = New System.Security.Cryptography.CryptoStream(objMemoryStream, _
objRijndaelManaged.CreateDecryptor(bytDecryptionKey, bytIV), _
System.Security.Cryptography.CryptoStreamMode.Read)
objCryptoStream.Read(bytTemp, 0, bytTemp.Length)
objCryptoStream.FlushFinalBlock()
objMemoryStream.Close()
objCryptoStream.Close()
Catch
End Try
Return StripNullCharacters(Encoding.ASCII.GetString(bytTemp))
End Function

Public Function StripNullCharacters(ByVal vstrStringWithNulls As String) As String
Dim intPosition As Integer
Dim strStringWithOutNulls As String
intPosition = 1
strStringWithOutNulls = vstrStringWithNulls
Do While intPosition > 0
intPosition = InStr(intPosition, vstrStringWithNulls, vbNullChar)
If intPosition > 0 Then
strStringWithOutNulls = Left$(strStringWithOutNulls, intPosition - 1) & _
Right$(strStringWithOutNulls, Len(strStringWithOutNulls) - intPosition)
End If
If intPosition > strStringWithOutNulls.Length Then
Exit Do
End If
Loop
Return strStringWithOutNulls
End Function

Wednesday, December 5, 2007

WTF are Delegates?

Ok, after quitting reading many sites explaining the hows and whats of Delegates, I found a simple one which makes sense for the below average joe like me.

http://www.15seconds.com/Issue/020815.htm


So, Delegates are like, pointer to function. So instead of Var x = "somevalue", u can set

  Var Click = Button1.Click

Or in Javascript, onload=myfunction; cool huh? I Still don't know practical use of Delegates.

I'll try to think of something.......


function Masturbate(Name)
{
 Shirley gives [Name] a hand
}

Declare MyDelegate(Name)

Main(){
    MyDeletage = AddressOf Masturbate
    Jake = New Man()
    Jake.CheckLegalAge(MyDelegate)
    Bill = New Man()
    Bill.CheckLegalAge(MyDelegate)
}

class Man(){
  function CheckLegalAge(Delegate){
    if This.Age = 18
      Delegate.Invoke(This.Name)
  }
}