开发者

need a little help "translating" a base64decoder script from PHP to Python

I have been trying to get something called simplesamlphp hooked up to a django app.

I'm almost there… alth开发者_如何学Pythonough i need to duplicate, in Python, the functionality of this php script:

I have copied the contents of the $raw variable in php, to the file 64.rtf. However when i run the Python equivalent i get an error stating:

TypeError: Incorrect padding

PHP code:

function getValue($raw) {

    $val = $raw;

    $url = parse_url($raw, PHP_URL_QUERY);
    if (!empty($url)) $val = $url;

    $arr = array();
    $query = parse_str($val, &$arr);

    #echo('<pre>');print_r($arr);

    if (array_key_exists('SAMLResponse', $arr)) return $arr['SAMLResponse'];
    if (array_key_exists('SAMLRequest', $arr)) return $arr['SAMLRequest'];
    if (array_key_exists('LogoutRequest', $arr)) return $arr['LogoutRequest'];
    if (array_key_exists('LogoutResponse', $arr)) return $arr['LogoutResponse'];

    return rawurldecode(stripslashes($val));
}

function decode($raw) {
    $message = getValue($raw);
    #echo 'using value: ' . $message; exit;

    $base64decoded = base64_decode($message);
    $gzinflated = gzinflate($base64decoded);
    if ($gzinflated != FALSE) {
        $base64decoded = $gzinflated;
    }
    $decoded = htmlspecialchars($base64decoded);
    return $decoded;
}    

I have only come up with this in Python so far:

string64 = open("64.rtf", "rU").read()
decodedstring = base64.b64decode(string64,)

What am I not getting? the rawurldecode(stripslashes bit?? or url_parser??

and what exactly does these do thats so essential to the decoding?

I hope you can help. thanks…


Here it is, in all of its glory.

#!/usr/bin/env python

import base64
import zlib
import cgi
import urlparse

def getValue(raw):
    args = urlparse.parse_qs(urlparse.urlparse(raw).query)
    keys = ['SAMLResponse', 'SAMLRequest', 'LogoutRequest', 'LogoutResponse']
    for key in keys:
        if key in args: return args[key][0]

def decode(raw):
    message = getValue(raw)
    message = message + "=" * (4 - len(message) % 4)
    base64decoded = base64.b64decode(message)
    try:
        base64decoded = zlib.decompressobj().decompress('x\x9c' + base64decoded)
    except zlib.error:
        pass # may want to handle this error
    return cgi.escape(base64decoded, True)

data = 'PHNhbWxwOlJlc3BvbnNlIHhtbG5zOnNhbWxwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiIHhtbG5zOnNhbWw9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphc3NlcnRpb24iIElEPSJwZnhkYTAxMjkzOC03MDkxLWNjZjQtZTc2Ny0wZWQ4OGVhN2Q1YmYiIFZlcnNpb249IjIuMCIgSXNzdWVJbnN0YW50PSIyMDExLTAxLTIxVDEyOjI4OjI5WiIgRGVzdGluYXRpb249Imh0dHBzOi8vd2F5Zi5teWRvbWFpbi5kay9zaW1wbGVzYW1sL3NhbWwyLW15bG9naW4ucGhwIiBJblJlc3BvbnNlVG89Il82ZDhmNDAxZDUzYTg1NDkzMzY2N2FiNWU5NzE1NWNmMzJjYWExMjBkZDciPjxzYW1sOklzc3Vlcj5odHRwczovL3Rlc3RicmlkZ2Uud2F5Zi5kazwvc2FtbDpJc3N1ZXI'

url = "http://www.google.com?SAMLResponse=" + data
print decode(url)

The reason you were getting an error when trying to b64decode your string, is because the string is not a TRUE base64 encoding. base64 encoded data is always a length that is evenly divisible by 4. Your string is not. In order to make the string length evenly divisible by 4, '=' characters are appended to the end of the string. In your case, the length % 4 == 3. So, we need to add one '=' to the end of the string to get it to decode correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