Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Usage:
- Para SHA-256 (irreversível):
- echo encrypt("string a ser criptografada");
- Para codificar RC4 (reversível):
- echo encode('hello world', 'senha');
- Para decodificar (a senha precisa ser a mesma utilizada para criptografar):
- echo encode('a7a8a2v2a1', 'senha'); // escrevi aleatoriamente essa hash aí
- */
- function encrypt($str) { // one way
- return hash('sha256', $str);
- }
- function encode($str, $key = null) { // two way
- return str2hex(_rc4($str, $key));
- }
- function decode($str, $key = null) {
- return _rc4(hex2str($str), $key);
- }
- function hex2str( $hex ) {
- return pack('H*', $hex);
- }
- function str2hex( $str ) {
- return array_shift( unpack('H*', $str) );
- }
- function _rc4($str, $key=null) { // internal
- $s = array();
- for ($i = 0; $i < 256; $i++) {
- $s[$i] = $i;
- }
- $j = 0;
- for ($i = 0; $i < 256; $i++) {
- $j = ($j + $s[$i] + ord($key[$i % strlen($key)])) % 256;
- $x = $s[$i];
- $s[$i] = $s[$j];
- $s[$j] = $x;
- }
- $i = 0;
- $j = 0;
- $res = '';
- for ($y = 0; $y < strlen($str); $y++) {
- $i = ($i + 1) % 256;
- $j = ($j + $s[$i]) % 256;
- $x = $s[$i];
- $s[$i] = $s[$j];
- $s[$j] = $x;
- $res .= $str[$y] ^ chr($s[($s[$i] + $s[$j]) % 256]);
- }
- return $res; // RC4
- }
Add Comment
Please, Sign In to add comment