Click here to Skip to main content
15,889,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, in my web application on sign up page i am using encription function to encript emailid of user and store it in database. and using decript function on verification page to verify user id.
while i sign up with some small email id it works fine but while using some big email id, big in the seance more characters in email id so encription doing well but while verifying it gives error that "invalid length for base 64 decode base 64 char array below is given my code.. please help...thanks in advance.








C#
public static string base64Encode(string sData)
      {
          try
          {
              byte[] encData_byte = new byte[sData.Length];

              encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);

              string encodedData = Convert.ToBase64String(encData_byte);

              return encodedData;

          }
          catch (Exception ex)
          {
              throw new Exception("Error in base64Encode" + ex.Message);
          }
      }

      public static string base64Decode(string sData)
      {
          try
          {

              System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();

              System.Text.Decoder utf8Decode = encoder.GetDecoder();

              byte[] todecode_byte = Convert.FromBase64String(sData);

              int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

              char[] decoded_char = new char[charCount];

              utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

              string result = new String(decoded_char);

              return result;
          }
          catch (Exception ex)
          {
              throw new Exception("Error in base64Decode" + ex.Message);
          }

      }

  }
Posted
Updated 4-Apr-12 20:24pm
v2

http://developergeeks.com/code/204/how-to-encrypt-and-decrypt-information-in-aspnet-web-page

from the above link replace password with mail id and check it should work
 
Share this answer
 
Comments
betu.009 5-Apr-12 2:38am    
if i want to use my same code..what changes should i do?
Hi,

My suggestion is for storing email address use MD5 algorithm(SQL). i think that would be better option as it is irreversible algorithm.

i know , you have question like if MD5 is irreversible then how can we match at the time of login ?

Answer is, you do not need to Decrept your email address, just pass it to your storedprocedure and it will again encrypt user input and match it with database value. then only problem with this flow is, when you would like to recover your email address then it is not possible. but yes you can do like user will enter his/her email address and if it is correct we can send an email on registered email address.

This method is very simple and effective for storing encrypted email address. and even it will not take more time / more database space for storage.

Hope above information will help you,

Thanks
-Amit
 
Share this answer
 
Comments
betu.009 5-Apr-12 2:45am    
you are right but in my case recover is also necessary.
AmitGajjar 5-Apr-12 2:46am    
Then ofcourse you can use MD5.
betu.009 5-Apr-12 3:07am    
i tried that but still gives me same error. error convertion base 64 char array
AmitGajjar 5-Apr-12 4:33am    
use CONVERT(VARCHAR(32), HashBytes('MD5', 'email@dot.com'), 2) function in your stored procedure for MD5 storage.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900