how can i get the value of ascii character?
anybody can help me to get the value of ascii character?
i make some encryption/decryption with Caesar chiper, and i need the value of ascii and then shift it.
i make some encryption/decryption with Caesar chiper, and i need the value of ascii and then shift it.
Comments
abcdefghijklmnopqrstuvwxyz0123456789 <- original order
3456789abcdefghijklmnopqrstuvwxyz012 <- ceasar order with "key" 29 or -7
So the result would be "hkgh7cc7k u"
What you do to cipher, you simply find the index of the actual character you want to cipher in the original order. Then you add the "key" and do a modulo according to the number of characters you have in the original character-set. Then you pick the character from the position you calculated.
I'm not sure, but for finding the index the lookdown instruction could be usefull.
The advantage of using this way is that you can easily modify the character-set. For example adding -,.;: <space> and so on would not change the code.
if i want change 1 (49) become < (60)
i just write like this ?
a:= string ("1")
a:= a + 11
So a:= a+11 ' caesar shift
then (in Basic pseudo code)
if a>126 then a=a -127 ' 127 maps to 0, 128 maps to 1
and then add so space is not included
a = a+33.
and you can combine that into one line
if a>126 then a=(a-127+33)
which simplifies to
if a>126 then a=a-94
You can use a similar technique to limit the character set further, eg leave out all the numbers and {}!@#$%^$ characters
He he, you can even do a proper Caesar cypher and leave out characters that Caesar would not have used, like U and J *grin*
if i want join A and B become AB, what should i write?
a:= A
b:= B
c:= a & b ' not work...
con _CLKMODE = XTAL1 + PLL16X _XINFREQ = 5_000_000 obj term: "CogOS_IO(Term)" var byte alpha_len, key byte cipher_buffer[255] word clear_message pub cesar | i,idx,tmp ' Initialize terminal term.start( 0,0 ) ' wait until any key has been pushed tmp:=-1 repeat while tmp==-1 term.str( string( "Test Lookdown",$0d ) ) term.str( string( "== please press key to start ==",$0d ) ) tmp:=term.rxtime( 1000 ) term.tx( 0 ) ' setup alpha_len := strsize( @alphabet ) key := 11 clear_message := string( "this message should be encrypted" ) ' loop over all characters of the message repeat i from 0 to strsize( clear_message )-1 ' find the index of the actual character in the alphabet idx:=0 repeat while alphabet[ idx ] and alphabet[ idx ]<>byte[clear_message][ i ] idx++ ' write the ciphered character cipher_buffer[ i ] := alphabet[ (idx + key)//alpha_len ] cipher_buffer[ i ] := 0 term.str( clear_message ) term.tx( 13 ) term.str( @cipher_buffer ) repeat dat alphabet byte " abcdefghijklmnopqrstuvwxyz0123456789",0
CogOS_IO can easily be replaced by FullDuplexSerial and you have a working program.
This code is generic and you can add any character to the alphabet that you need, which is the advantage of checking against number ranges of valid ascii characters.
Of couse this is a quick hack and if you want to make it fool-proof you have to check the length of the message. It should not be longer than the cipher-buffer. And currently the message should not contain chracters that are not in the alphabet. But both are easy changes.
key := alpha_len-key ' loop over all characters of the message repeat i from 0 to strsize( clear_message )-1 ' find the index of the actual character in the alphabet idx:=0 repeat while alphabet[ idx ] and alphabet[ idx ]<>byte[@cipher_buffer][ i ] idx++ ' write the ciphered character cipher_buffer[ i ] := alphabet[ (idx + key)//alpha_len ] term.str( clear_message ) term.tx( 13 ) term.str( @cipher_buffer ) term.tx( 13 )
Simply add it to the end of the previous code.
if i want save the result of chiper/dechiper in a variable, it can't??
You'll need to save the result of the cipher in an array. There's a recent thread with a lot of discussion on storing strings in an array.
Duane
Hope this helps
Var long C long b long a pub start | answer answer := PackAintoBasHaRDasyoucan Pub PackAintoBasHaRDasyoucan return C := A << 8 | B
Here's how I'd join A and B (assuming A and B are characters):
CON _clkmode = xtal1 + pll16x 'Use the PLL to multiple the external clock by 16 _xinfreq = 5_000_000 '5MHz crystal (QuickStart and Propeller Protoboards) _Baud = 57600 _ArraySize = 3 VAR byte a, b, c[_ArraySize] OBJ Pst : "Parallax Serial Terminal" PUB Main Pst.start(_Baud) waitcnt(clkfreq * 2 + cnt) ' a couple of seconds to open terminal window. Pst.str(string(13, "Simple String Test", 13, 13)) a := "A" b := "B" c[0] := a c[1] := b ' c[2] remains zero, which we need, since strings should have a terminating zero. Pst.str(string("a = ")) Pst.char(a) Pst.str(string(", b = ")) Pst.char(b) Pst.str(string(13, "c = ")) Pst.str(@c)
Here's the output:
You should tell us more about your problem. Why do you want to pack several values into a single variable (long) ?
It's common that texts are stored in byte-arrays or byte-buffers - except you deal with unicode. If you want to send it via serial interface, if you want to store it to SD-card .... whatever .... usually these are byte-streams. So, what's the reason to do it different?
i get the data from x-bee, dechiper it, n store to SD card
@MagIO2, your sample codes are helpfull me. i get the idea
@Duane, that's great. i think about bytes and your code is safe me
thank you guys
can you please tell me more about that?
A shift cipher has almost no security from anybody who is even vaguely interested in getting to the information. It's good for an introduction to cryptography, but the 128 bit AES built into the XBees is much better.