Solved

UTF8Encoding - Encoding Decoding does not work in WCF Service

Posted on 2014-01-03
9
387 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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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 74

Expert Comment

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

Author Comment

by:ts84zs
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
no.... i do not use SSL
0
 

Author Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

10 Experts available now in Live!

Get 1:1 Help Now