Solved

encryption

Posted on 2011-09-10
3
373 Views
Last Modified: 2012-05-12
I am try to encrypt a text for the first time
I have the code  below but when i try to run it with input string

"MessageBox.Show(Encrypt("string is that you need to encrypt", "abc123¿"))"

i get the message
"Specified key is not a valid size for this algorithm." when it reach line 8
 

1.Public Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
2.       Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
3.        Try
4.            Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
5.            Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
6.            Dim des As New DESCryptoServiceProvider
7.            Dim ms As New MemoryStream
8.            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

.
0
Comment
Question by:Anthony Matovu
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36517506
I *think* your problem is that you are using UTF8.GetBytes and a character in unicode is actually 2 bytes. When your call returns, you have a byte array of length 16, not 8. You can easily check this by putting a breakpoint at line 5 and inspecting the value of bykey. Is there a reason not use use ASCII.GetBytes?
0
 
LVL 15

Assisted Solution

by:x77
x77 earned 500 total points
ID: 36519894
Each Crypto Algorihm has some valid LegalSizes for Key  (126, 128, 256 bits by sample).

If you need 128 bits then you need a 16 byte Key, then you can right Pad Your key with spaces.

You can get sizes:

      Dim minSize As Integer = m_CryptoServiceProvider.LegalKeySizes(0).MinSize    
      Dim maxsize As Integer = m_CryptoServiceProvider.LegalKeySizes(0).MaxSize            
      ' Key sizes are in bits

On this page, you can see how to adapt your key to LegalSizes
http://www.kiwisyslog.com/kb/how-to:-call-.net-assemblies-from-kiwi-syslog-server/
0
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36519898
sorry, 128 bits -> 32 Bytes, about 32 Utf8 chars.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question