Solved

encryption

Posted on 2011-09-10
3
353 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 74

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now