Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Encryption and Decryption Text

Encryption and Decryption Text

Scheduled Pinned Locked Moved Visual Basic
securityhelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pdnet
    wrote on last edited by
    #1

    How Can I Encrypt and Decrypt a String remember its encrypt and Decrypt a null string also Please,Help me

    Arindam Banerjee Sr. Software Developer Rance Computer Pvt Ltd. Kolkata (India)

    J 1 Reply Last reply
    0
    • P pdnet

      How Can I Encrypt and Decrypt a String remember its encrypt and Decrypt a null string also Please,Help me

      Arindam Banerjee Sr. Software Developer Rance Computer Pvt Ltd. Kolkata (India)

      J Offline
      J Offline
      JUNEYT
      wrote on last edited by
      #2

      Please check the following sample, I think you can also include null strings checking. I hope it works out for you. :)

      Imports System.Text
      Imports System.Security.Cryptography

      Public Class Encryption
      #Region "Public Routines"
      const ENCRYPT_KEY AS STRING = "TEST"
      const ENCRYPT_VECTOR AS STRING = "CODEPRJT"

      Public Shared Function ToBase64(ByVal sDataToEncrypt As String) As String
          Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream
          Dim key As Byte() = Encoding.Default.GetBytes(ENCRYPT\_KEY.PadRight(24, Chr(0)))
          Dim Vector As Byte() = Encoding.Default.GetBytes(ENCRYPT\_VECTOR.PadRight(8, Chr(0)))
      
          Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
          Dim cryptoStream As CryptoStream = New CryptoStream(stream, des.CreateEncryptor(key, Vector), CryptoStreamMode.Write)
      
          Dim Input() As Byte = Encoding.Default.GetBytes(sDataToEncrypt)
      
          cryptoStream.Write(Input, 0, Input.Length)
          cryptoStream.FlushFinalBlock()
      
          Return Convert.ToBase64String(stream.ToArray())
      End Function
      
      Public Shared Function ToBase32(ByVal sDataToDecrypt As String) As String
          Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream
          Dim key As Byte() = Encoding.Default.GetBytes(ENCRYPT\_KEY.PadRight(24, Chr(0)))
          Dim Vector As Byte() = Encoding.Default.GetBytes(ENCRYPT\_VECTOR.PadRight(8, Chr(0)))
      
          Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
          Dim cryptoStream As CryptoStream = New CryptoStream(stream, des.CreateDecryptor(key, Vector), CryptoStreamMode.Write)
      
          Dim Input() As Byte = Convert.FromBase64String(sDataToDecrypt)
      
          cryptoStream.Write(Input, 0, Input.Length)
          cryptoStream.FlushFinalBlock()
      
          Return Encoding.Default.GetString(stream.ToArray())
      End Function
      

      #End Region
      End Class

      What a curious mind needs to discover knowledge is noting else than a pin-hole.

      P 1 Reply Last reply
      0
      • J JUNEYT

        Please check the following sample, I think you can also include null strings checking. I hope it works out for you. :)

        Imports System.Text
        Imports System.Security.Cryptography

        Public Class Encryption
        #Region "Public Routines"
        const ENCRYPT_KEY AS STRING = "TEST"
        const ENCRYPT_VECTOR AS STRING = "CODEPRJT"

        Public Shared Function ToBase64(ByVal sDataToEncrypt As String) As String
            Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream
            Dim key As Byte() = Encoding.Default.GetBytes(ENCRYPT\_KEY.PadRight(24, Chr(0)))
            Dim Vector As Byte() = Encoding.Default.GetBytes(ENCRYPT\_VECTOR.PadRight(8, Chr(0)))
        
            Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
            Dim cryptoStream As CryptoStream = New CryptoStream(stream, des.CreateEncryptor(key, Vector), CryptoStreamMode.Write)
        
            Dim Input() As Byte = Encoding.Default.GetBytes(sDataToEncrypt)
        
            cryptoStream.Write(Input, 0, Input.Length)
            cryptoStream.FlushFinalBlock()
        
            Return Convert.ToBase64String(stream.ToArray())
        End Function
        
        Public Shared Function ToBase32(ByVal sDataToDecrypt As String) As String
            Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream
            Dim key As Byte() = Encoding.Default.GetBytes(ENCRYPT\_KEY.PadRight(24, Chr(0)))
            Dim Vector As Byte() = Encoding.Default.GetBytes(ENCRYPT\_VECTOR.PadRight(8, Chr(0)))
        
            Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
            Dim cryptoStream As CryptoStream = New CryptoStream(stream, des.CreateDecryptor(key, Vector), CryptoStreamMode.Write)
        
            Dim Input() As Byte = Convert.FromBase64String(sDataToDecrypt)
        
            cryptoStream.Write(Input, 0, Input.Length)
            cryptoStream.FlushFinalBlock()
        
            Return Encoding.Default.GetString(stream.ToArray())
        End Function
        

        #End Region
        End Class

        What a curious mind needs to discover knowledge is noting else than a pin-hole.

        P Offline
        P Offline
        pdnet
        wrote on last edited by
        #3

        its throws an error message : Specified key is a known weak key for 'TripleDES' and cannot be used

        Arindam Banerjee Sr. Software Developer Rance Computer Pvt Ltd. Kolkata (India)

        J 1 Reply Last reply
        0
        • P pdnet

          its throws an error message : Specified key is a known weak key for 'TripleDES' and cannot be used

          Arindam Banerjee Sr. Software Developer Rance Computer Pvt Ltd. Kolkata (India)

          J Offline
          J Offline
          JUNEYT
          wrote on last edited by
          #4

          I had the same problem but when I change the keys it worked out. Actually teh code doesn't belong to me. Try to use different string keys.

          What a curious mind needs to discover knowledge is noting else than a pin-hole.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups