SHOW:
|
|
- or go back to the newest paste.
| 1 | Number.prototype.mod = function(n) {
| |
| 2 | return ((this%n)+n)%n; | |
| 3 | } | |
| 4 | ||
| 5 | - | function rotXMod(str, startIdx, addEachRot) {
|
| 5 | + | function rotplus(str, startIdx, addEachRot) {
|
| 6 | var result = ''; | |
| 7 | for (var i = 0; i < str.length; ++i) {
| |
| 8 | var rot = (startIdx + i * addEachRot); | |
| 9 | var c = str.charCodeAt(i) - 33 + rot; | |
| 10 | result += String.fromCharCode(33 + (c.mod(90))); | |
| 11 | } | |
| 12 | return result; | |
| 13 | } | |
| 14 | ||
| 15 | function encode(str, b64Count, rotStart, addEachRot) {
| |
| 16 | result = str; | |
| 17 | for (var i = 0; i < b64Count; ++i) {
| |
| 18 | result = btoa(result); | |
| 19 | } | |
| 20 | ||
| 21 | - | return rotXMod(result, rotStart, addEachRot); |
| 21 | + | return rotplus(result, rotStart, addEachRot); |
| 22 | } | |
| 23 | ||
| 24 | function decode(str, b64Count, rotStart, addEachRot) {
| |
| 25 | - | result = undoRotXMod(str, -rotStart, -addEachRot); |
| 25 | + | result = rotplus(str, -rotStart, -addEachRot); // to undo rotplus, just pass parameters as negative |
| 26 | for (var i = 0; i < b64Count; ++i) {
| |
| 27 | result = atob(result); | |
| 28 | } | |
| 29 | return result; | |
| 30 | } | |
| 31 | ||
| 32 | // The following code is a way you can brute-force any parameters. | |
| 33 | // The use of base64 as the final decoding step means _most_ parameters are | |
| 34 | // obviously invalid as they don't result in a valid base64 string. | |
| 35 | ||
| 36 | // additionally, due to the use of modulus there is a set amount of values | |
| 37 | // that can be used for rotStart and addEachRot so brute-forcing is a | |
| 38 | // pretty reasonable thing to do | |
| 39 | ||
| 40 | for (var b64Count = 0; b64Count < 30; ++b64Count) {
| |
| 41 | for (var rs = 0; rs < 90; ++rs) {
| |
| 42 | for (var aer = 0; aer < 90; ++aer) {
| |
| 43 | try {
| |
| 44 | console.log(decode(str, b64Count, rs, aer) + " (used params " + b64Count + "," + rs + "," + aer + ")"); // replace str with whatever you're trying to decode | |
| 45 | } | |
| 46 | catch (e) {
| |
| 47 | // if an error is thrown (from atob(), which is the base64 decoder) then | |
| 48 | // we know this solution is not valid. | |
| 49 | } | |
| 50 | } | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | // after running the above, you should get a bunch of text in your console. It should | |
| 55 | // be fairly obvious which bit of text is the correct answer |