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. Padding is invalid and cannot be removed

Padding is invalid and cannot be removed

Scheduled Pinned Locked Moved Visual Basic
csharpsecurityhelp
2 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.
  • R Offline
    R Offline
    Raheem MA
    wrote on last edited by
    #1

    Hi friends, We are getting the following error while trying to decrypt the string which is encrypted using the same class. "Padding is invalid and cannot be removed" following is the class: (VB.NET) Imports System Imports System.IO Imports System.Security.Cryptography Public Class EncryptDecrypt Dim key() As Byte Dim iv() As Byte Dim firstRJ As RijndaelManaged Sub EncryptDecrypt() Me.InitializeKeyIV() End Sub Sub InitializeKeyIV() key = New Byte() {229, 249, 126, 70, 196, 148, 231, 10, 130, 22, 65, 172, 216, 13, 68, 234, 46, 146, 31, 102, 228, 181, 212, 145} iv = New Byte() {8, 14, 130, 251, 155, 125, 219, 144, 103, 182, 95, 103, 58, 6, 205, 161} End Sub Public Function Encrypt(ByVal clearText) As String Dim retChiperText As String = "" If clearText <> "" Then Dim ms As MemoryStream = Nothing Dim cs As CryptoStream = Nothing Dim sw As StreamWriter = Nothing Try ms = New MemoryStream() firstRJ = New RijndaelManaged() Dim encryptor As ICryptoTransform = firstRJ.CreateEncryptor(key, iv) cs = New CryptoStream(ms, encryptor, CryptoStreamMode.Write) sw = New StreamWriter(cs) sw.Write(clearText) sw.Close() Dim chiperTextBytes As Byte() chiperTextBytes = ms.ToArray() retChiperText = Convert.ToBase64String(chiperTextBytes) Catch Ex As Exception Finally cs.Close() ms.Close() firstRJ.Clear() End Try End If Encrypt = retChiperText End Function Public Function Decrypt(ByVal chiperText) As String Dim retClearText As String = "" chiperText = Trim(chiperText) MessageBox.Show("Text: " + chiperText) If chiperText <> "" Then Dim cs2 As CryptoStream = Nothing Dim ms2 As MemoryStream = Nothing Dim sr As StreamReader = Nothing Try Dim chText As Byte() chText = Convert.FromBase64String(chiperText) firstRJ = New RijndaelManaged() ms2 = New MemoryStream(chText) Dim decryptor As ICryptoTransform = firstRJ.CreateDecryptor(key, iv) cs2 = New CryptoStream(ms2, decryptor, CryptoStreamMode.Read)

    D 1 Reply Last reply
    0
    • R Raheem MA

      Hi friends, We are getting the following error while trying to decrypt the string which is encrypted using the same class. "Padding is invalid and cannot be removed" following is the class: (VB.NET) Imports System Imports System.IO Imports System.Security.Cryptography Public Class EncryptDecrypt Dim key() As Byte Dim iv() As Byte Dim firstRJ As RijndaelManaged Sub EncryptDecrypt() Me.InitializeKeyIV() End Sub Sub InitializeKeyIV() key = New Byte() {229, 249, 126, 70, 196, 148, 231, 10, 130, 22, 65, 172, 216, 13, 68, 234, 46, 146, 31, 102, 228, 181, 212, 145} iv = New Byte() {8, 14, 130, 251, 155, 125, 219, 144, 103, 182, 95, 103, 58, 6, 205, 161} End Sub Public Function Encrypt(ByVal clearText) As String Dim retChiperText As String = "" If clearText <> "" Then Dim ms As MemoryStream = Nothing Dim cs As CryptoStream = Nothing Dim sw As StreamWriter = Nothing Try ms = New MemoryStream() firstRJ = New RijndaelManaged() Dim encryptor As ICryptoTransform = firstRJ.CreateEncryptor(key, iv) cs = New CryptoStream(ms, encryptor, CryptoStreamMode.Write) sw = New StreamWriter(cs) sw.Write(clearText) sw.Close() Dim chiperTextBytes As Byte() chiperTextBytes = ms.ToArray() retChiperText = Convert.ToBase64String(chiperTextBytes) Catch Ex As Exception Finally cs.Close() ms.Close() firstRJ.Clear() End Try End If Encrypt = retChiperText End Function Public Function Decrypt(ByVal chiperText) As String Dim retClearText As String = "" chiperText = Trim(chiperText) MessageBox.Show("Text: " + chiperText) If chiperText <> "" Then Dim cs2 As CryptoStream = Nothing Dim ms2 As MemoryStream = Nothing Dim sr As StreamReader = Nothing Try Dim chText As Byte() chText = Convert.FromBase64String(chiperText) firstRJ = New RijndaelManaged() ms2 = New MemoryStream(chText) Dim decryptor As ICryptoTransform = firstRJ.CreateDecryptor(key, iv) cs2 = New CryptoStream(ms2, decryptor, CryptoStreamMode.Read)

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      It would appear that whatever you're doing to the crypted stream afterwards is altering the data in the stream, or adding something to it.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      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