short encryption for integer [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I gave a task which encrypts an input integer value, this integer maximum 4 length, however, I'm required to encrypt into an alphanumeric string. Apart from this, the result that i generated from the same value (eg 10) the value have to not same. The most difficult parts is, The encrypted string maximum to only can have 15 length since we have to put it in the query string. It was a difficult task and I tried to ask google and I don't found any solution can help me with this. all the length is too long and doesn't meet the requirement I needed. Any encrypt professional can help me with this?

Assumptions: "integer maximum 6 length" means 6 numeric characters 000000-999999.
Encrypt with a algorithm that has a 8-byte block size and then Base64 encode, that will produce 12 characters of output.
Append 2 random bytes to the 6 characters of data to make 8-bytes, this will cause up to 2^16 or 65536 different results on encryption of the same value. Encrypt in ECB mode and Base64 encode. That will produce 12 characters of output.
To recover the input decode the Base64 encrypted to data, decrypt that and delete the 2 random bytes.
Possible encryption algorithm include Blowfish, XTEA, DES and others.
Note: For a larger range of different output the 6-digit number could first be converted to a binary representation of 3-bytes allowing 5 random bytes producing 2^40 different outputs for the same 6-digit input.

I assume that you want a function to input a four digit number: [0000 .. 9999] and produce a 15 character alphanumeric output.
You do not make it clear if this function is to be reversible. If reversibility is not needed, then a one-way hash function will do what you want. 15 hex characters are 60 bits or 15 Base32 characters are 75 bits. Use a larger size hash function, truncate and convert to hex or Base32. Base32 gives a wider range of output characters than hex.
For reversibility you will need a Format Preserving Encryption, where the output size is limited to 60 or 75 significant bits. For 60 bits, use DES as the base cipher as it has a 64 bit block size. 75 bits is more awkward. AES, at 128 bits, has too large a block size so you might need to write a simple 76 bit Feistel cipher. That will give you good obscurity, but only middling security. You do not say how secure this function needs to be.

Related

Hashing a byte array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have an input byte array which is huge in size ( > 8000 bytes). I need to store this byte array in DB, and fetch later for further operation.
The problem is, this byte array column is unique. Assume that if I need to retrieve the entire table information on this byte array column. It becomes extremely costly operation when it comes to byte comparison. So thought of storing the Hash Value of this byte array just to make the comparison operation easier.
Just wanted to know if hash value which is generated from the byte array will be unique OR is there any other way to achieve this.
If the size (in bytes) of the hash is smaller than 8000, this is not possible. After all, there are 256 ^ 8000 possible inputs, so there must be at least that many possible outputs if the hash function needs to be unique.
As the default C# hashcode returns an int, which (depending on your architecture) is 32 or 64 bits (so 4 or 8 bytes), this is not even close to be possible with the default hash function. (Of course, you could write your own hash function, but that's quite pointless.)

Splitting a 200 bit hexadecimal bitmask [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a bitmask of 200 bit stored as a hexadecimal value.
In order to apply the & bit operator, I have to first convert the hex to an integer but 200 bit is too big for a uint64 so my question is : how do I split my bitmask in 4 different hexadecimal value without loosing data?
So that I can also split my 200 bit data and then compare every chunk of data with the corresponding chunk of bitmask without altering the result.
You can use the BigInteger from System.Numerics (it's a separate assembly):
BigInteger bi = BigInteger.Parse("01ABC000000000000000000000000000000000", System.Globalization.NumberStyles.HexNumber);
VERY IMPORTANT: prepend a "0" before the hex number! (because BigInteger.Parse("F", NumberStyles.HexNumber) == -1, while BigInteger.Parse("0F", NumberStyles.HexNumber) == 15
BigInteger implement the "classical" logical operators (&, |, ^)
Requires .NET 4.0
The most efficient way of achieving this is writing a class that can store and do binary operations on 200bits of data, have strings as input, etc.

numeric conversion for 20 digits [duplicate]

This question already has answers here:
How do you deal with numbers larger than UInt64 (C#)
(8 answers)
Closed 8 years ago.
Hi all I am having a numeric digit with 20 characters as follows 34432523434234423434, I tried this converting using long,UInt64 but still I am getting an exception Value was either too large or too small so can some one help me out how can I convert this value
Your value is actually 65 bits long, so doesn't matter HOW you change its type, it will not fit into a 64bit variable.
2**64 = 18446744073709551616
your value = 34432523434234423434
Big integers aren't actually limited to 20 digits, they're limited to the numbers that can be expressed in 64 bits (for example, the number 99,999,999,999,999,999,999 is not a valid big integer despite it being 20 digits long).
The reason you have this limitation is that native format integers can be manipulated relatively fast by the underlying hardware whereas textual versions of a number (tend to) need to be processed one digit at a time.
If you want a number larger than the largest 64-bit unsigned integer 18,446,744,073,709,551,615 then you will need to store it as a string (or other textual field) and hope that you don't need to do much mathematical manipulation on it.
Alternatively, you can look into floating point numbers which have a larger range but less precision, or decimal numbers which should be able to give you 65 digits for an integral value, with decimal(65,0) as the column type.

Handling special characters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am wondering how to best handle a special character such as ’ using c#?
e.g
public static string DecodeFrom64(string toDecode)
{
byte[] arrayToDecode = System.Convert.FromBase64String(toDecode);
return System.Text.Encoding.Unicode.GetString(arrayToDecode);
}
The problem here is that you've stored a UTF-8 string to a different encoding in your database - probably the Windows-1252 code page (CP2152). As a result the UTF-8 character ’ represented by the byte sequence E2 80 99 is translated into the CP2152 single-byte characters ’. This was all explained to your previously in this answer, which also gives a solution to your current problem.
In order to get back to the original UTF-8 encoding you will need to take the string returned from your database and correct it with the following code:
public static string UTF8From1252(string source)
{
// get original UTF-8 bytes from CP1252-encoded string
byte[] bytes = System.Text.Encoding.GetEncoding("windows-1252").GetBytes(source);
return System.Text.Encoding.UTF8.GetString(bytes);
}
This highlights the fact that it is vital to use the correct encoding at all times when using the GetBytes method.
It is important to note that the reverse of this transformation is not always possible, since there are gaps in the CP2152 code space - values that will be discarded or altered during conversion from byte values.
The hex values for these gaps are: 81 8D 8F 90 9D.
Unfortunately these values are present in various UTF-8 encodings, such as ” (E2 80 9D). If you have one of these values in your database then it will not load correctly. Depending on how you did the first stage conversion the third byte may be lost or corrupted in the database, in which case you cannot retrieve it.

Encryption and Decryption With Only Special characters [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
i have to Encrypt and decrypt a String but with only Special characters
i dont want alphanumeric to be part of encryption
i have tried Rijndael Encryption and i am getting only aplphanumeric Output
how can i get Special Characters
is there any other Algorithm for special characters
Tried TripleDes provider and same result , is there any algorithm for selecting only special characters while encrypting
I have A product and before shipping the Product i generate the Serial Key which contains Information about license ,now when Client activates the Software i want to Encrypt the Machine name + Serial key + motherboard no to generate the Activation key,now when ever my client opens the software i will decrypt the Activate key and validate the serial key and also whether he is using any other machine which i will compare with machine id and mother board no,for that i need a encryption which contains only special characters if it is possible ,till now its not working
Computers are digital. In other words, all of the data they manipulate, including cipher text output from encryption, are discrete numbers.
So, when you say that the output is alphanumeric, you are really talking about a transformation of the output, not the output itself. Just use a different transformation on the output. Common transformations are Base-64 and hexadecimal, but these don't meet your (rather strange) requirements; you'll probably have to make up your own.
For example, you could represent the cipher text with "+" and "-" characters, mapping these to zero and one bits. Of course that could make some really long character strings!
Alternatively, if you could identify 16 "special characters", you could map each to a 4-bit value, and use something that amounts to hexadecimal with special characters instead of digits and letters.
Can I ask why in the world you want to do this?
Take the hex characters you get from a hex output, map them one-to-one with some "special" characters and then map back on decrypting.
I'll bet this isn't solving any real problem. Apart from Solitaire, there hasn't really been many modern encryption systems using characters rather than bytes since the end of WWII, we just use characters in rendering the output.
Certainly, mapping like this isn't going to add anything in the way of security, just cause problems in portability.
AES/Rijndael outputs bytes that appear random; you will get all values from 0x00 to 0xFF. I suspect that what is happening is that at some point the bytes are being converted to either hex characters or Base64 to allow for display. If you don't want characters then leave your output as a byte array and don't convert it.
Conversion from bytes to hex characters, or Base64, has no effect on the security of the encryption. It just allows the cyphertext to be transmitted over systems which are expecting character data.

Categories