views:

68

answers:

2

how will i convert the following public key to integer.

these are the RSAKeyValue:

Modulus: yf4I7PVef43rZ2NdPFA5FQFb/y/k/5Awqrwc+/VDUimthRg4C5K2P6EUhU5n2m4HUiz102LIuwsYDYuyHwG3VUbAb4zjqxiOwrSpsHfCvgOdLsb4DBrXFFGp5kMtoZrDzl84tnDlyYgy8v3o5Qp2eeQgDaau2PhYUxoco6IArHU=

Exponent:AQAB

i want to do this bcoz i want to take its xor with another integer value...

plz help here...

thnx...

A: 

If the key is already in a string you can call GetHashCode on it which will return an int. Obviously as you want to take a much larger value, and then represent it with an int (32 bits) you will have collision issues with any function you use.

Update

@mattmc3 makes a good point (see comments). There is no guarantee between .net framework versions that a call to GetHashCode will return the same value when called on equivalent strings. Also calls may return a different value when called in different builds of the framework e.g. 32bit vs 64bit.

chibacity
thnx for ur reply... wt do u mean by collision issues..?
Jamal Abdul Nasir
@Jamal If you take your key which has a large number of bits (e.g 512) and then use a function to reduce this to a 32 bit representation (i.e. an int) then you cannot do this functionally without loss of information. The key factor here is that there is a probability that multiple keys can result in the same int - this is a collision.
chibacity
As far as I understand the question, this is a complete nonsense answer...
Thorarin
@thorian well I interpretted it as I understood it. It has been accepted as the answer. If you are going to describe my answer as nonsense, then please do explain what you think the question is.
chibacity
Well, `GetHashCode` will get you a 32-bit int, but it's completely meaningless. The way I read the question was that the poster wanted to represent the string an an *arbitrary length* integer (without data loss), so he can perform some binary logic on it.
Thorarin
The question is perhaps vague then. Surely a comment on the question rather than a rude comment on someone else's answer is more productive and polite. And downvoting based on question vagueness, that's pretty shabby too.
chibacity
I agree the question is a little vague, but XOR'ing an already meaningless integer with something else makes no sense, so you could have deducted that was not the intention. My apologies for the bluntness however.
Thorarin
You are missing a very big point here - the OP accepted my answer, and you are making these points and voting me down after he accepted the answer. I give up. :)
chibacity
By definition, you CANNOT rely on the values returned by GetHashCode(). It can change in the future and then you are SOL. See http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx and http://connect.microsoft.com/VisualStudio/feedback/details/344006/system-string-gethashcode-inconsistency-across-platforms-32-bit-and-64-bit .
mattmc3
o komon guys dont fight.. actually if u get a public key in python u will get an integer... but in dotnet it is not the case... so thats y i want to make it integer over here to have its xor with other integer value... but later on when i did research on my algorithm i was unable to get back that public key again from which i got hash code... so em stuck... now em again thinking on my algorithm... :( anyways thnx aloadz aloadz guyz... really appreciate it...
Jamal Abdul Nasir
A: 

Without going into why you'd want to do this in the first place; if you want to XOR the modulus with another, there is no need to represent it as a big integer first. You can use Convert.FromBase64String to get an array of bytes, and XOR those byte for byte.

string firstModulus = "yf4I7 ...etc... HU=";
string secondModulus = "...";

byte[] m1 = Convert.FromBase64String(firstModulus);
byte[] m2 = Convert.FromBase64String(secondModulus);

byte[] xor = new byte[m1.Length];
for (int i = 0; i < m1.Length; i++)
    xor[i] = (byte)(m1[i] ^ m2[i % m2.Length]);

string xorString = Convert.ToBase64String(xor);

Edit: I've updated the code to allow m2 to be shorter than the m1 array. The array m2 can be filled from any source. In this example I use a second base64 string, but if you use a regular integer, you can use BitConverter.GetBytes to convert it to a byte array.

For the record, .NET 3.5 does not have a built-in integer class that is big enough to hold the entire modulus. The whole reason that RSA works is because they are very large numbers. .NET 4 has BigInteger, but you don't need it here.

Thorarin
The OP makes no reference to having a second modulus.
chibacity
No, but he mentioned some other integer value. This is just an example.
Thorarin
mattmc3