Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7696849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:52:31+00:00 2026-05-31T21:52:31+00:00

I have two functions that are supposed to encrypt and decrypt a string but

  • 0

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?

Public Shared Function Encrypt(ByVal strText As String, ByVal strKey As String) As String
    Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &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 = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:52:33+00:00Added an answer on May 31, 2026 at 9:52 pm

    Perhaps the Decrypt need to read the data crypted, not write over.

    Public Shared Function Decrypt(ByVal strText As String, ByVal strKey As String) As String 
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF} 
        Dim inputByteArray(strText.Length) As Byte 
        string result
        Try 
            Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strKey, 8)) 
            Using des As New DESCryptoServiceProvider 
                inputByteArray = Convert.FromBase64String(strText) 
                Using ms As New IO.MemoryStream(inputByteArray)
                   Using cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Read) 
                       Using srDecrypt As = New StreamReader(cs)
                           result = srDecrypt.ReadToEnd()
                       End Using
                   End Using
                End Using
            End Using
            Return result
        Catch ex As Exception 
            Return ex.Message 
        End Try 
    End Function 
    

    Edit: Now it works. Added Using to the Disposable classes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions that have different enough logic but pretty much the same
So I have two jquery functions that bassically do the same, but on 2
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
I have need for a function pointer that takes two arguments and returns a
I have two functions whose underlying logic is the same but in one case
Suppose you have a Django view that has two functions: The first function renders
I have created two functions that sum and subtract the numeric values of two
I have two fields that I'd like to match. (already done the validation functions
I have an IF statement that consists of two separate function calls passing values
I have two functions in an ActionScript class, they are: private function loaderCompleteHandler(event:Event):void {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.