473,671 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I encrypt a number from 1 to 1000 to a 4 digit hex code?

I need to create a method for a web page I'm building that encrypts a
number from 1 to 1000 into a 4 digit hex code. I know nothing about
cryptography. Can anyone steer me in the right direction?

Aug 7 '06 #1
7 4838

ffrug...@gmail. com wrote:
I need to create a method for a web page I'm building that encrypts a
number from 1 to 1000 into a 4 digit hex code. I know nothing about
cryptography. Can anyone steer me in the right direction?
WAIT: I meant into a 4 digit string that contains 0-9 a-z or A-Z. I
know, dumb mistake, (hex codes are only 0-9 a-f).

Aug 7 '06 #2
Well I think you will have quite a problem ...

you have

a-z - 26
+A-Z - 26
+0-9 - 10
---------------
62 unique chars possible in each char.

Hex gives you 16 possibilities in each char ... as such you could not
possibly encode such a string without loss ... Are you looking for a hash
algorithm (where you may have duplicate values if so string already defines
a hash code that can be used or you could use one of the hash algorithms
provided with the framework (i.e. sha1))?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<ff******@gmail .comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
>
ffrug...@gmail. com wrote:
>I need to create a method for a web page I'm building that encrypts a
number from 1 to 1000 into a 4 digit hex code. I know nothing about
cryptography . Can anyone steer me in the right direction?

WAIT: I meant into a 4 digit string that contains 0-9 a-z or A-Z. I
know, dumb mistake, (hex codes are only 0-9 a-f).

Aug 7 '06 #3
There won't be loss if you represent the string as a integer (four
bytes) and then encode that.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Greg Young" <dr************ *******@hotmail .comwrote in message
news:e6******** ******@TK2MSFTN GP02.phx.gbl...
Well I think you will have quite a problem ...

you have

a-z - 26
+A-Z - 26
+0-9 - 10
---------------
62 unique chars possible in each char.

Hex gives you 16 possibilities in each char ... as such you could not
possibly encode such a string without loss ... Are you looking for a hash
algorithm (where you may have duplicate values if so string already
defines a hash code that can be used or you could use one of the hash
algorithms provided with the framework (i.e. sha1))?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<ff******@gmail .comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
>>
ffrug...@gmail .com wrote:
>>I need to create a method for a web page I'm building that encrypts a
number from 1 to 1000 into a 4 digit hex code. I know nothing about
cryptograph y. Can anyone steer me in the right direction?

WAIT: I meant into a 4 digit string that contains 0-9 a-z or A-Z. I
know, dumb mistake, (hex codes are only 0-9 a-f).


Aug 7 '06 #4
Thank you again Greg. You're never long with a good reply. I took
some time to look up encrypting with the .net framework. I could use
something like a DES to encrypt my string (a small number salted with a
few letters) but I need the resultant encrypted string to be 4
characters long. How can I do this?

Aug 7 '06 #5
Thank you as well Nicholas. I hadn't seen your post earlier. I should
explain what I'm doing:

I want to create codes significant to me, (ex. "F14") and encrypt them
to four character strings, (ex. "R5wt") that I can then distribute to
other people. When I get these encrypted strings back I need to be
able to translate them into codes again:

R5wt = F14

That's it. It's probably not as complicated as I've made it sound.

Thank you.

