Solved

encryption

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

25 Experts available now in Live!

Get 1:1 Help Now