vb.net DES Encrypt to Java decrypt... init vector

Last post 07-05-2008 7:34 AM by mohsinvbfan. 3 replies.

Sort Posts:

  • vb.net DES Encrypt to Java decrypt... init vector

    05-14-2007, 2:48 PM
    • Member
      150 point Member
    • GreaseEater
    • Member since 12-27-2002, 9:20 AM
    • Posts 35

    I'm working on a .net app that is going to send a request to an existing Java servlet that is expecting a string in a DES encrypted form.  I've been comparing code samples from various sources that do DES encryption in vb.net against the ones written in Java.  The code samples written in Java do not seem to have a variable called "IV" (initialization vector) or "salt".  Is there a way to get vb.net to work like the Java samples?  I've tried using an empty byte array or setting it to nothing, but I'm not getting the response I expect.

     Thanks

    Filed under:
  • Re: vb.net DES Encrypt to Java decrypt... init vector

    05-16-2007, 6:07 AM
    Answer

    Hi

    Initialization vectors (IVs) is a continuously changing number used in combination with a secret key to encrypt data, which prevent dictionary attacks. For example, without an initialization vector, "password" will always encrypt to the same thing, and which makes a dictionary attack easier.

    The initialization vector is not used with ECB mode but is important for other usage modes such as CBC. So I think you'd hava a look at your java code and give more information about this.

    For further detailed information please refer to:

    http://tcllib.sourceforge.net/doc/des.html 

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

     

    Best Regards
    XiaoYong Dai
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: vb.net DES Encrypt to Java decrypt... init vector

    05-16-2007, 10:00 AM
    Answer
    • Member
      150 point Member
    • GreaseEater
    • Member since 12-27-2002, 9:20 AM
    • Posts 35

    Thanks for your response.  ECB was exactly what we needed.  It seems that a lot of the samples going around the internet are wrong also, the byte array length is incorrect on them.  Here is the final code that works: 

      

    1    	Public Function EncryptToBase64String(ByVal stringToEncrypt As String, ByVal SEncryptionKey As String) As String
    2    		Try
    3    			'convert our input string to a byte array
    4    			Dim inputByteArray() As Byte = StrToByteArray(stringToEncrypt)
    5    			'convert the key to a byte array
    6    			key = StrToByteArray(SEncryptionKey)
    7    			'now encrypt the bytearray 
    8    			Dim des As New DESCryptoServiceProvider
    9    			des.Mode = CipherMode.ECB
    10   			des.Key = key
    11   			Dim ms As New MemoryStream
    12   			Dim cs As New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)
    13   			cs.Write(inputByteArray, 0, inputByteArray.Length - 1)
    14   			cs.FlushFinalBlock()
    15   			' now return the byte array as a "safe for XMLDOM" Base64 String
    16   			Return Convert.ToBase64String(ms.ToArray())
    17   		Catch e As Exception
    18   			Return e.Message
    19   		End Try
    20   	End Function
    
     
    Filed under:
  • Re: vb.net DES Encrypt to Java decrypt... init vector

    07-05-2008, 7:34 AM
    • Member
      2 point Member
    • mohsinvbfan
    • Member since 07-05-2008, 7:17 AM
    • Posts 1

    although ur post is so old. em also suffring from the same problem.have to add a java applete and have to pass DES ENCRYPTION values.i saw ur post and tried.in ur this function KEY varibale was not defined.i also have to have 'StrToByteArray' function. as i open the page and called this function on button click to verify results. i provided it the 32 character key (which was being supplied to encrypt string in java) and a simple string link to convert.  it throughs exeption specified key is not a valid size for this algorithym... 

    wot is the iv how can i create or get iv for my 32 character key???

    i urgently req help on this... em pasting my code below

     

    Imports System.Security

    Imports System.Security.Cryptography

    Imports System.Security.Cryptography.TripleDESCryptoServiceProvider

    Imports System.IO

    Partial Class Default5

    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    MsgBox (EncryptToBase64String("filter", "S1o2n3i4q5u6e7L8i9m9i8t7e6d5U4K3"))

    End Sub

     

    Public Function EncryptToBase64String(ByVal stringToEncrypt As String, ByVal SEncryptionKey As String) As String

    Try

    'convert our input string to a byte array

    Dim inputByteArray() As Byte = StrToByteArray(stringToEncrypt)

    'convert the key to a byte array

    Dim Key = StrToByteArray(SEncryptionKey)

    'now encrypt the bytearray

    Dim des As New DESCryptoServiceProvider

    des.Mode = CipherMode.ECB

    des.Key = Key

    Dim ms As New MemoryStreamDim cs As New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)

    cs.Write(inputByteArray, 0, inputByteArray.Length - 1)

    cs.FlushFinalBlock()

    ' now return the byte array as a "safe for XMLDOM" Base64 String

    Return Convert.ToBase64String(ms.ToArray())

    Catch e As Exception

    Return e.Message

    End Try

    End Function

    Public Shared Function StrToByteArray(ByVal str As String) As Byte()

    Dim encoding As New System.Text.UTF8Encoding

    Return encoding.GetBytes(str)

    End Function 'StrToByteArray

    End Class

Page 1 of 1 (4 items)