|
What would you like it to be?
|
|
|
|
|
I have searched and searched, and I just cannot find this solution. I know its out there becasuse I found it before.
Ok, I have a byte array of a fixed length, say 5 bytes.
I want to represent the byte array as a fixed length number, without using int, long etc etc.
I then want to be able to have the number entered by the user into an edit box, and encode it back to the byte array.
So, for example...
User enters the following (these are examples, not actual values)
2354 3488 3433 2332
Then this in decoded into its byte array representing the data.
In my seperate generator, I set the bytes, and it encodes as:
2354 3488 3433 2332
I am really looking for 2 brief code sample functions like the following
private void convertSerialNumber(string serial)
{
encode into byte array
}
private void convertByteToSerial (byte[] arr)
{
decode into serial (2354 3488 3433 2332
}
The reason I need to do this, is once I generate the byte array, I want to encrypt it.
If I simply use standard conversion routines, or use the string to int fuctions etc, I cannot do this
Could someone PLEASE provide brief coding examples.
I believe the functions I found before were custom functions using bit operators.
Converting and displaying as hex is no problem as they are built in, and I can find many examples of BASE32 encode/decode, but I relay want to represent my data as digits like 0000 0000 0000 0000
I hope this is clear enough as it is yet again driving me insane, and I am trying, I have spent a few months yet again.
Thank you so much in advance.
Steve
|
|
|
|
|
By default a number is already in base 10. Unfortunately, you are confusing a few concepts. For example:
string number = "2354 3488 3433 2332";
is not a base 10 encoded number it is a Unicode string occupying 20 bytes. Here is what you most likely want:
using System.Security.Cryptography;
using System.IO;
...
Int32 anInteger = 1233455;
DES des = DESCryptoServiceProvider.Create();
ICryptoTransform transform = des.CreateEncryptor();
CryptoStream stream = new CryptoStream(memorySteam, transform, CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(stream);
writer.Write(anInteger);
|
|
|
|
|
Hi Steve,
1.
stephen.darling wrote: 2354 3488 3433 2332
I don't see how that would be an example. A real example would show data before and after the conversion. As it stands, it is just a data sample.
Furthermore, 4-digit numbers will not fit in a byte, as you well know. So how many bytes do you want? One per character (just replace each char by its ASCII ordinal number?).
2.
if 2354 is a string at some point, you can find the equivalent integer by calling upon int.Parse() or int.TryParse(), which I recommend.
3.
Maybe what you want is offered by the BitConverter class; have a look at GetBytes() and some of the ToInt() methods. [[FIXED]]
4.
Probably there are better schemes, that are even easier to implement.
example1: get the input string, convert it to bytes right away, then encrypt. No need to know the numeric values at all.
example2: let the user enter in the format you choose, check the format, remove all non-digits (using string.Replace), then just call long.Parse() once, to get a single 64-bit number.
5.
You seem to be struggling with some elementary C# stuff, are you sure your app needs serial numbers, encryption, and this early stage?
|
|
|
|
|
Luc Pattyn wrote: GetInt()
Surely you mean ToInt?
|
|
|
|
|
yep.
|
|
|
|
|
Your example data is very confusing. You have 8 bytes of data that you are trying to put into a 5 byte array. Without knowing exactly how big your individual elements are there is no way to decode, because you won't know where the boundaries are.
And then the other weird thing is that the numbers are already in base 10.
What exactly is the problem you ran into when you tried to encrypt using standard conversion routines?
|
|
|
|
|
I proberbly havent explained very clearly.
Yes, the following is in base 10
1234 5678 1234 5678
But if I was to simply encrypt the above I would have something like
£$%^%$"$£% (example)
I need to accept a string of digits, then get its bytes.
Then, encrypt the bytes
Then, re-display the encytped bytes as a base 10 representation
So, for example.
I have a byte array
Byte[1] = ID
Byte[2] = version
etc
Then I encode it into a string of base10
0000 0000 000 etc
Then if we enter the above number into a sep function, we can decode it back to
byte[1] etc etc
I did find this before....
Also, the example above, saying to use int64 is no good if my serial number is, for example, 25 digits long
Thank you
Steve
|
|
|
|
|
I'm still not sure what you want. Why not do something like:
string str = arr[0].ToString() + " " + arr[1].ToString();
|
|
|
|
|
Is this to encode or decode, and what function would I use to reverse it?
Also, to clarify, microsoft activation number which is 51 digits long, when decoded into a byte array, is said to be simply a byte representation in litle endian, or a multi presision representation.
You see, if I simply encrypt a string of digits I end up with unreadable data, I could use built in functions to encode it into a hex string or Base32 etc, but I want to represent it as digits.
This is very hard to explain for me, sorry.
Steve
|
|
|
|
|
We still don't get what data you are after. Your last example had version and ID in the array. But we don't know if serial number is supposed to go in the array with those pieces of data or is serial number is supposed to be derived from the data in the array. We also don't know if any of the other potential pieces of data in the array are larger than one byte.
|
|
|
|
|
stephen.darling wrote: This is very hard to explain for me,
Suggestion: forget all technical terms, explain what the *probably non-technical) user sees and is supposed to do (and why). i.e. provide a functional specification. That would be a good start.
|
|
|
|
|
OK, let me try one more time, seen as your all being patient with me, which is greatly appreciated.
I will have a license generator that will create a fixed length serial number like:
0000 0000 0000 0000
This will be built from a range of bytes, though the exact number of bytes is not set in stone yet.
I thought I would use some routine to set some bytes like:
byte 1 = 0x00 (ID of program)
byte 2 = 0x00 (version)
etc etc
Then, I want to encrypt the byte array, then present it as a string of digits (a number that is an encoding of the byte array)
0000 0000 0000 0000
Then when the user types this into the application, I decode it back into bytes so I can decrypt it and get at each byte.
If I simply created a serial number as a string of numbers:
string serial = ("1234 1234 1234 1234")
and encrypted it, it is no longer numbers.
To add, all of this comes from the following paper, with actual code in c++
http://www.licenturion.com/xp/fully-licensed-wpa.txt[^]
but even if I managed to convert the code into c#, it only has decoder functions and not encoding functions.
Thank you again
Steve
|
|
|
|
|
stephen.darling wrote: encrypted, it is no longer numbers.
That is where you go wrong IMO. Encryption manipulates bits, it does not enforce a representation or format.
Here is my suggested approach:
A. you come up with a byte array holding the information you want;
B. you encrypt that into a new byte array;
C. so now the only remaining problem is to represent the encrypted byte array as a series of numbers (for human comfort I guess), here are a few possibilities:
C1. use base-8 (i.e. octal digits) for each byte, yielding values in the range [000,377] = 3 digits
C2. combine two bytes into a ushort, then use decimal, yielding values [00000,65535] = 5 digits
C3. combine 4 bytes into a uint [[FIXED]], then use decimal, yielding [0, some 4 billion] = 10 digits (no gain!)
I recommend C2.
combining bytes (unsigned by default) into longer unsigned numbers can be done with simple formulas, such as:
byte hi=...;
byte lo=...;
ushort hilo = (hi<<8)|(lo);
BTW: one can discuss which byte is hi, which is lo; it does not really matter as long as you do it consistently one way or the other.
|
|
|
|
|
Thank you for the responce, would your option (c2) do the following, as I found the part of the paper that concerns me, so here it is qouted from the paper itself...
"a 41-digit decimal number. A
decimal number of this length roughly corresponds to a 136-bit binary
number. In fact, the 41-digit number is just the decimal encoding of
such a 136-bit multi-precision integer, which is stored in little
endian byte order as a byte array. Hence, the above Installation ID
can also be represented as a sequence of 17 bytes as in
0xXX 0xXX 0xXX 0xXX 0xXX 0xXX 0xXX 0xXX
0x94 0xAA 0x46 0xD6 0x0F 0xBD 0x2C 0xC8
0x00"
Now, when I looked at the code, there was alot of << operations etc, so that maybe closer.
Also, how do I combine it all together using your option?
If a ushor gives me 5 digits, how do I combine the bytes to form one 20 or 25 digit number?
Thank you again
Steve
|
|
|
|
|
You lost me again. Do you have to implement what is in the text, or are you just looking for a working approach?
Anyway, they may actually have a 136-bit number in there, although I suspect they invented that to explain things more easily (i.e. I think they give a mathematical expose, not a code explanation).
What I proposed is, for 2N bytes, you have N 5-digit numbers; you never hold an overall number. You do have a string containing those N 5-digit numbers and some spaces, if that is the format you choose. It takes some string operations to aggregate that (and later split it again).
|
|
|
|
|
I was just looking for a working approach, although, there is code in c++ that decodes a string of 41 digits like the following "34234234459890798734987298374 etc" into that byte array. When I use my graphic calculator, the long string of digits does end up being the byte array, it just seems so hard to implement.
It is stange, if I have a byte array, I can very quickly output it as a string, represented as a hex or base32 etc. But representing a byte array as numbers seems so difficult.
Thank you for your help anyway, I do appreceate it alot.
I think for now Ill have to generate the serial number by hand and leave it unencrypted.
Thank you again,
Steve
|
|
|
|
|
stephen.darling wrote: But representing a byte array as numbers seems so difficult.
Do you want to assume the byte array is a binary representation of a large number, then convert that number to it's base10 form in a string? For example:
byte[] bigNumber = {1, 22};
Would be converted to:
string strBigNumber = "278";
You can use the BigInteger data type in .Net Framework 4 to help with that. There are also articles on how to do this if you are using an earlier version of the .Net Framework.
|
|
|
|
|
Aw bignumber? Ill have a look into that. thank you
Steve
|
|
|
|
|
Hi,
I don't know if I may help, but if I understand it well
your problem goes like this:
1. you have a fixed sequence of bytes, say you have
an array of 16 bytes. This may represents a number (more
precisily an unsigned or signed integer) in extended precision
in binary format (i.e. in base 2).
2. you want to handle this number and to convert it in
decimal base (base 10). Hence you get a nice base 10
representation outputted as a formatted string.
3. you want to be able to go back from the decimal
representation to the binary (base 2) one.
Trivial Example: do suppose your array is just 4 bytes long
(i.e. the standard size of an int)
namely you have in hex the value 0x0abc
stored as a four byte array in binary (base2) as:
(leading zeroes omitted) ...0000101010111100
you then want to convert it to the decimal representation
(base 10) with result: the string "2748".
and then be able to convert it back to its binary format
in base 2 in order to access it byte wise.
Well if this is right I think then you shall use
multiple precision arithmetique techniques: i.e.
you define a size (say 128 bytes) for your extended
precision integer and then you want a couple of functions
encodeBase10, decodeBase10 in order to be able to represent
such a number as a decimal string and convert back from it.
If such is your intention, you may take a look at the
GNU library "gmp", i.e. a multi precision library.
-----------
Instead of the solution above, you may want to do a translation byte
by byte, then the other method proposed in this thread
is just to output a converted value for each byte
in decimal taking care to pad with leading zeros where
needed, i.e. you transform from this:
(base 2) 00011010 00001111 00000111 10000011 - four bytes
to this
(base 10) 026 015 007 131 - string with 12 digits
and then convert back by grouping the string digits
3 by 3.
or if using ushort's instead of bytes,
you play with groups of 5 decimal digits
(always padding leading zeroes).
In this second case, for example, you have that a 25 long
string base10 digit represents 6 unsigned short numbers
and hence 12 bytes.
Hope this points you to the desired solution,
Cheers
|
|
|
|
|
Don't you mean an uint at C3?
|
|
|
|
|
I sure do. Thanks.
|
|
|
|
|
Is it possible to write a code that adds today's date to a jpg photo?(the date is changed everyday)
|
|
|
|
|
To what? The filename? The image itself? Data inside the image (aka steganography)?
|
|
|
|
|
if you want to create a new image file containing the original image plus a legend or a watermark, this pseudo-code should do:
Bitmap bm=Bitmap.FromFile(filepath);
Graphics g=Graphics.FromImage(bm);
g.DrawString(...);
bm.Save(someOtherFilepath, ImageFormat.Jpeg);
bm.Dispose();
FYI: this simple scheme will fail when someOtherFilepath==filepath
|
|
|
|
|