Incorrect decode Base_64

Posted on
  • When use function atob ('base64_text') then result not matched to binary data. For example first bytes of TLS key is 0x30, 0x82, 0x03... is "MIID...", but Espruino decode this string start from 0x00.

  • Could you provide an actual example please?

  • I see that problem is some symbols of line control that must be ignored by 'atob', but they decoded as 0x00 so that final decode is inccorrect.


    1 Attachment

    • error3.png
  • Looks like this one : Base64 decoding (atob) of padded strings yields trailing \x00

    Tested on

    VERSION: "2v05.19",
    GIT_COMMIT: "084dd0b6f",
    BOARD: "PICO_R1_3",

    var x = "MIIBIzCBzgIJAKHw8axCL//dMA0GCSqGSIb3DQEBCwUAMBkxFzAVBgNVBAMMDnRlc3Quc2JpdC0uaW50MB4XTIwMDQyNzEyMDcxNVoXDTIxMDQyNzEyMDcxNVowGTEXMBUGA1UEAwwOdGVzdC5zYml0LS5pbnQwXDANBgkqhkiG9w0BAQEFAANLADBIAkEA+OyavawKgLML4gw/ZNGu5HAo1SVDgB/FTtI2T1LOY2stw/pRqHQAi7MTF+qnLnLshop94sUf7Q99/0RqMpUWzwIDAQABMA0GCSqGSIb3DQEBCwUAA0EA0vO5szrdZycoMUUQOGscabo9lu5RZgDIUodmKUF096WF1021zParwAbeFoGLI9N6n9vWC/2ul5Zk/IpNWV13pg==";
    
    
    function hexdump(buffer, blockSize) {
        blockSize = blockSize || 16;
        var block, addr, codes, code;
        var lines = [];
        var hex = "0123456789ABCDEF";
        for (var b = 0; b < buffer.length; b += blockSize) {
            block = buffer.slice(b, Math.min(b + blockSize, buffer.length));
            addr = ("0000" + b.toString(16)).slice(-4);
            codes = block.split('').map(function (ch) {
                code = ch.charCodeAt(0);
                return " " + hex[(0xF0 & code) >> 4] + hex[0x0F & code];
            }).join("");
            codes += "   ".repeat(blockSize - block.length);
            var chars = block.replace(/[\x00-\x1F\x20]/g, '.');
            chars +=  " ".repeat(blockSize - block.length);
            lines.push(addr + " " + codes + "  " + chars);
        }
        return lines.join("\n");
    }
    
    console.log("\n"+hexdump(x));
    console.log("\n"+hexdump(atob(x)));
    console.log("\n"+hexdump(btoa(atob(x))));
    
    

    output

    0000  4D 49 49 42 49 7A 43 42 7A 67 49 4A 41 4B 48 77  MIIBIzCBzgIJAKHw
    0010  38 61 78 43 4C 2F 2F 64 4D 41 30 47 43 53 71 47  8axCL//dMA0GCSqG
    0020  53 49 62 33 44 51 45 42 43 77 55 41 4D 42 6B 78  SIb3DQEBCwUAMBkx
    0030  46 7A 41 56 42 67 4E 56 42 41 4D 4D 44 6E 52 6C  FzAVBgNVBAMMDnRl
    0040  63 33 51 75 63 32 4A 70 64 43 30 75 61 57 35 30  c3Quc2JpdC0uaW50
    0050  4D 42 34 58 54 49 77 4D 44 51 79 4E 7A 45 79 4D  MB4XTIwMDQyNzEyM
    0060  44 63 78 4E 56 6F 58 44 54 49 78 4D 44 51 79 4E  DcxNVoXDTIxMDQyN
    0070  7A 45 79 4D 44 63 78 4E 56 6F 77 47 54 45 58 4D  zEyMDcxNVowGTEXM
    0080  42 55 47 41 31 55 45 41 77 77 4F 64 47 56 7A 64  BUGA1UEAwwOdGVzd
    0090  43 35 7A 59 6D 6C 30 4C 53 35 70 62 6E 51 77 58  C5zYml0LS5pbnQwX
    00a0  44 41 4E 42 67 6B 71 68 6B 69 47 39 77 30 42 41  DANBgkqhkiG9w0BA
    00b0  51 45 46 41 41 4E 4C 41 44 42 49 41 6B 45 41 2B  QEFAANLADBIAkEA+
    00c0  4F 79 61 76 61 77 4B 67 4C 4D 4C 34 67 77 2F 5A  OyavawKgLML4gw/Z
    00d0  4E 47 75 35 48 41 6F 31 53 56 44 67 42 2F 46 54  NGu5HAo1SVDgB/FT
    00e0  74 49 32 54 31 4C 4F 59 32 73 74 77 2F 70 52 71  tI2T1LOY2stw/pRq
    00f0  48 51 41 69 37 4D 54 46 2B 71 6E 4C 6E 4C 73 68  HQAi7MTF+qnLnLsh
    0100  6F 70 39 34 73 55 66 37 51 39 39 2F 30 52 71 4D  op94sUf7Q99/0RqM
    0110  70 55 57 7A 77 49 44 41 51 41 42 4D 41 30 47 43  pUWzwIDAQABMA0GC
    0120  53 71 47 53 49 62 33 44 51 45 42 43 77 55 41 41  SqGSIb3DQEBCwUAA
    0130  30 45 41 30 76 4F 35 73 7A 72 64 5A 79 63 6F 4D  0EA0vO5szrdZycoM
    0140  55 55 51 4F 47 73 63 61 62 6F 39 6C 75 35 52 5A  UUQOGscabo9lu5RZ
    0150  67 44 49 55 6F 64 6D 4B 55 46 30 39 36 57 46 31  gDIUodmKUF096WF1
    0160  30 32 31 7A 50 61 72 77 41 62 65 46 6F 47 4C 49  021zParwAbeFoGLI
    0170  39 4E 36 6E 39 76 57 43 2F 32 75 6C 35 5A 6B 2F  9N6n9vWC/2ul5Zk/
    0180  49 70 4E 57 56 31 33 70 67 3D 3D                 IpNWV13pg==
    
    0000  30 82 01 23 30 81 CE 02 09 00 A1 F0 F1 AC 42 2F  0‚.#0Î...¡ðñ¬B/
    0010  FF DD 30 0D 06 09 2A 86 48 86 F7 0D 01 01 0B 05  ÿÝ0...*†H†÷.....
    0020  00 30 19 31 17 30 15 06 03 55 04 03 0C 0E 74 65  .0.1.0...U....te
    0030  73 74 2E 73 62 69 74 2D 2E 69 6E 74 30 1E 17 4C  st.sbit-.int0..L
    0040  8C 0C 0D 0C 8D CC 4C 8C 0D CC 4D 56 85 C3 4C 8C  Œ...ÌLŒ.ÌMV…ÃLŒ
    0050  4C 0D 0C 8D CC 4C 8C 0D CC 4D 56 8C 06 4C 45 CC  L..ÌLŒ.ÌMVŒ.LEÌ
    0060  05 41 80 D5 41 00 C3 03 9D 19 5C DD 0B 9C D8 9A  .A€ÕA.Ã..\Ý.œØš
    0070  5D 0B 4B 9A 5B 9D 0C 17 0C 03 41 82 4A A1 92 21  ].Kš[....A‚J¡’!
    0080  BD C3 40 40 40 41 40 00 D2 C0 0C 12 00 90 40 3E  ½Ã@@@A@.ÒÀ...@>
    0090  3B 26 AF 6B 02 A0 2C C2 F8 83 0F D9 34 6B B9 1C  ;&¯k. ,øƒ.Ù4k¹.
    00a0  0A 35 49 50 E0 07 F1 53 B4 8D 93 D4 B3 98 DA CB  .5IPà.ñS´“Ô³˜ÚË
    00b0  70 FE 94 6A 1D 00 22 EC C4 C5 FA A9 CB 9C BB 21  pþ”j.."ìÄÅú©Ëœ»!
    00c0  A2 9F 78 B1 47 FB 43 DF 7F D1 1A 8C A5 45 B3 C0  ¢Ÿx±GûCßÑ.Œ¥E³À
    00d0  80 C0 40 00 4C 03 41 82 4A A1 92 21 BD C3 40 40  €À@.L.A‚J¡’!½Ã@@
    00e0  42 C1 40 00 D0 40 34 BC EE 6C CE B7 59 C9 CA 0C  BÁ@.Ð@4¼îlηYÉÊ.
    00f0  51 44 0E 1A C7 1A 6E 8F 65 BB 94 59 80 32 14 A1  QD..Ç.ne»”Y€2.¡
    0100  D9 8A 50 5D 3D E9 61 75 D3 6D 73 3D AA F0 01 B7  يP]=éauÓms=ªð.·
    0110  85 A0 62 C8 F4 DE A7 F6 F5 82 FF 6B A5 E5 99 3F  … bÈôÞ§öõ‚ÿk¥å™?
    0120  22 93 56 57 5D E9                                "“VW]é
    
    0000  4D 49 49 42 49 7A 43 42 7A 67 49 4A 41 4B 48 77  MIIBIzCBzgIJAKHw
    0010  38 61 78 43 4C 2F 2F 64 4D 41 30 47 43 53 71 47  8axCL//dMA0GCSqG
    0020  53 49 62 33 44 51 45 42 43 77 55 41 4D 42 6B 78  SIb3DQEBCwUAMBkx
    0030  46 7A 41 56 42 67 4E 56 42 41 4D 4D 44 6E 52 6C  FzAVBgNVBAMMDnRl
    0040  63 33 51 75 63 32 4A 70 64 43 30 75 61 57 35 30  c3Quc2JpdC0uaW50
    0050  4D 42 34 58 54 49 77 4D 44 51 79 4E 7A 45 79 4D  MB4XTIwMDQyNzEyM
    0060  44 63 78 4E 56 6F 58 44 54 49 78 4D 44 51 79 4E  DcxNVoXDTIxMDQyN
    0070  7A 45 79 4D 44 63 78 4E 56 6F 77 47 54 45 58 4D  zEyMDcxNVowGTEXM
    0080  42 55 47 41 31 55 45 41 77 77 4F 64 47 56 7A 64  BUGA1UEAwwOdGVzd
    0090  43 35 7A 59 6D 6C 30 4C 53 35 70 62 6E 51 77 58  C5zYml0LS5pbnQwX
    00a0  44 41 4E 42 67 6B 71 68 6B 69 47 39 77 30 42 41  DANBgkqhkiG9w0BA
    00b0  51 45 46 41 41 4E 4C 41 44 42 49 41 6B 45 41 2B  QEFAANLADBIAkEA+
    00c0  4F 79 61 76 61 77 4B 67 4C 4D 4C 34 67 77 2F 5A  OyavawKgLML4gw/Z
    00d0  4E 47 75 35 48 41 6F 31 53 56 44 67 42 2F 46 54  NGu5HAo1SVDgB/FT
    00e0  74 49 32 54 31 4C 4F 59 32 73 74 77 2F 70 52 71  tI2T1LOY2stw/pRq
    00f0  48 51 41 69 37 4D 54 46 2B 71 6E 4C 6E 4C 73 68  HQAi7MTF+qnLnLsh
    0100  6F 70 39 34 73 55 66 37 51 39 39 2F 30 52 71 4D  op94sUf7Q99/0RqM
    0110  70 55 57 7A 77 49 44 41 51 41 42 4D 41 30 47 43  pUWzwIDAQABMA0GC
    0120  53 71 47 53 49 62 33 44 51 45 42 43 77 55 41 41  SqGSIb3DQEBCwUAA
    0130  30 45 41 30 76 4F 35 73 7A 72 64 5A 79 63 6F 4D  0EA0vO5szrdZycoM
    0140  55 55 51 4F 47 73 63 61 62 6F 39 6C 75 35 52 5A  UUQOGscabo9lu5RZ
    0150  67 44 49 55 6F 64 6D 4B 55 46 30 39 36 57 46 31  gDIUodmKUF096WF1
    0160  30 32 31 7A 50 61 72 77 41 62 65 46 6F 47 4C 49  021zParwAbeFoGLI
    0170  39 4E 36 6E 39 76 57 43 2F 32 75 6C 35 5A 6B 2F  9N6n9vWC/2ul5Zk/
    0180  49 70 4E 57 56 31 33 70                          IpNWV13p
    
  • Of course , I delete new line chars, but more easy to fix it source code.

      **const reg= /\r\n/gi;
      pem=pem.replace(reg, '');
      var sr=atob(pem);**
    

    // var st="";
    // for (var prop in sr){st+=sr.charCodeAt(prop).toString(16)+',';}
    // console.log(st);

      if(flash.writeFileSync("certificate.cer", sr)) {
        console.log('New sertificate data saved...');
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Incorrect decode Base_64

Posted by Avatar for user111703 @user111703

Actions