Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple function to encrypt and decrypt a script
    primarykey
    data
    text
    <p>I have two functions that are supposed to encrypt and decrypt a string but the encrypt function returns a different encrypted string for the same text everytime. What have I done wrong?</p> <pre><code>Public Shared Function Encrypt(ByVal strText As String, ByVal strKey As String) As String Dim IV() As Byte = {&amp;H12, &amp;H34, &amp;H56, &amp;H78, &amp;H90, &amp;HAB, &amp;HCD, &amp;HEF} Try Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strKey, 8)) Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText) Dim des As New DESCryptoServiceProvider Dim ms As New IO.MemoryStream 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 Finally End Try End Function Public Shared Function Decrypt(ByVal strText As String, ByVal strKey As String) As String Dim IV() As Byte = {&amp;H12, &amp;H34, &amp;H56, &amp;H78, &amp;H90, &amp;HAB, &amp;HCD, &amp;HEF} Dim inputByteArray(strText.Length) As Byte Try Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strKey, 8)) Dim des As New DESCryptoServiceProvider inputByteArray = Convert.FromBase64String(strText) Dim ms As New IO.MemoryStream Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write) cs.Write(inputByteArray, 0, inputByteArray.Length) cs.FlushFinalBlock() Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8 Return encoding.GetString(ms.ToArray()) Catch ex As Exception Return ex.Message End Try End Function </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload