|
-
Jul 8th, 2004, 05:55 PM
#1
Thread Starter
Frenzied Member
Encryption [Resolved]
The EnCrypt Method works. The Decrypt method thows an error of
Length of the data to decrypt is invalid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Security.Cryptography.CryptographicException: Length of the data to decrypt is invalid.
Guys I really need to get this working. Please Help.
Code:
/// <summary>
/// Decrypt a string encrypted by the EnCrypt method.
/// </summary>
/// <param name="value">String to DeCrypt.</param>
/// <returns>The Decrypted string.</returns>
public static string DeCrypt(string value)
{
string ret = "";
// char c = new char();
// for(int i = 0; i < value.Length; i++)
// {
// c = System.Convert.ToChar(value.Substring(i, 1));
// ret += System.Convert.ToChar(System.Convert.ToInt32(c) - 5);
// }
byte[] key = new byte[]{
0x01,
0x02,
0x03,
0x03,
0x04,
0x05,
0x05,
0x06,
0x07,
0x08,
0x09,
0x10,
0x11,
0x12,
0x13,
0x14,
0x15,
0x16};
byte[] iv = new byte[]{
0x01,
0x02,
0x03,
0x03,
0x04,
0x05,
0x05,
0x06,
0x07,
0x08,
0x09,
0x10,
0x11,
0x12,
0x13,
0x14,
0x15,
0x16};
byte[] b = ToByteArray(value);
System.IO.MemoryStream s = new System.IO.MemoryStream(b);
System.Security.Cryptography.RijndaelManaged rj = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.ICryptoTransform trans = rj.CreateDecryptor();//key, iv);
System.Security.Cryptography.CryptoStreamMode mode = System.Security.Cryptography.CryptoStreamMode.Read;
System.Security.Cryptography.CryptoStream enc = new System.Security.Cryptography.CryptoStream(s, trans, mode);
System.IO.StreamReader r = new System.IO.StreamReader(enc);
ret = r.ReadToEnd();
return ret;
}
/// <summary>
/// Encrypt a string.
/// </summary>
/// <param name="value">The String to encrypt.</param>
/// <returns>The encrypted string.</returns>
public static string EnCrypt(string value)
{
string ret = "";
// char c = new char();
// for(int i = 0; i < value.Length; i++)
// {
// c = System.Convert.ToChar(value.Substring(i, 1));
// ret += System.Convert.ToChar(System.Convert.ToInt32(c) + 5);
// }
byte[] key = new byte[]{
0x01,
0x02,
0x03,
0x03,
0x04,
0x05,
0x05,
0x06,
0x07,
0x08,
0x09,
0x10,
0x11,
0x12,
0x13,
0x14,
0x15,
0x16};
byte[] iv = new byte[]{
0x01,
0x02,
0x03,
0x03,
0x04,
0x05,
0x05,
0x06,
0x07,
0x08,
0x09,
0x10,
0x11,
0x12,
0x13,
0x14,
0x15,
0x16};
byte[] b = ToByteArray(value);
System.IO.MemoryStream s = new System.IO.MemoryStream(b);
System.Security.Cryptography.RijndaelManaged rj = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.ICryptoTransform trans = rj.CreateEncryptor();//key, iv);
System.Security.Cryptography.CryptoStreamMode mode = System.Security.Cryptography.CryptoStreamMode.Read;
System.Security.Cryptography.CryptoStream enc = new System.Security.Cryptography.CryptoStream(s, trans, mode);
System.IO.StreamReader r = new System.IO.StreamReader(enc);
ret = r.ReadToEnd();
return ret;
}
Last edited by Magiaus; Jul 24th, 2004 at 02:05 PM.
Magiaus
If I helped give me some points.
-
Jul 9th, 2004, 12:16 PM
#2
Thread Starter
Frenzied Member
new code same error
PHP Code:
namespace Root.Data.Helpers
{
/// <summary>
/// Provides a common location for static methods that don't fit well anywhere else.
/// </summary>
public class Common
{
private static string cryptKey = "%ALL_YOUR_BASE_ARE_BELONG_TO_US%";
private static byte[] cryptIV = new byte[]{
0x31,
0x13,
0x44,
0x08,
0x22,
0x77,
0xFF,
0x0A,
0xAC,
0xDC,
0xFC,
0x11,
0x19,
0xCC,
0x01,
0x32};
/// <summary>
/// Decrypt a string encrypted by the EnCrypt method.
/// </summary>
/// <param name="value">String to DeCrypt.</param>
/// <returns>The Decrypted string.</returns>
public static string DeCrypt(string value)
{
value = value.Trim();
string ret = "";
byte[] key = ToByteArray(cryptKey, EncodeMethod.ASCII);
byte[] b = ToByteArray(value, EncodeMethod.ASCII);
byte[] bt = new byte[b.Length];
System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(
ms,
rm.CreateDecryptor(key, cryptIV),
System.Security.Cryptography.CryptoStreamMode.Read);
cs.Read(bt, 0, bt.Length);
cs.FlushFinalBlock();
ms.Close();
cs.Close();
ret = FromByteArray(bt, EncodeMethod.ASCII);
return ret;
}
/// <summary>
/// Encrypt a string.
/// </summary>
/// <param name="value">The String to encrypt.</param>
/// <returns>The encrypted string.</returns>
public static string EnCrypt(string value)
{
value = value.Trim();
string ret = "";
byte[] key = ToByteArray(cryptKey, EncodeMethod.ASCII);
byte[] b = ToByteArray(value, EncodeMethod.ASCII);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(
ms,
rm.CreateEncryptor(key, cryptIV),
System.Security.Cryptography.CryptoStreamMode.Write);
cs.Write(b, 0, b.Length);
cs.FlushFinalBlock();
b = ms.ToArray();
ret = System.Convert.ToBase64String(b, 0, b.Length);
ms.Close();
cs.Close();
return ret;
}
/// <summary>
/// Read an internal resource as a string.
/// </summary>
/// <param name="name">The fully qualified name of the resource, i.e. "PKPromo.Data.XmlTemplates.Flyers.Jazz".</param>
/// <returns>System.String.</returns>
internal static string ResourceString(string name)
{
System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
string s = "";
for(int i =0; i < stream.Length; i++)
{
s += (char)stream.ReadByte();
}
stream.Close();
stream = null;
return s;
}
/// <summary>
/// Determines the type of text encoding.
/// </summary>
public enum EncodeMethod
{
/// <summary>
/// Americian Standard Charecter Interchange I something.
/// </summary>
ASCII,
/// <summary>
/// Unicode. Provides support for wide charecters and an extende charecter set.
/// </summary>
UNICODE
}
/// <summary>
/// Convert a string into a Unicode array of bytes.
/// </summary>
/// <param name="value">The string to convert</param>
/// <param name="method">The encoding type to use.</param>
/// <returns>byte[]</returns>
internal static byte[] ToByteArray(string value, EncodeMethod method)
{
if(method == EncodeMethod.UNICODE)
{
return (new System.Text.UnicodeEncoding()).GetBytes(value);
}
else if(method == EncodeMethod.ASCII)
{
return (new System.Text.ASCIIEncoding()).GetBytes(value);
}
return null;
}
/// <summary>
/// Convert a unicode byte array to a string.
/// </summary>
/// <param name="value">The byte array to be converted.</param>
/// <param name="method">The encoding type to use.</param>
/// <returns>String repesentation of the in passed byte array.</returns>
internal static string FromByteArray(byte[] value, EncodeMethod method)
{
if(method == EncodeMethod.UNICODE)
{
return (new System.Text.UnicodeEncoding()).GetString(value);
}
else if(method == EncodeMethod.ASCII)
{
return (new System.Text.ASCIIEncoding()).GetString(value);
}
return "";
}
}
}
Magiaus
If I helped give me some points.
-
Jul 9th, 2004, 12:17 PM
#3
Thread Starter
Frenzied Member
p.s. Yes I plan to change the key and vector
Magiaus
If I helped give me some points.
-
Jul 9th, 2004, 04:48 PM
#4
Lively Member
[Edit]
OK I found my wokring backwards problem but I too now get the PKS7 Padding error....
What the hell it looks identical (sort of) to the example on MSDN...
MDSN RijndaelManaged Example
[/Edit]
PHP Code:
using System;
namespace Helpers.Encryptor
{
/// <summary>
/// Summary description for StringEncryptor.
/// </summary>
public class StringEncryptor
{
private static string cryptKey = "%ALL_YOUR_BASE_ARE_BELONG_TO_US%";
private static string cryptIV = "%WE_GET_SIGNAL!%";
public static void Main()
{
string enc = EnCrypt("All your base, base, base!");
string dec = DeCrypt(enc);
Console.WriteLine(enc);
Console.WriteLine(dec);
Console.Read();
}
/// <summary>
/// Method used for Data Encryption
/// </summary>
/// <param name="value"></param>
/// <returns>Encrypted String</returns>
public static string EnCrypt(string value)
{
value = value.Trim();
string ret = "";
byte[] key = ToByteArray(cryptKey);
byte[] iv = ToByteArray(cryptIV);
byte[] b = ToByteArray(value);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(
ms,
rm.CreateEncryptor(key,iv),
System.Security.Cryptography.CryptoStreamMode.Write);
cs.Write(b, 0, b.Length);
cs.FlushFinalBlock();
ret = FromByteArray(b);
ms.Close();
cs.Close();
return ret;
}
/// <summary>
/// Method used for Data Decryption
/// in conjunction with Encryption Method.
/// </summary>
/// <param name="value"></param>
/// <returns>Decrypted String</returns>
public static string DeCrypt(string value)
{
string ret = "";
byte[] key = ToByteArray(cryptKey);
byte[] iv = ToByteArray(cryptIV);
byte[] b = ToByteArray(value);
byte[] bl;
System.IO.MemoryStream ms = new System.IO.MemoryStream(b);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(
ms,
rm.CreateDecryptor(key,iv),
System.Security.Cryptography.CryptoStreamMode.Read);
bl = new Byte[b.Length];
cs.Read(bl, 0, bl.Length);
ret = FromByteArray(bl);
ms.Close();
cs.Close();
return ret;
}
internal static byte[] ToByteArray(string value)
{
return (new System.Text.ASCIIEncoding()).GetBytes(value);
}
internal static string FromByteArray(byte[] value)
{
return (new System.Text.ASCIIEncoding()).GetString(value);
}
}
}
Last edited by MedevH; Jul 10th, 2004 at 05:09 PM.
There is no good or evil only CODE! ~MedevH~
-
Jul 10th, 2004, 05:09 PM
#5
Lively Member
There is no good or evil only CODE! ~MedevH~
-
Jul 11th, 2004, 11:03 PM
#6
Lively Member
I've noticed only one difference between the MSDN Example and Your code (and mine) Magius.
Aside from all the MSDN code being thrown inside of the Main() method they never return an encrypted string to the Decryption code only and array of encrypted bytes...
PHP Code:
//Get encrypted array of bytes.
encrypted = msEncrypt.ToArray(); //...
MemoryStream msDecrypt = new MemoryStream(encrypted); //...
fromEncrypt = new byte[encrypted.Length];
//Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
They then decrypt and convert to a string for display.
Call it a hunch but if we were to pass in a string and pass out an array of byte and then pass back in the same array of bytes we may not get the padding error...
this code is untested cause I'm on a machine without the Framework installed on it but check this out and hit me back.
Basically the only thing I changed was the input a string return a string of both methods to input a string return byte (Encrypt) input a byte return a string (Decrypt)...
PHP Code:
using System;
namespace Helpers.Encryptor
{
/// <summary>
/// Summary description for StringEncryptor.
/// </summary>
public class StringEncryptor
{
private static string cryptKey = "%ALL_YOUR_BASE_ARE_BELONG_TO_US%";
private static string cryptIV = "%WE_GET_SIGNAL!%";
public static void Main()
{
byte[] enc = EnCrypt("All your base, base, base!");
string dec = DeCrypt(enc);
Console.WriteLine(FromByteArray(enc));
Console.WriteLine(dec);
Console.Read();
}
/// <summary>
/// Method used for Data Encryption
/// </summary>
/// <param name="value"></param>
/// <returns>Encrypted String</returns>
public static byte[] EnCrypt(string value)
{
value = value.Trim();
string ret = "";
byte[] key = ToByteArray(cryptKey);
byte[] iv = ToByteArray(cryptIV);
byte[] b = ToByteArray(value);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(
ms,
rm.CreateEncryptor(key,iv),
System.Security.Cryptography.CryptoStreamMode.Write);
cs.Write(b, 0, b.Length);
cs.FlushFinalBlock();
b = ms.ToArray();
//ret = FromByteArray(b);
ms.Close();
cs.Close();
return b;
}
/// <summary>
/// Method used for Data Decryption
/// in conjunction with Encryption Method.
/// </summary>
/// <param name="value"></param>
/// <returns>Decrypted String</returns>
public static string DeCrypt(byte[] value)
{
string ret = "";
byte[] key = ToByteArray(cryptKey);
byte[] iv = ToByteArray(cryptIV);
//byte[] b = ToByteArray(value);
byte[] bl;
System.IO.MemoryStream ms = new System.IO.MemoryStream(value);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(
ms,
rm.CreateDecryptor(key,iv),
System.Security.Cryptography.CryptoStreamMode.Read);
bl = new Byte[value.Length];
cs.Read(bl, 0, bl.Length);
ret = FromByteArray(bl);
ms.Close();
cs.Close();
return ret;
}
internal static byte[] ToByteArray(string value)
{
return (new System.Text.ASCIIEncoding()).GetBytes(value);
}
internal static string FromByteArray(byte[] value)
{
return (new System.Text.ASCIIEncoding()).GetString(value);
}
}
}
There is no good or evil only CODE! ~MedevH~
-
Jul 24th, 2004, 02:04 PM
#7
Thread Starter
Frenzied Member
fully tested and working
PHP Code:
public class Crypto
{
#region Crypto
internal static string cryptKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
internal static string cryptIV = "xxxxxxxxxxxxxxxx";
internal static System.Security.Cryptography.RijndaelManaged rj;
internal static string Encrypt(string value)
{
byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(value);
byte[] key = System.Text.ASCIIEncoding.ASCII.GetBytes(cryptKey);
byte[] iv = System.Text.ASCIIEncoding.ASCII.GetBytes(cryptIV);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
rj = new System.Security.Cryptography.RijndaelManaged();
rj.Key = key;
rj.IV = iv;
System.Security.Cryptography.ICryptoTransform encrypt = rj.CreateEncryptor();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encrypt, System.Security.Cryptography.CryptoStreamMode.Write);
cs.Write(b, 0, b.Length);
cs.FlushFinalBlock();
byte[] bo = ms.GetBuffer();
int i = 0;
for(i = 0; i < bo.Length; i++)
{
if(bo[i] == 0) break;
}
return System.Convert.ToBase64String(bo, 0, i);
}
internal static string Decrypt(string value)
{
byte[] b = System.Convert.FromBase64String(value);
byte[] key = System.Text.ASCIIEncoding.ASCII.GetBytes(cryptKey);
byte[] iv = System.Text.ASCIIEncoding.ASCII.GetBytes(cryptIV);
System.IO.MemoryStream ms = new System.IO.MemoryStream(b, 0, b.Length);
rj = new System.Security.Cryptography.RijndaelManaged();
rj.Key = key;
rj.IV = iv;
System.Security.Cryptography.ICryptoTransform encrypt = rj.CreateDecryptor();
System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encrypt, System.Security.Cryptography.CryptoStreamMode.Read);
System.IO.StreamReader sr = new System.IO.StreamReader(cs);
return sr.ReadToEnd();
}
#endregion
}
Magiaus
If I helped give me some points.
-
Sep 30th, 2005, 08:05 AM
#8
New Member
Re: Encryption [Resolved]
-
Sep 30th, 2005, 08:42 AM
#9
Thread Starter
Frenzied Member
Re: Encryption [Resolved]
umm, hi there Mr. Testing
Magiaus
If I helped give me some points.
-
Oct 3rd, 2005, 02:13 PM
#10
Lively Member
Re: Encryption [Resolved]
umm, hi there Mr. Testing
Hi indeed... what the hells is [email protected] all about... are you such a newbie you don't know wether or not your internet browser support form posting?
Wrong place for that might i suggest The Internet for Dummies
Last edited by MedevH; Oct 3rd, 2005 at 02:20 PM.
There is no good or evil only CODE! ~MedevH~
-
Oct 3rd, 2005, 10:24 PM
#11
Thread Starter
Frenzied Member
Re: Encryption [Resolved]
I was just looking at his name and my avatar and I happened to wonder could this be one of the Magiaus and Medevh fan club jerking our chain...?
But, then I was like nah that's just my parinoia and the fact that this the only post to date by umm Mr. Testing....
\\\--> Fan Club Can be found but looking is not recomended <--///
Clue 1: A Big Project
Clue 2: Magiaus
Clue 3: Madevh
Clue 4: Search
Magiaus
If I helped give me some points.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|