I've problem with Rijndael PKCS7

Y

Yuna84

I've tried with a simple program in vb.net but when i'm going to
decript in my messagebox appears this message: PKCS7 padding is
invalid and cannot be removed.

I've found this few lines of codes in this site:
http://www.dotnethell.it/articles/article.aspx?ArticleID=93

I've trasform in vb.net and this is the result

Imports system.security.cryptography
imports system.text
('I don't write the other normal imports)



Public Function Encode(ByVal s As String, ByVal chiave As String,
ByVal IV As String) As String
Dim ascii As New ASCIIEncoding
Dim rjm As New RijndaelManaged
rjm.KeySize = 128
rjm.BlockSize = 128
rjm.Key = ascii.GetBytes(chiave)
rjm.Key = ascii.GetBytes(IV)
Dim input() As Byte = Encoding.UTF7.GetBytes(s)
Dim output() As Byte =
rjm.CreateEncryptor().TransformFinalBlock(input, 0, input.Length)

Return Convert.ToBase64String(output)
End Function

Public Function Decode(ByVal s As String, ByVal chiave As String,
ByVal IV As String) As String
Dim rjm As New RijndaelManaged
Dim ascii As New ASCIIEncoding

rjm.KeySize = 128
rjm.BlockSize = 128
rjm.Key = ascii.GetBytes(chiave)
rjm.Key = ascii.GetBytes(IV)
Try
Dim input() As Byte = Convert.FromBase64String(s)
Dim output() As Byte =
rjm.CreateDecryptor().TransformFinalBlock(input, 0, input.Length)
Return Encoding.UTF7.GetString(output)
Catch e As Exception
MessageBox.Show(e.Message & " " & e.Source)
End Try
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
txtDecripta.Text = (Encode(txtCripta.Text, "AsDfGhJkLqPwOeIr",
"RiEoWpQlKjHgFdSa"))
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
Console.WriteLine(Decode(txtDecripta.Text, "AsDfGhJkLqPwOeIr",
"RiEoWpQlKjHgFdSa"))
End Sub

Please. This is an emergency!!!! Can you help me???
 
H

Hernan de Lahitte

I suggest you to take a look at the sample in
http://sourceforge.net/projects/ncrypto/
You will find a good sample in CryptoHelper.Encrypt or Decrypt methods.

In your sample, I notice the different encoding methods when encrypting and
decrypting and the algorithm usage is not very "clean" (Close() the alg
instance when finishing) nor readable.


Hernan de Lahitte
Lagash Systems S.A.
http://weblogs.asp.net/hernandl


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top