Results 1 to 11 of 11

Thread: Encryption [Resolved]

  1. #1

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Red face 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.

  2. #2

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    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(cryptKeyEncodeMethod.ASCII);
                
    byte[] ToByteArray(valueEncodeMethod.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(keycryptIV),
                    
    System.Security.Cryptography.CryptoStreamMode.Read);
                
                
                
    cs.Read(bt0bt.Length);
                
    cs.FlushFinalBlock();
                
                
    ms.Close();
                
    cs.Close();
                
                
    ret FromByteArray(btEncodeMethod.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(cryptKeyEncodeMethod.ASCII);
                
    byte[] ToByteArray(valueEncodeMethod.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(keycryptIV),
                    
    System.Security.Cryptography.CryptoStreamMode.Write);
                
                
    cs.Write(b0b.Length);
                
    cs.FlushFinalBlock();

                
    ms.ToArray();
                
                
    ret System.Convert.ToBase64String(b0b.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 =0stream.Lengthi++)
                {
                    
    += (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 valueEncodeMethod 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[] valueEncodeMethod 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.

  3. #3

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    p.s. Yes I plan to change the key and vector
    Magiaus

    If I helped give me some points.

  4. #4
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91
    [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[] 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(b0b.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[] 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(bl0bl.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~

  5. #5
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91
    UPDATED
    There is no good or evil only CODE! ~MedevH~

  6. #6
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91
    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(fromEncrypt0fromEncrypt.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[] 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(b0b.Length); 
                
    cs.FlushFinalBlock();
                
    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(bl0bl.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~

  7. #7

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Cool 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[] 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(msencryptSystem.Security.Cryptography.CryptoStreamMode.Write);

                
    cs.Write(b0b.Length);
                
    cs.FlushFinalBlock();

                
    byte[] bo ms.GetBuffer();
                
    int i 0;
                for(
    0bo.Lengthi++)
                {
                    if(
    bo[i] == 0) break;
                }
                return 
    System.Convert.ToBase64String(bo0i);
            }
            
    internal static string Decrypt(string value)
            {
                
    byte[] 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(b0b.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(msencryptSystem.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.

  8. #8
    New Member
    Join Date
    Sep 2005
    Posts
    8

    Re: Encryption [Resolved]


  9. #9

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Encryption [Resolved]

    umm, hi there Mr. Testing
    Magiaus

    If I helped give me some points.

  10. #10
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91

    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~

  11. #11

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Thumbs down 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
  •  



Click Here to Expand Forum to Full Width