473,378 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Encryption padding error

Bob Ross
119 100+
I am trying to ecrypt passwords to store them in a DB but I want to store them in String(nvarchar) format. So I have written this class

Expand|Select|Wrap|Line Numbers
  1.     Public Class PasswordEncryption
  2.         Private IV As Byte() = Convert.FromBase64String("pl21q5lX3KvPdKEyss7Za0i2nEdfzfbHvbsxlsKCWbU=")
  3.         Private pKey As Byte() = Convert.FromBase64String("1taYM5kPvxIdvNwBcuQefdiKMlzgqpdgiP/BjG4anVw=")
  4.         Dim Cipher As RijndaelManaged
  5.         Public Sub New()
  6.             Cipher = New RijndaelManaged
  7.             Cipher.KeySize = 256
  8.             Cipher.BlockSize = 256
  9.             Cipher.Mode = CipherMode.CBC
  10.             Cipher.Key = pKey
  11.             Cipher.IV = IV
  12.         End Sub
  13.         Public Function EncryptMessage(ByVal plainText As String) As String
  14.             Dim BR As Byte() = Encoding.Unicode.GetBytes(plainText)
  15.             Dim transform As ICryptoTransform = Cipher.CreateEncryptor()
  16.             Dim cipherText As Byte() = transform.TransformFinalBlock(BR, 0, BR.Length)
  17.             Return Convert.ToBase64String(Cipher.Key)
  18.         End Function
  19.         Public Function DecryptMessage(ByVal cipherText As String) As String
  20.             Dim BR As Byte() = Convert.FromBase64String(cipherText)
  21.             Dim transform As ICryptoTransform = Cipher.CreateDecryptor()
  22.             Dim plainText As Byte() = transform.TransformFinalBlock(BR, 0, BR.Length)
  23.             Return Convert.ToBase64String(plainText)
  24.         End Function
  25.     End Class
But every time I try


Expand|Select|Wrap|Line Numbers
  1.             Dim Enc As New PasswordEncryption
  2.             Dim PassEnc As String = Enc.EncryptMessage("hello")
  3.             Dim PassUnEnc As String = Enc.DecryptMessage(PassEnc)
I get "Padding is invalid and cannot be converted" Anyone know how I can fix this?
Tried different padding modes but same error everytime.
Aug 22 '07 #1
3 1234
Plater
7,872 Expert 4TB
I would try to debug until the exact spot where it fails to have the padding, then examine the strings and see where it is getting lost?
What you have seems sound enough, assuming what you're using is a 2-way ciper
Aug 22 '07 #2
Bob Ross
119 100+
I can't examine the strings because I am unable to read encrypted text.

It is getting stuck when it trys to decrypt the string this line
Expand|Select|Wrap|Line Numbers
  1. Dim plainText As Byte() = transform.TransformFinalBlock(BR, 0, BR.Length)
  2.  
Aug 23 '07 #3
Plater
7,872 Expert 4TB
EDIT:
Actually, your encrypt function is only returning the key
Expand|Select|Wrap|Line Numbers
  1. Return Convert.ToBase64String(Cipher.Key)
  2.  
Which you can't decrypt.
You should be returning, I *believe* they key AND the encrypted message or at lease the encrypted message.
Aug 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: no game | last post by:
Can I encrypt data more than 117 bytes in C# (can use CAPICOM and Crypto API libraries) using RSA 1024 bit. Any sample code would be appreciated. Thanks
2
by: Carolyn Vo | last post by:
We have code in Java that encrypts a string using DES. However, when we encrypt in C# the string is encrypted differently. The code to encrypt in Java is: DESKeySpec desKeySpec = new...
36
by: phil-news-nospam | last post by:
Here is a simpler (no drop shadows) example of the padding bug I see: http://phil.ipal.org/usenet/ciwas/2006-05-08/buttons-1.html So far I find nothing in the CSS2 document that says I should...
4
by: Andreas Pauley | last post by:
Hi all, I'm trying to implement a Python equivalent of a C# method that encrypts a string. My Python attempt is in the attached file, but does not return the same value as the C# method (see...
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
0
by: Vayse | last post by:
I need to encrypt some strings in serveral programs I have. So I grabbed some code from the MS help. I wrote an app that would help me generate the encrypted strings. Its s form with 4 text boxes...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
5
by: FeurFly | last post by:
I am trying to find a way to use the RSAPKCS #1 encryption scheme in c# using only the public half of the key. Mainly, I would use an RSAParameters class to store the key info (exponent, modulus,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.