Solved

encryption

Posted on 2011-09-10
3
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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