Click here to Skip to main content
15,935,248 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi all,

When i try to decrypt my password which is stored in binary(16),null datatype in DB, it gives me error like 'Invalid length for a Base-64 char array.'.

i try the below code from one of the asp.net forum.

C#
public string DecryptPassowrd(object obj)
{
    string password = obj.ToString();
    System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
    System.Text.Decoder utf8Decode = encoder.GetDecoder();
    byte[] todecode_byte = Convert.FromBase64String(password);
    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;
}


I got the error at byte[] todecode_byte = Convert.FromBase64String(password); line.

when i googled for that error many of site says to replace " " to "+". but when i did this it gives me another error.

String cannot be of zero length.
Parameter name: oldValue

i am trying to decode "0xA02B92F62E209BC4058E08CF4F6C9689".

so what sould i do to overcome this error?

thanks,
krunal
Posted
Updated 16-Jul-12 21:00pm
v2
Comments
ssd_coolguy 17-Jul-12 3:11am    
hei.. i am not sure but this error comes when your string contains any blank space.. that's why you need to replace youre sting with " " to "+".
kk2014 17-Jul-12 4:25am    
i think there is no blank space in my string i have provided but solution like this given on net. 0x indicating it is hex string.
OriginalGriff 17-Jul-12 4:52am    
"so can u give/suggest me code to decrypt the string in normal plain text which i had provided?"

Sorry, but no - I have absolutely no idea how you encrypted it.
Start with the code you used to that - it should be fairly clear.

That doesn't look like a base64 string - it looks like a normal hex number - albeit a bit hex number - and the "0x" prefix does sort of hint at that as well.

I would go back to where you stored the information, and look at the process you used to convert it from binary to a format you can store in a DB - then reverse it.

BTW: You do realize that Base64 is not an encryption algorithm in nay way, don't you? And that it provides the same security level as printing your data on a T shirt and selling them on Ebay?
 
Share this answer
 
Comments
Sandeep Mewara 17-Jul-12 4:44am    
Comment from OP:

ok nop.

so can u give/suggest me code to decrypt the string in normal plain text which i had provided?

thanks.
OriginalGriff 17-Jul-12 4:51am    
Sorry, but no - I have absolutely no idea how you encrypted it.
Start with the code you used to that - it should be fairly clear.
OriginalGriff 17-Jul-12 4:52am    
Sorry Sandeep - I didn't notice it was from you!
Sandeep Mewara 17-Jul-12 5:37am    
No problemo! :)
try this it will work

public string Decryptdata(string encryptpwd)
{
string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
decryptpwd = new String(decoded_char);
return decryptpwd;
}
 
Share this answer
 
add an space between ""

"" should be " "
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900