encryption help

S

Simon Whale

Hi All,

i am using the following two functions to encrypt data, bank details. i can
encrypt the details no problems. but when i try to decrypt the details i
get the following error

"PKCS7 padding is invalid and cannot be removed"

does anybody know i can encrypt and decrypt data from a database ? any
pointers would be greatfully appreciated!!



Many thanks

Simon Whale

P.S using vb.net 2003 and sql server 200





Public Function DecryptString(ByVal Value As String) As String

Dim ct As ICryptoTransform

Dim ms As MemoryStream

Dim cs As CryptoStream

Dim byt() As Byte

ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)

byt = Convert.FromBase64String(Value)

ms = New MemoryStream

cs = New CryptoStream(ms, ct, CryptoStreamMode.Write)

cs.Write(byt, 0, byt.Length)

cs.FlushFinalBlock()

cs.Close()

Return Encoding.UTF8.GetString(ms.ToArray())

End Function

Public Function EncryptString(ByVal Value As String) As String

Dim ct As ICryptoTransform

Dim ms As MemoryStream

Dim cs As CryptoStream

Dim byt() As Byte

ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV)

byt = Encoding.UTF8.GetBytes(Value)

ms = New MemoryStream

cs = New CryptoStream(ms, ct, CryptoStreamMode.Write)

cs.Write(byt, 0, byt.Length)

cs.FlushFinalBlock()

cs.Close()

Return Convert.ToBase64String(ms.ToArray())

End Function
 
G

Guest

Simon -

Well... I do not know for sure, since the provided code is incomplete, but i
believe that the problem has to do with the IV. The IV should be different
for every cipher text. This means that the IV needs to be known to the
function performing the decryption. It looks like your code:

ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV)

generates a new IV during the decryption process [maybe? I dont really know
since mCSP is not defined in the code provided]

In applications I have written, the IV is included in the cipher text as
plaintext.
 

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

Top