Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DC Algorithm in JavaScript (decrypt)
#1
In response to someone's attempt to read a LF2 data file in JavaScript, I made a simple HTML page for this purpose.

I based myself on @A-Man's explanation for the DC algorithm:

DC Algorithm (Click to View)
Code:
<!DOCTYPE html>

<html>
    <head>
        <title>LF2 - Data Reader</title>
        <meta charset="UTF-8" />
        <meta name="description" content="LF2 data files reader.">
        <meta name="keywords" content="LF2, DC , data, algorithm, file, files, decypher">
        <meta name="author" content="MangaD">

        <link rel="shortcut icon" href="http://www.lf-empire.de/favicon.ico" type="image/x-icon">
        
        <style type="text/css">
            html, body {
                height:100%;
                width:100%;
                border:0;
                margin:0;
                padding: 0;
                text-align:center;
            }
            #main {
                margin-top:30px;
                text-align:initial;
                display:inline-block;
            }
            #DCode {
                width:600px;
                height:400px;
            }
        </style>

        <script type="text/javascript">

            var file;

            function fileSupport() {// Check for the various File API support.
                if (window.File && window.FileReader && window.FileList && window.Blob) {
                    // Great success! All the File APIs are supported.
                } else {
                    alert('The File APIs are not fully supported in this browser.');
                }
            }

            function openFile() {
                file = document.getElementById("fileInput").files[0];
                var fileName = escape(file.name);
                var fileType = (file.type ? file.type : 'n/a');
                var fileSize = file.size;
                var lastModified = file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() : 'n/a';
                
                if(file.name.substring(file.name.lastIndexOf(".")) != ".dat" ) {
                    alert('LF2 data file only.');
                    return false;
                }
                else {
                    fileType = "LF2/data";
                }
                document.getElementById('details').innerHTML =
                    '<strong>' + fileName + '</strong> (' + fileType + ') - ' +
                    fileSize + ' bytes, last modified: ' + lastModified;
                return true;
            }
            
            function decrypt() {
                var key = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
                var reader = new FileReader();
                
                reader.onload = function(){
                    var data = reader.result;
                    var text = "";
                    
                    for (var i = 123, j = 0; i < data.length; i++, j++)
                        text += String.fromCharCode(data.charCodeAt(i) - key.charCodeAt(j%key.length));
                    
                    document.getElementById('DCode').innerHTML = text;
                };
                
                reader.readAsBinaryString(file);
            }
        </script>
    </head>
    
    <body onload="fileSupport();">
        <div id="main">
            <label for="fileInput">Open .dat file:</label>
            <br>
            <input id="fileInput" type="file" onchange="if(openFile()) decrypt();">
            <br>
            <output id="details"></output>
            <br>
            <textarea id="DCode" wrap='off'></textarea>
        </div>
    </body>
</html>

Download file here.
[Image: random.php?pic=random]
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
The meaning of life is to give life a meaning.
Stop existing. Start living.
Reply
#2
Thanks for answering, man.

I'm not sure, but I think the problem I couldn't read the file as well before is because I'm just calling a function with a argument of string, where I use a string with the binary content of the criminal.dat, but I don't use XMLHttpRequest or any other thing to get the file content... I just paste it.

Your code works as well! You can change the line to make the loop more faster, though.

Code:
for (var i = 123, j = 0; i < data.length; i++, j++)

to

Code:
for (var i = 123, j = 0, len = data.length; i < len; i++, j++)

(Because, in JavaScript, the for numeral loop limit is always calculated, then the data string length will be calculated everytime, also.)
Reply
Thanks given by: MangaD , Rhino.Freak , kairunotabi
#3
great!

actually i have been thinking sometimes ago to make an online IDE.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)