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
Solved

UTF8Encoding - Encoding Decoding does not work in WCF Service

Posted on 2014-01-03
9
398 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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