Anonymous avatar Anonymous committed 01d7296

fix ciphersaber 2 behavior, add a little error checking to rc4.bash

Comments (0)

Files changed (2)

crypt/ciphersaber.bash

 # 
 
 cipher_saber() {
-	local c i j k s t x LC_CTYPE=C
-
-	if (($# == 2)); then
-		((n = $2))
-	else
-		((n = 1))
-	fi
+	local c i j k n s t x LC_CTYPE=C
 
 	while IFS= read -r -d '' -n 1 c; do
 		((${#k[@]} == ${#1})) && break
 		((s[i] = i))
 	done
 
-	while ((n--)); do
-		for ((j = i = 0; i < 256; i++)); do
+	for ((j = 0, n = ($# > 1) ? $2 : 1; n; n--)); do
+		for ((i = 0; i < 256; i++)); do
 			((j += s[i] + k[i % ${#k[@]}], j &= 0xff,
 			  t = s[i], s[i] = s[j], s[j] = t))
 		done
 		for str; do
 			printf %s "$str" |
 			while IFS= read -r -d '' -n 2 x; do
+				[[ $x == [[:xdigit:]][[:xdigit:]] ]] || { printf "Bad hex byte <%s>(0x%02x)\n" "$x" "'$x" 1>&2; return 1; }
 				printf %b "\x$x"
 			done
 		done
 	else
 		while IFS= read -r -d '' -n 2 x; do
+			[[ $x == [[:xdigit:]][[:xdigit:]] ]] || { printf "Bad hex byte <%s>(0x%02x)\n" "$x" "'$x" 1>&2; return 1; }
 			printf %b "\x$x"
 		done
 	fi
 	local c i j k s t x LC_CTYPE=C
 
 	while IFS= read -r -d '' -n 2 x; do
-		[[ $x == [[:xdigit:]][[:xdigit:]] ]] || break
+		((${#k[@]} == ${#1})) && break
+		[[ $x == [[:xdigit:]][[:xdigit:]] ]] || { printf "Bad hex byte <%s>(0x%02x)\n" "$x" "'$x" 1>&2; return 1; }
 		k+=( $((0x$x)) )
 	done <<< "$1"
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.