Aug 7 '06 #6
I need to create a method for a web page I'm building that encrypts a
number from 1 to 1000 into a 4 digit hex code. I know nothing about
cryptography. Can anyone steer me in the right direction?
Except the case when you want a toy encryption, go with something standard,
and implemented by sombody who knows what is doing.
For instance use the Win Crypto API, or the free Crypto++ Library
(http://www.eskimo.com/~weidai/cryptlib.html)

--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Aug 7 '06 #7
On 6 Aug 2006 18:40:27 -0700, ff******@gmail. com wrote:
>I need to create a method for a web page I'm building that encrypts a
number from 1 to 1000 into a 4 digit hex code. I know nothing about
cryptography . Can anyone steer me in the right direction?
Some observations:

1 You will never get a great deal of security if you are encrypting
such a small range of numbers. All an attacker has to do is to try
each possible number and see if it works.

2 All the cyphers in .NET (Rijndael, DES, 3DES, RC2) are block cyphers
so unless you are careful your 4 digits will be padded to the size of
a block (8 bytes or 16 bytes).

3 You don't say if you are going to hard code your encryption into
your code or if you are going to be changing the key on a regular
basis. If you want a simple way to hard code things then see below.

4 If you want to change the key then my suggestion would be to use a
stream cipher, or else a block cypher in counter (CTR) mode. Given
that .NET does not seem to have an easy way to operate its cyphers in
CTR mode, then you best bet is to use RC4 which is a stream cipher.
RC4 is a bit old and not very secure, but you are never going to get a
lot of security anyway given the limited range of numbers you are
encrypting. The big advantage of RC4 is that it is absurdly easy to
program. As it is a stream cipher it will not expand your plaintext
beyond four bytes. If you do decide to use a block cipher then use
Rijndael, also known as AES, in CTR mode.

5 You should read up a little on cryptography. Wikipedia is very good
on this. Articles I suggest are:

Block cipher: http://en.wikipedia.org/wiki/Block_cipher

Stream cipher: http://en.wikipedia.org/wiki/Stream_cipher

Block cipher modes:
http://en.wikipedia.org/wiki/Block_c...s_of_operation

RC4: http://en.wikipedia.org/wiki/RC4

Rijndael/AES:
http://en.wikipedia.org/wiki/Advance...ption_Standard
6 Simple Hard Coding

This is a Vigenère cypher,
(http://en.wikipedia.org/wiki/Vigenère_cipher) which is enough to
deter casual readers, but is not in any real sense secure - a given
digit in a given position will always have the same cyphertext. Note
also that anything to do with security needs more error checking than
most other code.
// Code ----------------------------------

static string chars =
"qwe5rtyu6iopl4 kjhg7fdsa3zxcv8 bnmQ2AZXS9WEDCV 1FRTGB0NHYUJMKI OLP";
static int charLen = chars.Length;

static int ptMin = 1; // Minimum value of plaintext
static int ptMax = 1000; // Maximum value of plaintext
static int ptDigits = 4; // Number of digits in plaintext
static string dummy = "0000"; // Same length as plaintext
// Offsets is the key to the encryption,
// any four numbers in the range 1 to 61 inclusive.
static int[] offsets = { 34, 60, 18, 3 };

//----------------------------------------

static void Main() {

const int numTests = 20;

Random rand = new Random();
int pText1, pText2;
String cText;

for (int i = 0; i < numTests; ++i) {
pText1 = 1 + rand.Next() % 1000;
Console.Write(" Plaintext: {0:0000}", pText1);

cText = Encypher(pText1 );
Console.Write(" \tcyphertext: {0}", cText);

pText2 = Decypher(cText) ;
Console.Write(" \tplaintext2: {0:0000}", pText2);
if (pText1 != pText2) {
Console.WriteLi ne("\tFailed!") ;
} else {
Console.WriteLi ne("\tOK");
} // end if
} // end for

Console.Write(" Press Enter to continue... ");
Console.ReadLin e();
}

//----------------------------------------

static string Encypher(int plaintext) {
if (plaintext < ptMin || plaintext ptMax) {
throw new ArgumentExcepti on("Encypher: plaintext value
incorrect.");
} // end if
StringBuilder pText = new
StringBuilder(p laintext.ToStri ng(dummy));
StringBuilder cText = new StringBuilder(d ummy);
for (int i = 0; i < ptDigits; ++i) {
cText[i] = chars[(CharVal(pText[i]) + offsets[i]) %
chars.Length];
} // end for
return cText.ToString( );
} // end Encypher()

//----------------------------------------

static int Decypher(string cyphertext) {
if (cyphertext == null) {
throw new ArgumentNullExc eption("Decyphe r: null cyphertext.");
} // end if
if (cyphertext.Len gth != ptDigits) {
throw new ArgumentExcepti on("Decypher: cyphertext is the wrong
length.");
} // end if
StringBuilder pText = new StringBuilder(d ummy);
for (int i = 0; i < ptDigits; ++i) {
// Add extra charLen to avoid negatives
pText[i] = chars[(CharVal(cypher text[i]) - offsets[i] +
charLen) % charLen];
} // end for

int retVal = Convert.ToInt32 (pText.ToString ());
if (retVal < ptMin || retVal ptMax) {
throw new ApplicationExce ption("Decypher : return value
incorrect.");
} // end if
return retVal;
} // end Decypher()

//----------------------------------------

static int CharVal(char target) {
return chars.IndexOf(t arget);
} // end CharVal()

// End Code ------------------------------
HTH

rossum

Aug 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2938
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
37
6355
by: Protoman | last post by:
Hi!!! Protoman here, I need to write an infinite precision number class b/c I want to compute pi. Give me some sample code. Also, when I run my program and it computes pi, will my computer freeze b/c it's infinite precision? Thanks for the help!
3
1720
by: JellyON | last post by:
Do you know about an encryption script which accepts the accentued characters. I've found the one below (from WebExpert premade scripts), but when you encrypt "assurément", then reverse the process, you retrieve "assurment" : the "é" is gone ! Here is this script (maybe you have another) in an HTML page (simply copy/paste to see the result) : <html> <head>
20
4162
by: Drebin | last post by:
It's a long story really, but the bottom line is we need to encrypt or obfuscate a clear-text 9-digit SSN/taxpayer ID into something less than 21 characters. It doesn't need to be super-secure, just something that isn't plain-text and it HAS to be as unique as the original number. It also does not need to be a symmetric algorithm - we are using this as a way to create a unique "userid" on a system to which we single-signon. So it's used...
2
5185
by: Scott | last post by:
How do I trim the left digit from a number? The user will input a number with 3 digit value and I need to trim the leftmost value from the number. A typical value might be 123.4567 and I need to remove the 1 and use it's value in another calculation.
17
7692
by: rhitz1218 | last post by:
Hi, I'm trying to create a function that will sort a number's digits from highest to lowest. For example 1000 - will become 0001 or 1234 to 4321
4
4064
by: dusiapapa | last post by:
Hello, all! I'm faced with next problem. I have ASP.NET page which takes url-parameters from ColdFusion site. These parameters are encrypted with ColdFusion Encrypt function and I can not decrypt them correctly. I tried to translate several code examples (in java and C++) I had found on the Internet into C# but they don't work properly.
2
27333
by: hikmaz | last post by:
I am trying to get the rightmost digits (%10) of a number (taken from the user) and store it into successive array locations and get rid of the rightmost digit (\10) to store the next and so on and so forth. For example, if the number entered by the user is 4321 then, int array = 1 int array = 2 int array = 3 int array = 4 int array = '\0' (End of array - when number == 0) This is the piece of code i...
1
3605
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent via an email function. But just can't figure it out. <? //////////////////////////////////////////////////////////// //
0
8483
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8824
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8603
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6236
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2818
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.