RC4 encryption in tcl 7.6, StoryServer 4
MemeRot writes | more than 12 years ago
[
##
## INPUTS: plainText
##
## RETURNS: cipherText
##
## NOTES: Conversion to Hex values may not
## have been necessary, but was much easier to debug
## See references at and
##
##
## This has been tested and found to match RC4, using
## the perl module, also the public examples.
## This was written for StoryServer 4.2, which is
## based on tcl 7.6 (i.e. no binary stream, namespaces)
##
## Tcl'ers, some of the syntax is slightly different,
## not very though.
##
## cypherText is created as a variable with the
## encrypted string, not returned as the return value
## from this function.
proc RC4_encrypt {plainText} {
## Must set these globally - but its global
## TO THIS PROCEDURE only
global buffer S i enewsKey hexKeyList keyList enewsHex K f c HexVal plainList ccList l resultSet cipherText counter cardLen base64String
for {set loop 0} {$loop <= 255} {incr loop} {
append buffer " $loop "
}
## Create a State array consisting of the numbers
## 0 through 255
FOREACH num IN $buffer {
[array set S [list $num $num]
NULL]
}
## The cipher key. Guard it.
## Put your own key in here, this will resolve to
## slashtroll
set cipherKey [DECODE64 "c2xhc2h0cm9sbA=="]
## Its much easier to deal with the algorithm in
## hex and decimal than strings, so do that. This
## creates a list of the hex values.
for {set l 0} {$l < [string length [SHOW cipherKey]]} {incr l} {
scan [string index [SHOW cipherKey] [SHOW l]] %c keyInts
lappend hexKeyList [format %X $keyInts]
}
## destroy the key so that if there is a stack dump
## it is not compromised.
unset cipherKey
## Make the key decimal, expand it to fill 256
## places in an array
FOREACH HexVal IN [SHOW hexKeyList] {
[scan $HexVal %x KeyStr]
[lappend keyList $KeyStr]
}
set i 0
for {set loop 1} {$loop <= 32} {incr loop} {
for {set doubleloop 0} {$doubleloop <= 7} {incr doubleloop} {
array set cypherHex [list $i [lindex $keyList $doubleloop]]
incr i
}
}
FOREACH num IN $buffer {
[array set K [list $num $cypherHex($num)]
NULL]
}
## Delete more key variables in case of stack dump.
unset hexKeyList keyList cypherHex
## Use the key array to mix the state array into an
## encrypting variable array
set i 0
set f 0
FOREACH i IN $buffer {
[SET f [expr ($f + $S($i) + $K($i)) % 256]]
[SET tempVal $S($i)]
[array set S [list $i $S($f)]
NULL]
[array set S [list $f $tempVal]
NULL]
}
## Get rid of the Key array. Now it should not be
## feasable for a user to crash the system with
## invalid plaintext and derive the key from any
## existing variables.
unset K
## Create a hex, then decimal list of the plaintext
set i 0
set f 0
set c 0
for {set l 0} {$l < [string length $plainText]} {incr l} {
lappend ccList [string index $plainText $l]
}
FOREACH HexVal IN [SHOW ccList] {
[scan $HexVal %c plainStr]
[lappend plainList $plainStr]
}
## Encrypt the plaintext by deriving a number from
## the encrypting variable array to use and
## XOR'ing it with the value of the plaintext
FOREACH num IN $plainList {
[SET i [expr ($i + 1) % 256]]
[SET f [expr ($f + $S($i)) % 256]]
[SET tempVal $S($i)]
[array set S [list $i $S($f)]
NULL]
[array set S [list $f $tempVal]
NULL]
[SET t [expr ($S($i) + $S($f)) % 256]]
[SET encrByte $S($t)]
[array set resultSet [list $c [expr $num ^ $encrByte]]
incr c
NULL]
}
SET base64String "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
set counter 0
set cardLen [array size resultSet]
FOREACH num IN {1 2 3 4 5 6} {
[IF {[SHOW counter] != [SHOW cardLen]} {
[append cipherText [string index $base64String [expr [SHOW resultSet([SHOW counter])] / 4]]]
[incr counter; NULL]
[IF {[SHOW counter] != [SHOW cardLen]} {
[append cipherText [string index $base64String [expr ([SHOW resultSet([expr [SHOW counter] - 1])] % 4) * 16 + ([SHOW resultSet([SHOW counter])] / 16)]]]
[incr counter; NULL]
[IF {[SHOW counter] != [SHOW cardLen]} {
[append cipherText [string index $base64String [expr ([SHOW resultSet([expr [SHOW counter] - 1])] % 16) * 4 + ([SHOW resultSet([SHOW counter])] / 64)]]]
[append cipherText [string index $base64String [expr [SHOW resultSet([SHOW counter])] % 64]]]
[incr counter; NULL]
} ELSE {
[append cipherText [string index $base64String [expr ([SHOW resultSet([expr [SHOW counter] - 1])] % 16) * 4]]]
[append cipherText "="]
}]
} ELSE {
[append cipherText [string index $base64String [expr ([SHOW resultSet([expr [SHOW counter] - 1])] % 4) * 16]]]
[append cipherText "=="]
}]
}]
}
unset buffer S i f c HexVal plainList ccList l resultSet counter cardLen base64String
return
}]
Visual Studio vs. Eclipse: a Programmer's Comparison
Intellisense *almost* works flawlessly. The times when it doesn't show properties that I know exist drive me bananas.
The unit testing changes from VS 2010 to VS 2012 were quite confusing and a big step down in usability. Wasn't even at an ok level until update 2.
Visual Studio vs. Eclipse: a Programmer's Comparison
Depends on your context a bit. When dealing with automatically binding form fields to a view model like with an MVC
.net project the convenience and speed pay off handsomely, even at the cost of having to have your form fields and properties identically named.
Visual Studio vs. Eclipse: a Programmer's Comparison
The syntactic overhead in C# is at least lighter than Java/C++. It's just Foo.Bar == 'something' or Foo.Bar = 'thing', you just have to declare Bar with a {get; set; }
Visual Studio vs. Eclipse: a Programmer's Comparison
No good reason I shouldn't be able to shoot laser beams out of my eyes either. But I can neither change how an attribute is accessed in Java/.Net nor shoot laser beams out of my eyes.
Visual Studio vs. Eclipse: a Programmer's Comparison
About the only thing a getter and setter offers is an advantage of doing something before / after accessing the variable for a read / write operation... which breaks the objected oriented mentality.
How does it do that? A form might pass over [date] [time] [timezone] and my object might have [utc_datetime]. The setters for date, time, and timezone set private variables that i don't care about individually. And my getter for utc_datetime will stick date and time together, then look at the timezone and the date and figure out what amount of hours to add or subtract, and return the utc_datetime. But only in cases where the calling code actually ends up requesting it. Otherwise that calculation can be skipped.
Visual Studio vs. Eclipse: a Programmer's Comparison
There is a product by a company called Xamarin that lets you write C# and compile it for many platforms - iOS, Android, Mac, and Windows. You can either develop in their IDE, or treat their product as a plugin to Studio.
There's also Mono. The ServiceStack web services framework is completely portable between windows and mono on linux.
Or you can use PyTools () to write python.
It's still predominantly used for Windows. But the other opportunities are much more than they used to be.
Visual Studio vs. Eclipse: a Programmer's Comparison object you've saved a ton of database queries. The the calling code just uses CompanyConfig.AllowSomeOption freely.
That requires you to actually code that of course. The automatic get/set is just giving you the opportunity to do stuff like that. Once your class has exposed a public int MyField; you really can't come back later and change that. But exposing a public int MyField {get; set;} lets you come back and fill in the get/set logic when needed
Visual Studio vs. Eclipse: a Programmer's Comparison
The automated get/set provides the ability to come back later and add your own custom get/set functions. Knowing that 90% of the time you never will. But if you do come back and do it, you have to do the whole shebang - declare your private variable and have the get and set operate on that variable. I believe the need for an explicit common private variable to use in both of them is why you can't leave one as automatic
Visual Studio vs. Eclipse: a Programmer's Comparison
;}
Ask Slashdot: Geek-Centric Magazines Still Published On Paper?
Yes. I have a set of very clever glasses with strips of duct tape over them that block out all the ads while I read magazines.
Ask Slashdot: Geek-Centric Magazines Still Published On Paper?
I prefer to keep my electronics out of the bathroom though, where all of my quality magazine reading happens.
Plate Readers Abound in DC Area, With Little Regard For Privacy
Are you kidding? It takes a screwdriver and 5 minutes. Pull up next to an innocent car. Swap it's plates with the plates on your stolen car. Now the innocent car sets off the camera warnings. Repeat daily.
Plate Readers Abound in DC Area, With Little Regard For Privacy
You have to ask yourself if you really want 100% automated, ubiquitous enforcement of every law in existence. How many laws apply to you right now? You really have no way of knowing.
Plate Readers Abound in DC Area, With Little Regard For Privacy
I've had my plates stolen before. If someone was tracking the criminal who stole my plates and thought they were tracking me, that would be bad for me.
Plate Readers Abound in DC Area, With Little Regard For Privacy
There is a Constitutional right to privacy that extends beyond the 4th Amendment. If it covers having an abortion, wouldn't it cover something that is actually your right to privacy?
Plate Readers Abound in DC Area, With Little Regard For Privacy
I will bet that the information in these database is used to obtain search warrants, and used in criminal prosecutions. And I will bet that the security measures on these insular, home grown database systems is woefully insufficient to ensure that it is real data, and not someone just editing the database to get a bogus warrant.
Why Are CC Numbers Still So Easy To Find?
It is easy for someone who works on a cash register to turn a fraudulent card into cash. They just need to ring up a bogus charge, put that receipt in the cash register, and take out the corresponding amount of cash. How are you going to prove what happened?