Commits

Evan Gates  committed 01d7296

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

  • Participants
  • Parent commits 6672003

Comments (0)

Files changed (2)

File 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

File crypt/rc4.bash

 		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"