Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Everyone..

I use encryption and decryption function given bellow but it not working for 64 bit character.

Please help me what can i do so it works perfect anywhere or if any one have better function for encryption and decryption can you provide me it.


VB
Public Shared Function EncryptText(ByVal strText As String) As String
      Return Encrypt(strText, "&%#@?,:*")
  End Function

  'Decrypt the text
  Public Shared Function DecryptText(ByVal strText As String) As String
      Return Decrypt(strText, "&%#@?,:*")
  End Function

  'The function used to encrypt the text
  Private Shared Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
      Dim byKey() As Byte = {}
      Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

      Try
          byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))

          Dim des As New DESCryptoServiceProvider
          Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
          Dim ms As New MemoryStream
          Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
          cs.Write(inputByteArray, 0, inputByteArray.Length)
          cs.FlushFinalBlock()
          Return Convert.ToBase64String(ms.ToArray())

      Catch ex As Exception
          Return ex.Message
      End Try

  End Function

  'The function used to decrypt the text
  Private Shared Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
      Dim byKey() As Byte = {}
      Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
      Dim inputByteArray(strText.Length) As Byte

      Try
          byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
          Dim des As New DESCryptoServiceProvider
          inputByteArray = Convert.FromBase64String(strText)
          Dim ms As New MemoryStream
          Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)

          cs.Write(inputByteArray, 0, inputByteArray.Length)
          cs.FlushFinalBlock()
          Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

          Return encoding.GetString(ms.ToArray())

      Catch ex As Exception
          Return ex.Message
      End Try

  End Function


Thank you..!!
Posted
Updated 1-Nov-12 23:08pm
v2

For other forms of encryption see

.NET Encryption Simplified[^]

And Google is your best friend.
 
Share this answer
 
UTF-8 is variable length, this will explain it and provide a solution:
http://msdn.microsoft.com/en-us/library/system.text.encoding.utf8%28v=vs.80%29.aspx[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900