T
Tom Andrecht
I'm trying to get some encryption/decryption routines going to take care of
my data, and while the encryption is working great, I keep running into the
message "Padding is invalid and cannot be removed" on the decryption piece.
From everything I can see, I am doing things correctly here My code is as
follows:
private const string PassStr = "MyPrivateKey";
private static readonly byte[] PassSalt = new byte[] { Byte Byte
Byte Byte Byte};
private static readonly Enabled en;
static Encryption()
{
en = new Enabled();
en.enabled = true;
}
private static byte[] TransDown(char[] TransString)
{
byte[] ResultStr = new byte[TransString.Length];
for (int i = 0; i < TransString.Length; i++)
ResultStr = Convert.ToByte(TransString);
return ResultStr;
}
private static char[] TransUp(Byte[] TransString)
{
char[] ResultStr = new char[TransString.Length];
for (int i = 0; i < TransString.Length; i++)
ResultStr = Convert.ToChar(TransString);
return ResultStr;
}
private static char[] TransDown(String TransString)
{
char[] ResultStr = new char[TransString.Length];
for (int i = 0; i < TransString.Length; i++)
ResultStr = Convert.ToChar(TransString);
return ResultStr;
}
public static char[] Encrypt(char[] InputString)
{
if (en.enabled)
{
if ((InputString != null) && (InputString.Length > 0))
{
char[] ResultStr = new char[32];
MemoryStream memoryStream;
CryptoStream cryptoStream;
RijndaelManaged rijndael = new RijndaelManaged();
ResultStr = TransDown(PassStr);
rijndael.Key = TransDown(ResultStr);
rijndael.IV = PassSalt;
rijndael.Padding = PaddingMode.PKCS7;
rijndael.Mode = CipherMode.CBC;
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream,
rijndael.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(TransDown(InputString), 0,
InputString.Length);
cryptoStream.FlushFinalBlock();
//ResultStr = TransUp(memoryStream.ToArray());
cryptoStream.Close();
return TransUp(memoryStream.ToArray());
}
else
return TransDown("NULL");
}
else
return TransDown("Not Authorized");
}
public static char[] Decrypt(char[] InputString)
{
if (en.enabled)
{
if ((InputString != null) && (InputString.Length > 0))
{
char[] ResultStr = new char[32];
byte[] ResultByte = new byte[InputString.Length];
MemoryStream memoryStream = new
MemoryStream(TransDown(InputString));
memoryStream.Position = 0;
CryptoStream cryptoStream;
RijndaelManaged rijndael = new RijndaelManaged();
ResultStr = TransDown(PassStr);
rijndael.Key = TransDown(ResultStr);
rijndael.IV = PassSalt;
rijndael.Padding = PaddingMode.PKCS7;
rijndael.Mode = CipherMode.CBC;
cryptoStream = new CryptoStream(memoryStream,
rijndael.CreateDecryptor(), CryptoStreamMode.Read);
cryptoStream.Read(ResultByte, 0, InputString.Length);
cryptoStream.Close();
return TransUp(ResultByte);
}
else
return TransDown(" ") ;
}
else
return TransDown("Not Authorized");
}
The message I am getting is:
Msg 6522, Level 16, State 2, Line 1
A .NET Framework error occurred during execution of user defined routine or
aggregate 'Decrypt':
System.Security.Cryptography.CryptographicException: Padding is invalid and
cannot be removed.
System.Security.Cryptography.CryptographicException:
at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer,
Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
at
System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32
offset, Int32 count)
at SQLEncryption.Encryption.Decrypt(Char[] InputString)
Can anyone tell me where I am going wrong? I don't know the cryptography
components well enough to have a clue about what I am doing wrong. Thanks.
my data, and while the encryption is working great, I keep running into the
message "Padding is invalid and cannot be removed" on the decryption piece.
From everything I can see, I am doing things correctly here My code is as
follows:
private const string PassStr = "MyPrivateKey";
private static readonly byte[] PassSalt = new byte[] { Byte Byte
Byte Byte Byte};
private static readonly Enabled en;
static Encryption()
{
en = new Enabled();
en.enabled = true;
}
private static byte[] TransDown(char[] TransString)
{
byte[] ResultStr = new byte[TransString.Length];
for (int i = 0; i < TransString.Length; i++)
ResultStr = Convert.ToByte(TransString);
return ResultStr;
}
private static char[] TransUp(Byte[] TransString)
{
char[] ResultStr = new char[TransString.Length];
for (int i = 0; i < TransString.Length; i++)
ResultStr = Convert.ToChar(TransString);
return ResultStr;
}
private static char[] TransDown(String TransString)
{
char[] ResultStr = new char[TransString.Length];
for (int i = 0; i < TransString.Length; i++)
ResultStr = Convert.ToChar(TransString);
return ResultStr;
}
public static char[] Encrypt(char[] InputString)
{
if (en.enabled)
{
if ((InputString != null) && (InputString.Length > 0))
{
char[] ResultStr = new char[32];
MemoryStream memoryStream;
CryptoStream cryptoStream;
RijndaelManaged rijndael = new RijndaelManaged();
ResultStr = TransDown(PassStr);
rijndael.Key = TransDown(ResultStr);
rijndael.IV = PassSalt;
rijndael.Padding = PaddingMode.PKCS7;
rijndael.Mode = CipherMode.CBC;
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream,
rijndael.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(TransDown(InputString), 0,
InputString.Length);
cryptoStream.FlushFinalBlock();
//ResultStr = TransUp(memoryStream.ToArray());
cryptoStream.Close();
return TransUp(memoryStream.ToArray());
}
else
return TransDown("NULL");
}
else
return TransDown("Not Authorized");
}
public static char[] Decrypt(char[] InputString)
{
if (en.enabled)
{
if ((InputString != null) && (InputString.Length > 0))
{
char[] ResultStr = new char[32];
byte[] ResultByte = new byte[InputString.Length];
MemoryStream memoryStream = new
MemoryStream(TransDown(InputString));
memoryStream.Position = 0;
CryptoStream cryptoStream;
RijndaelManaged rijndael = new RijndaelManaged();
ResultStr = TransDown(PassStr);
rijndael.Key = TransDown(ResultStr);
rijndael.IV = PassSalt;
rijndael.Padding = PaddingMode.PKCS7;
rijndael.Mode = CipherMode.CBC;
cryptoStream = new CryptoStream(memoryStream,
rijndael.CreateDecryptor(), CryptoStreamMode.Read);
cryptoStream.Read(ResultByte, 0, InputString.Length);
cryptoStream.Close();
return TransUp(ResultByte);
}
else
return TransDown(" ") ;
}
else
return TransDown("Not Authorized");
}
The message I am getting is:
Msg 6522, Level 16, State 2, Line 1
A .NET Framework error occurred during execution of user defined routine or
aggregate 'Decrypt':
System.Security.Cryptography.CryptographicException: Padding is invalid and
cannot be removed.
System.Security.Cryptography.CryptographicException:
at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer,
Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
at
System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[]
inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32
offset, Int32 count)
at SQLEncryption.Encryption.Decrypt(Char[] InputString)
Can anyone tell me where I am going wrong? I don't know the cryptography
components well enough to have a clue about what I am doing wrong. Thanks.