[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

UTF8Encoding - Encoding Decoding does not work in WCF Service

Posted on 2014-01-03
9
Medium Priority
?
424 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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