简体   繁体   中英

custom mapping table for base64

i need to use base 64 to encrypt some data. but while everyone can decode it, does it make sense to encode it?

so i need to use custom maping table to encode insted of "ABCD......789+/"

i found a function at php.net - http://www.php.net/manual/en/function.base64-encode.php#78765

it can do what i need but i don't know how to decode the encrypted data.

base64 encryption really isn't for security. You want to use mcrypt or similar for that. base64 is specifically for transferring data in a way that is safe and can be understood by multiple interested parties.

But, here is how you'd go about undoing that poster's method:

If you look at his comments, he's switched 's' and 9 (actually, his array has two 'S', but I think the second was a typo). So this should work:

// there is a more efficient way of doing this, but this was easy to demonstrate.
// you'll need to use temp stand-ins 
$res = str_replace( array( 9, 's' ), array( '{', '}' ), $input ); 
$res = str_replace( array( '{', '}' ), array( 's', 9 ), $res ); 
$base64_raw = '';

for( $i = 0; $i < strlen( $res ); $i++ )
{
    $tmp = $res[ $i ];
    // if it is upper case, then append the lower-case version.
    if( $tmp == strtoupper( $tmp ) ) $base64_raw .= strtolower( $tmp );
    // else append the upper-case version.
    else $base64_raw .= strtoupper( $tmp );
}

echo base64_decode( $base64_raw );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM