Solved

UTF8Encoding - Encoding Decoding does not work in WCF Service

Posted on 2014-01-03
9
390 Views
Last Modified: 2014-03-11
i have this function  to encode a string -
 private string EncodePasswordEntered(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);
            }
        }

I call EncodePasswordEntered("y") to get encrypted value for my password - "y"
Then i send that encrypted password to a WCF Service -
ValidateMyPassword(myencryptedpwd);

Then In ValidateMyPassword function, i call function
this code to decode the that encoded value - DecodePassword(myencryptedpwd); .. the value returned by DecodePassword(myencryptedpwd) should be "y" but it is some alphanumeric value
Code of DecodePassword is below
public string DecodePassword(string sData)
       {

            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;

        }

   

pl help
0
Comment
Question by:ts84zs
  • 5
  • 4
9 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 39754776
I tested your code in LINQPad, and it seems to work fine for me. Are you receiving an error message?

Screenshot

Side note:  Base-64 encoding is not encryption. If you actually need encryption, then you need to employ the classes found under the System.Security.Cryptography namespace.
0
 

Author Comment

by:ts84zs
ID: 39754840
my code does not work when used in WCF, otherwise it works
so my password validation occurs in WCF and i have to decode that password in my WCF service


Side note:  Base-64 encoding is not encryption. If you actually need encryption, then you need to employ the classes found under the System.Security.Cryptography namespace.
ya i want to do encryption
can you please give me example thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39754849
When you say "my code does not work," what behavior are you witnessing?
0
 

Author Comment

by:ts84zs
ID: 39754879
for e.g. if i call  
someencodedvalue = EncodePasswordEntered("y")
now I have to pass that password to a WCF Service

so in that WCF Service function, i call  DecodePassword(someencodedvalue) and i pass someencodedvalue  
 
So DecodePassword(someencodedvalue)  should return "y"
but it returns some alphanumeric value
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39754893
Put a breakpoint on the first line of the DecodePassword method. Call your service, and once the breakpoint is hit, inspect the value of the sData variable. Does it match what the other application sent when it called the service?
0
 

Author Comment

by:ts84zs
ID: 39754975
oops my mistake, i was doing
someencodedvalue=someencodedvalue.ToUpper()

in my code and then calling DecodePassword(someencodedvalue)  

after i removed someencodedvalue.ToUpper() , it is working fine

is there any risk in using this type of encryption? i just do not want the actual password to be passed in the service...

thanks a lot
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39754988
is there any risk in using this type of encryption?
Certainly, because as I said it's not encryption. Base-64 is an encoding, not an encryption.

Are you using SSL at all?
0
 

Author Comment

by:ts84zs
ID: 39755005
no.... i do not use SSL
0
 

Author Comment

by:ts84zs
ID: 39755006
pl guide... if required i can open another ticket/question
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now