Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
marcucio
Posts: 41
Registered: ‎01-14-2011
My Device: Not Specified

browser bitwise bug?

I am using a javascript Tea decryption function that works great on iPad, desktop, and my Palm Pre. When I try to use it in the Playbook browser I get different results. The following is the code in question:

 

 

Tea.decrypt = function(ciphertext, password) {
    if (ciphertext.length == 0) return('');
    var v = Tea.strToLongs(Base64.decode(ciphertext));
    var k = Tea.strToLongs(Utf8.encode(password).slice(0,16)); 
    var n = v.length;
    
    // ---- <TEA decoding> ---- 
    
    var z = v[n-1], y = v[0], delta = 0x9E3779B9;
    var mx, e, q = Math.floor(6 + 52/n), sum = q*delta;
    while (sum != 0) {
        e = sum>>>2 & 3;
        for (var p = n-1; p >= 0; p--) {
            z = v[p>0 ? p-1 : n-1];
            mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z);
            y = v[p] -= mx;
        }
        sum -= delta;
    }
    
    // ---- </TEA> ---- 
    
    var plaintext = Tea.longsToStr(v);
    // strip trailing null chars resulting from filling 4-char blocks:
    plaintext = plaintext.replace(/\0+$/,'');
    return Utf8.decode(plaintext);
}
It's the while loop that gives different results on the Playbook. I am not an expert on bitwise operators but does anyone out there know if this should act the same on all webbrowsers?
Any help would be appreciated, thanks

 

 

Mike
Get It Done Tasks
http://getitdoneapp.com
Please use plain text.
Contributor
marcucio
Posts: 41
Registered: ‎01-14-2011
My Device: Not Specified

Re: browser bitwise bug?

no takers on this? I will try to write a small app which demestrats how this acts differently on the playbook than other web browsers.

Mike
Get It Done Tasks
http://getitdoneapp.com
Please use plain text.
Developer
TheMarco
Posts: 669
Registered: ‎02-19-2011
My Device: BlackBerry PlayBook 32GB

Re: browser bitwise bug?

If you have a good showcase demo of the issue I'd file a bug in the issue tracker. That tends to get their attention pretty well. A lot better than posting here in my experience.

Staff UI Prototyper (read: full-time hacker)


My BB10 apps: Screamager | Scientific RPN Calculator | The Last Weather App

Please use plain text.
Contributor
marcucio
Posts: 41
Registered: ‎01-14-2011
My Device: Not Specified

Re: browser bitwise bug?

I submitted a bug, FYI here is the test file i submitted:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function longsToStr(l) {  // convert array of longs back to string
    var a = new Array(l.length);
    for (var i=0; i<l.length; i++) {
        a[i] = String.fromCharCode(l[i] & 0xFF, l[i]>>>8 & 0xFF, 
                                   l[i]>>>16 & 0xFF, l[i]>>>24 & 0xFF);
    }
    return a.join('');  // use Array.join() rather than repeated string appends for efficiency in IE
}
function decrypt(ciphertext, password) {
    if (ciphertext.length == 0) return('');
    var v = [-72000622,-707140671,1821327308]//Tea.strToLongs(Base64.decode(ciphertext));
    var k = [862088557, 825844835, 892822581, 942682168];//Tea.strToLongs(Utf8.encode(password).slice(0,16)); 
    var n = v.length;
    
    // ---- <TEA decoding> ---- 
    
    var z = v[n-1], y = v[0], delta = 0x9E3779B9;
    var mx, e, q = Math.floor(6 + 52/n), sum = q*delta;
    while (sum != 0) {
        e = sum>>>2 & 3;
        for (var p = n-1; p >= 0; p--) {
            z = v[p>0 ? p-1 : n-1];
            mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z);
            y = v[p] -= mx;
        }
        sum -= delta;
    }
    
    // ---- </TEA> ---- 
    
    var plaintext = longsToStr(v);
    // strip trailing null chars resulting from filling 4-char blocks:
    plaintext = plaintext.replace(/\0+$/,'');
    return plaintext;//Utf8.decode(plaintext);
}
</script>
</head>
<body>
<p>Expected result: test record</p>
<p>Actual result: <span id="result"></span></p>
</body>
<script type="text/javascript">
document.getElementById('result').innerHTML = decrypt("klu1+8Hj2dXMP49s", "mmb3cd915d758008bd19d0f2428fbb354a");
</script>
</html>
Mike
Get It Done Tasks
http://getitdoneapp.com
Please use plain text.