Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am thinking to encrypt the password using RSAAlgorithm but i am getting this error "
Input string was not in a correct format.
" Please someone explain me what is going wrong. I will paste the code here

What I have tried:

Encryption Key: Method
public string EncryptWithKey(string Message, string PublicKey)
{
    string bitStrengthString = PublicKey.Substring(0, PublicKey.IndexOf("</BitStrength>") + 14);
    int KeySize = Convert.ToInt32(bitStrengthString.Replace("<BitStrength>", "").Replace("</BitStrength>", ""));
}


Getting error at second line KeySize parameter.

I have used this method in C# like:
rsaEncryption.EncryptWithKey(password, PublicKey);


Here rsaEncryption is a class object
Posted
Updated 12-Mar-18 21:29pm
Comments
Member 8583441 13-Mar-18 3:28am    
I have given PublicKey in c# like in 3 ways:
1) "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
2) "123456789abcdefghijklmnopqrstuvwxyz"
3) "123456789"

But no use getting same error

If i am using third one i.e., "123456789" --> "Input string was not in a correct format."

Second One and first one "123456789abcdefghijklmnopqrstuvwxyz" --> Getting another error.

1 solution

Convert.ToInt32 takes a string and converts it to an integer - but if the string contains any characters which are not digits, then it will throw an exception.
You need to use the debugger to look at exactly what you are passing to Convert.ToInt32 - so break it out a bit:
C#
string bitStrengthString = PublicKey.Substring(0, PublicKey.IndexOf("</BitStrength>") + 14);
string part1 = bitStrengthString.Replace("<BitStrength>", "");
string part2 = part1.Replace("</BitStrength>", "");
int KeySize = Convert.ToInt32(part2);
Now put a breakpoint on the final line, and look at what is in part1 and part2.

Until you know that, you have no idea what you need to do to fix this - and we can't do that for you!
 
Share this answer
 
Comments
Member 8583441 13-Mar-18 4:34am    
i am getting the same output value for part1 and part2 i.e., "123456789abcd"
still getting error in keysize
OriginalGriff 13-Mar-18 4:46am    
And there is your problem: "123456789abcd" is not a valid number.
So now you need to work out what part of that you need - or even if you are looking at the wrong data altogether. I suspect the latter: 123456789 does not look like a valid key size to me at all!
Member 8583441 13-Mar-18 5:18am    
Then can you suggest me the valid key sir
Member 8583441 13-Mar-18 4:39am    
If the publikey="" then the error is "Index and length must refer to a location within the string"

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