Hi,
I am trying to get an extremely simple character string encryption function
to work using modulo 10 arithmetic.
void ccencode(UNSIGNED8* pattern) {
UNSIGNED8 key[] = CCKEY;
UNSIGNED8 digval = 0;
UNSIGNED8 count;
for (count=0; count < (strlen(pattern)-4); count++) {
digval = ((*(pattern+strlen(pattern)-count-1)-'0')+key[strlen(pattern)-count-1]+digval)%10;
*(pattern+4+count) = digval+'0';
}
} /* ccencode */
The pattern to be encrypted is a string of ASCII numeric digits. This function
starts with the last digit and adds it to the corresponding digit in the key
(the key array of integers is #defined elsewhere) and then modulo divides it
by 10 to always end up with a digit in the range 0 - 9. The digit is then
added to '0' to make an ASCII character and written to the pattern. the digit
then accumulates into the next and so on.
Two other characteristcs are that the top 4 digits of the pattern are
deliberately unencrypted and the remainder are not only encrypted, but
also reversed in sequence.
My problem is that the first 6 digits are processed correctly but the
remainder do not. The next 3 encrypted digits are 1 greater than they should be
and the remaining 3 are then way out.
Surely this cannot be a rounding effect as % is an
integer division giving an integer remainder. The algorithm works fine
on a Borland C++ compiler but not on gcc. Note that I have to run gcc
in ISO mode for other reasons.
Any ideas what is wrong ?
Thanks in advance
John Blackburn 10 2065
"john blackburn" writes: I am trying to get an extremely simple character string encryption function to work using modulo 10 arithmetic.
void ccencode(UNSIGNED8* pattern) {
UNSIGNED8 key[] = CCKEY; UNSIGNED8 digval = 0; UNSIGNED8 count;
for (count=0; count < (strlen(pattern)-4); count++) {
digval = ((*(pattern+strlen(pattern)-count-1)-'0')+key[strlen(pattern)-count-1]+digval)%10; *(pattern+4+count) = digval+'0'; }
} /* ccencode */
The pattern to be encrypted is a string of ASCII numeric digits. This function starts with the last digit and adds it to the corresponding digit in the key (the key array of integers is #defined elsewhere) and then modulo divides it by 10 to always end up with a digit in the range 0 - 9. The digit is then added to '0' to make an ASCII character and written to the pattern. the digit then accumulates into the next and so on.
Two other characteristcs are that the top 4 digits of the pattern are deliberately unencrypted and the remainder are not only encrypted, but also reversed in sequence.
My problem is that the first 6 digits are processed correctly but the remainder do not. The next 3 encrypted digits are 1 greater than they should be and the remaining 3 are then way out.
Surely this cannot be a rounding effect as % is an integer division giving an integer remainder. The algorithm works fine on a Borland C++ compiler but not on gcc. Note that I have to run gcc in ISO mode for other reasons.
Any ideas what is wrong ?
I didn't try to analyze that. But be aware that the default for char
"signage" is undefined in the standard; it can be either signed or unsigned,
it is a vendor option. So if you are using char type anywhere in your
program be sure to explicitly specify it as unsigned.
osmium wrote: I didn't try to analyze that. But be aware that the default for char "signage" is undefined in the standard; it can be either signed or unsigned, it is a vendor option. So if you are using char type anywhere in your program be sure to explicitly specify it as unsigned.
In the main header file it states :-
#define unsigned char UNSIGNED8
john blackburn <jo*********************@lintonhealy.co.uk> wrote: for (count=0; count < (strlen(pattern)-4); count++) {
digval = ((*(pattern+strlen(pattern)-count-1)-'0')+key[strlen(pattern)-count-1]+digval)%10; *(pattern+4+count) = digval+'0';
Assuming that strlen(pattern)>5, you're scribbling over your own input
buffer. When count==0, you read from pattern[strlen(pattern)-1], but you
write to pattern[4]. By the time count gets to strlen(pattern)-5, you
write to pattern[strlen(pattern)-1], but you read from pattern[4]...
which already contains its new value. The original values of the early
members of pattern never even get read. You need to use a second buffer
to hold the output while you still need the original value of pattern.
Richard
Richard Bos wrote: john blackburn <jo*********************@lintonhealy.co.uk> wrote:
for (count=0; count < (strlen(pattern)-4); count++) {
digval =
((*(pattern+strlen(pattern)-count-1)-'0')+key[strlen(pattern)-count-1]+digval)%10; *(pattern+4+count) = digval+'0';
Assuming that strlen(pattern)>5, you're scribbling over your own input buffer. When count==0, you read from pattern[strlen(pattern)-1], but you write to pattern[4]. By the time count gets to strlen(pattern)-5, you write to pattern[strlen(pattern)-1], but you read from pattern[4]... which already contains its new value. The original values of the early members of pattern never even get read. You need to use a second buffer to hold the output while you still need the original value of pattern.
Richard
God, how can I have been so stupid - thanks a lot !
john blackburn wrote: osmium wrote:
I didn't try to analyze that. But be aware that the default for char "signage" is undefined in the standard; it can be either signed or unsigned, it is a vendor option. So if you are using char type anywhere in your program be sure to explicitly specify it as unsigned.
In the main header file it states :-
#define unsigned char UNSIGNED8
Wrong way around:
#define UNSIGNED8 unsigned char
Or:
typedef unsigned char UNSIGNED8;
Your #define will replace 'unsigned' with 'char UNSIGNED8' which is probably
not what you want.
john blackburn wrote: osmium wrote:
I didn't try to analyze that. But be aware that the default for char "signage" is undefined in the standard; it can be either signed or unsigned, it is a vendor option. So if you are using char type anywhere in your program be sure to explicitly specify it as unsigned.
In the main header file it states :-
#define unsigned char UNSIGNED8
Then somebody had better repair the main header file.
Are you sure that's what it says? Perhaps you meant
#define UNSIGNED8 unsigned char
or
typedef unsigned char UNSIGNED8;
.... because if things are as you say, you can't expect
anything to work. (And if you're paraphrasing the
problematic code instead of quoting it *exactly*, you
can't expect an accurate diagnosis.)
-- Er*********@sun.com
On Tue, 19 Oct 2004 14:43:11 +0100
john blackburn <jo*********************@lintonhealy.co.uk> wrote: osmium wrote:
I didn't try to analyze that. But be aware that the default for char"signage" is undefined in the standard; it can be either signed or unsigned, it is a vendor option. So if you are using char type anywhere in your program be sure to explicitly specify it as unsigned.
In the main header file it states :-
#define unsigned char UNSIGNED8
Why use a #define rather than a typedef?
Also, you should be aware that a char may be more than 8 bits, although
I think it is mainly embedded systems where characters are larger than 8
bits.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Flash Gordon wrote: #define unsigned char UNSIGNED8
Why use a #define rather than a typedef?
My mistake it is :-
typedef unsigned char UNSIGNED8;
Eric Sosman wrote: john blackburn wrote: osmium wrote:
I didn't try to analyze that. But be aware that the default for char "signage" is undefined in the standard; it can be either signed or unsigned, it is a vendor option. So if you are using char type anywhere in your program be sure to explicitly specify it as unsigned.
In the main header file it states :-
#define unsigned char UNSIGNED8
Then somebody had better repair the main header file. Are you sure that's what it says? Perhaps you meant
#define UNSIGNED8 unsigned char
or
typedef unsigned char UNSIGNED8;
... because if things are as you say, you can't expect anything to work. (And if you're paraphrasing the problematic code instead of quoting it *exactly*, you can't expect an accurate diagnosis.)
No, you're right it is a typedef, I typed it quickly and got it wrong; the
actual encryption code, however, is an exact cut and paste.
Thanks for following up for me.
John
Thank you for everyone's replies - most helpful.
Regards
John This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Griff |
last post by:
Test program:
===============================================================
t = 5.8
interval = 2.0
while t < 6.1:
print "%s mod %s = %s " % (t, interval, t % interval )
t += 0.1...
|
by: Javier Gomez |
last post by:
I have a table with 15.000 records.
How can encrypt all information if after will shown in a form (text
box)decryted ?????
Thanks in advance
Javier Gomez
|
by: Alexander |
last post by:
I played a little bit with modulo cause I wanted to implement a small
parser for functions as I stumbled over a calculation error. First I
thought the reason might be a type conversion I did...
|
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,...
|
by: Ben Blank |
last post by:
I have a loop which iterates over an array in a particular order:
for (j = 0; j < 16; j++) T = ...;
The loop proceeds normally for j = 0 through j = 4, but gives an
IndexOutOfRangeException at...
|
by: DazedAndConfused |
last post by:
Can you encrpt a serialized object?
Or am I trying to do something that just doesn't work that way?
I am trying to encrypt a serialized object. I can read and write the object
to a file...
|
by: Chadwick Boggs |
last post by:
I need to perform modulo operations on extremely large numbers. The %
operator is giving me number out of range errors and the mod(x, y)
function simply seems to return the wrong results. Also,...
|
by: fineman |
last post by:
Hi all,
I want to get a 64bit(8 bytes) Encrypt result use DES class in the
VS2005.
Though I encrypt data is 64bit(8 bytes), but DES return encrypt result
that always is 128bit(16 bytes),
I don't...
|
by: Max Vit |
last post by:
Here is my problem: I have an application built in Access that outputs
sensitive data to a text file.
I would like to encrypt this data *whilst* the file is being
outputted.
The encryption I was...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |