Solved

UTF8Encoding - Encoding Decoding does not work in WCF Service

Posted on 2014-01-03
9
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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
 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 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