5kyy8lu3 Posted September 3, 2009 Share Posted September 3, 2009 Hi. I wrote a simple encryption script, for fun, and I can't get it to work. I make an array with 1-255, then shuffle it with shuffle() then make a new array with keys 1 through 255 and give the values of the shuffled array to the new one, so each ascii code has a new code it "converts" to, so it's a pseudo "encryption table" kinda deal. I then go one character at a time with substr and convert each character using that array. When I decrypt it, I get this weird square FFFD character that's randomly taking the place of some character. Since the encryption is random, each time i refresh the page it outputs FFFD character in place of a different string. So my question is, is this causes by a string that isn't "compatible" with being converted to something and then back? I could skip "bad" characters but I don't even know what's causing this so it's hard to find a solution. Here's what I get as a result when I encrypt, then decrypt "What is g�oing� on with this script???" What is �oin� on with this script??? What �s g��ng �n w�th th�s scr��t??? Wh�� is �oin� on wi�h �his s��ip�??? What�i��going�on�with�thi���cript��� What �s �o�n� on w�th th�s scr�pt??? W�at i� going on �it� t�i� �cri�t��� The missing characters are those FFFD characters but they don't seem to copy/paste. Any ideas? Here's the code: <?php echo '<pre>'; function encrypt($Incoming) { $Codes = array("32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126"); shuffle($Codes); $count = 0; for ( $i = 32; $i < 127; $i++ ) { $CryptTable[$i] = $Codes[$count]; $count++; } for ( $i = 0; $i < strlen($Incoming); $i++) { $NewString = $NewString . chr($CryptTable[ord(substr($Incoming, $i, 1))]); } for ( $i = 32; $i < 127; $i++ ) { if ( $CryptTable[$i] < 100 ) { $CryptTable[$i] = 'x' . $CryptTable[$i]; } $Key = $Key . str_replace('0', 'x', $CryptTable[$i]); } $Keyed = substr($Key, 0, 144) . $NewString . substr($Key, -141); return($Keyed); } function decrypt($Incoming) { $Key = substr($Incoming, 0, 144) . substr($Incoming, -141); $string = substr($Incoming, 144, ( strlen($Incoming) - 285 )); $count = 0; for ( $i = 32; $i < 127; $i++ ) { $DeCryptTable[str_replace('x', '', substr($Key, $count, 3))] = $i; $count = $count + 3; } for ( $i = 0; $i < strlen($string); $i++ ) { $NewString = $NewString . chr($DeCryptTable[ord(substr($string, $i, 1))]); } return($NewString); } echo decrypt(encrypt("What is going on with this script???")); ?> Thanks =) Quote Link to comment https://forums.phpfreaks.com/topic/172920-solved-fffd-ascii-character/ Share on other sites More sharing options...
5kyy8lu3 Posted September 3, 2009 Author Share Posted September 3, 2009 So, I limited the range of characters I convert from chr(33) to chr(126) to make sure I don't have any special characters screwing this up. It still does the same thing. I tried a for loop echo'ing chr(32) through chr(126) and I don't see that FFFD character anywhere, so in theory it shouldn't in my decryption output either but it still does =/ any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/172920-solved-fffd-ascii-character/#findComment-911350 Share on other sites More sharing options...
Zane Posted September 3, 2009 Share Posted September 3, 2009 I'm sorry but you're gonna have to elaborate a little more on this FFFD character. I'm sure I already know which one it is ... it's probably on the tip of my tongue..or I've forgotten or something..can you please tell us what this character is exactly...just for clarification if anything else. Quote Link to comment https://forums.phpfreaks.com/topic/172920-solved-fffd-ascii-character/#findComment-911357 Share on other sites More sharing options...
5kyy8lu3 Posted September 3, 2009 Author Share Posted September 3, 2009 I'm sorry but you're gonna have to elaborate a little more on this FFFD character. I'm sure I already know which one it is ... it's probably on the tip of my tongue..or I've forgotten or something..can you please tell us what this character is exactly...just for clarification if anything else. this: Quote Link to comment https://forums.phpfreaks.com/topic/172920-solved-fffd-ascii-character/#findComment-911362 Share on other sites More sharing options...
Zane Posted September 3, 2009 Share Posted September 3, 2009 so it's showing all 65,025 of them except that one? 65,024 ascii characters In other news...this is useful ..perhaps http://forums.whirlpool.net.au/forum-replies-archive.cfm/1178058.html Quote Link to comment https://forums.phpfreaks.com/topic/172920-solved-fffd-ascii-character/#findComment-911371 Share on other sites More sharing options...
5kyy8lu3 Posted September 3, 2009 Author Share Posted September 3, 2009 so it's showing all 65,025 of them except that one? 65,024 ascii characters i'm only playing with 32 through 126. example: <?php echo '<pre>'; for ( $i = 33; $i < 127; $i++ ) { echo $i . ' ' . chr($i) . '<br>'; } ?> which works fine. but when I decrypt my encrypted text, some characters show up as that "fffd" character, which google searching tells me means it's trying to display an invalid character, which is B.S. because I did a print_r() on my decryption table and all the ascii codes are within the 33-126, which ALL work fine using that above script. i'm stumped =*( Quote Link to comment https://forums.phpfreaks.com/topic/172920-solved-fffd-ascii-character/#findComment-911375 Share on other sites More sharing options...
5kyy8lu3 Posted September 3, 2009 Author Share Posted September 3, 2009 Ok I think I figured it out. Not sure what I did exactly but I made things more "definite" and it works this time around. here's the code incase anyone is curious of how my super primitive encryption works lol: <?php echo '<pre>'; function encrypt($Incoming) { for ( $i = 33; $i < 255; $i++ ) { $Codes[] = $i; } shuffle($Codes); $count = 0; for ( $i = 33; $i < 255; $i++ ) { $CryptTable[$i] = $Codes[$count]; $count++; } for ( $i = 0; $i < strlen($Incoming); $i++) { $Byte = substr($Incoming, $i, 1); $AsciiCode = ord($Byte); if ( ( $AsciiCode > 32 ) && ( $AsciiCode < 255 ) ) { $NewCode = $CryptTable[$AsciiCode]; $NewString = $NewString . chr($NewCode); } else { $NewString = $NewString . $Byte; } } for ( $i = 33; $i < 255; $i++ ) { if ( $CryptTable[$i] < 100 ) { $CryptTable[$i] = 'x' . $CryptTable[$i]; } $Key = $Key . $CryptTable[$i]; } $Keyed = substr($Key, 0, 333) . $NewString . substr($Key, 333); return($Keyed); } function decrypt($Incoming) { $Key = substr($Incoming, 0, 333) . substr($Incoming, -333); $string = substr($Incoming, 333, ( strlen($Incoming) - 666 )); $count = 0; for ( $i = 33; $i < 255; $i++ ) { $DeCryptTable[str_replace('x', '', substr($Key, $count, 3))] = $i; $count = $count + 3; } for ( $i = 0; $i < strlen($string); $i++ ) { $Byte = substr($string, $i, 1); $AsciiCode = ord($Byte); if ( ( $AsciiCode > 32 ) && ( $AsciiCode < 255 ) ) { $NewCode = $DeCryptTable[$AsciiCode]; $NewString = $NewString . chr($NewCode); } else { $NewString = $NewString . $Byte; } } return($NewString); } $a = encrypt("What is going on with this script???"); echo 'Encrypted: ' . $a . '<br><br>'; echo 'Decrypted: ' . decrypt($a); ?> Quote Link to comment https://forums.phpfreaks.com/topic/172920-solved-fffd-ascii-character/#findComment-911396 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.