Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Read signature data from APK's RSA signature file

Read signature data from APK's RSA signature file

Scheduled Pinned Locked Moved C#
androidcsharpjavacryptographydebugging
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mynametaken
    wrote on last edited by
    #1

    I have the source in java that reads signature data from RSA (erxtracted from APK (Android Package) file) and it works flawlessly but i would like to convert to C# in order to use in my c# application. The issue is i'm getting different data from GetRawCertData() no matter what I have tried. This is the result: C#: AQAAADCCBYkwggNxoAMCAQICFQDmX5cziG0zO22ity1a/dKI6FnZyzANBgk... Java: AQAABY0wggWJMIIDcaADAgECAhUA5l+XM4htMzttorctWv3SiOhZ2cswDQYJKoZIhvcNA... I'm not familar with signatures, and i'm not even sure what excat format is it but I know it is signed with Java's KeyStore file. What should I do to get it right? it is very close though. Executing .jar file would be a workaround but I would like to avoid executing something externally This is my C# code

    X509Certificate cert = X509Certificate.CreateFromSignedFile("BNDLTOOL.RSA");

    MemoryStream ms = new MemoryStream();
    BinaryWriter bw = new BinaryWriter(ms);
    bw.Write(1);

    byte[] data = cert.GetRawCertData();
    bw.Write(data);
    bw.Write(data.Length);

    byte[] buffer = ms.ToArray();

    Debug.WriteLine(Convert.ToBase64String(buffer));

    This is Java code from a project that I found online

    PKCS7 pkcs7 = new PKCS7(StreamUtil.readBytes(zipFile.getInputStream(ze)));
    Certificate[] certs = pkcs7.getCertificates();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.write(certs.length);
    for (int i = 0; i < certs.length; i++) {
    byte[] data = certs[i].getEncoded();
    System.out.printf(" --SignatureHash[%d]: %08x\n", i, Arrays.hashCode(data));
    dos.writeInt(data.length);
    dos.write(data);
    }
    byte[] signatures = baos.toByteArray();
    System.out.println(Base64.getEncoder().encodeToString(signatures));

    J L 3 Replies Last reply
    0
    • M mynametaken

      I have the source in java that reads signature data from RSA (erxtracted from APK (Android Package) file) and it works flawlessly but i would like to convert to C# in order to use in my c# application. The issue is i'm getting different data from GetRawCertData() no matter what I have tried. This is the result: C#: AQAAADCCBYkwggNxoAMCAQICFQDmX5cziG0zO22ity1a/dKI6FnZyzANBgk... Java: AQAABY0wggWJMIIDcaADAgECAhUA5l+XM4htMzttorctWv3SiOhZ2cswDQYJKoZIhvcNA... I'm not familar with signatures, and i'm not even sure what excat format is it but I know it is signed with Java's KeyStore file. What should I do to get it right? it is very close though. Executing .jar file would be a workaround but I would like to avoid executing something externally This is my C# code

      X509Certificate cert = X509Certificate.CreateFromSignedFile("BNDLTOOL.RSA");

      MemoryStream ms = new MemoryStream();
      BinaryWriter bw = new BinaryWriter(ms);
      bw.Write(1);

      byte[] data = cert.GetRawCertData();
      bw.Write(data);
      bw.Write(data.Length);

      byte[] buffer = ms.ToArray();

      Debug.WriteLine(Convert.ToBase64String(buffer));

      This is Java code from a project that I found online

      PKCS7 pkcs7 = new PKCS7(StreamUtil.readBytes(zipFile.getInputStream(ze)));
      Certificate[] certs = pkcs7.getCertificates();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      DataOutputStream dos = new DataOutputStream(baos);
      dos.write(certs.length);
      for (int i = 0; i < certs.length; i++) {
      byte[] data = certs[i].getEncoded();
      System.out.printf(" --SignatureHash[%d]: %08x\n", i, Arrays.hashCode(data));
      dos.writeInt(data.length);
      dos.write(data);
      }
      byte[] signatures = baos.toByteArray();
      System.out.println(Base64.getEncoder().encodeToString(signatures));

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      This...

      mynametaken wrote:

      %08x

      Is not even close to being the same as the following.

      mynametaken wrote:

      ToBase64String

      So presumably that is not where you see a difference.

      M 1 Reply Last reply
      0
      • M mynametaken

        I have the source in java that reads signature data from RSA (erxtracted from APK (Android Package) file) and it works flawlessly but i would like to convert to C# in order to use in my c# application. The issue is i'm getting different data from GetRawCertData() no matter what I have tried. This is the result: C#: AQAAADCCBYkwggNxoAMCAQICFQDmX5cziG0zO22ity1a/dKI6FnZyzANBgk... Java: AQAABY0wggWJMIIDcaADAgECAhUA5l+XM4htMzttorctWv3SiOhZ2cswDQYJKoZIhvcNA... I'm not familar with signatures, and i'm not even sure what excat format is it but I know it is signed with Java's KeyStore file. What should I do to get it right? it is very close though. Executing .jar file would be a workaround but I would like to avoid executing something externally This is my C# code

        X509Certificate cert = X509Certificate.CreateFromSignedFile("BNDLTOOL.RSA");

        MemoryStream ms = new MemoryStream();
        BinaryWriter bw = new BinaryWriter(ms);
        bw.Write(1);

        byte[] data = cert.GetRawCertData();
        bw.Write(data);
        bw.Write(data.Length);

        byte[] buffer = ms.ToArray();

        Debug.WriteLine(Convert.ToBase64String(buffer));

        This is Java code from a project that I found online

        PKCS7 pkcs7 = new PKCS7(StreamUtil.readBytes(zipFile.getInputStream(ze)));
        Certificate[] certs = pkcs7.getCertificates();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.write(certs.length);
        for (int i = 0; i < certs.length; i++) {
        byte[] data = certs[i].getEncoded();
        System.out.printf(" --SignatureHash[%d]: %08x\n", i, Arrays.hashCode(data));
        dos.writeInt(data.length);
        dos.write(data);
        }
        byte[] signatures = baos.toByteArray();
        System.out.println(Base64.getEncoder().encodeToString(signatures));

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Your statement

        bw.Write(1);

        writes a 4-byte int with value 1, whereas your Java string "AQAABY0w..." starts with a byte of value 1. Maybe what you want is:

        bw.Write((byte)1);

        :)

        Luc Pattyn [My Articles] The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.

        M 1 Reply Last reply
        0
        • J jschell

          This...

          mynametaken wrote:

          %08x

          Is not even close to being the same as the following.

          mynametaken wrote:

          ToBase64String

          So presumably that is not where you see a difference.

          M Offline
          M Offline
          mynametaken
          wrote on last edited by
          #4

          %08x is not related to it. I have edited the question

          1 Reply Last reply
          0
          • L Luc Pattyn

            Your statement

            bw.Write(1);

            writes a 4-byte int with value 1, whereas your Java string "AQAABY0w..." starts with a byte of value 1. Maybe what you want is:

            bw.Write((byte)1);

            :)

            Luc Pattyn [My Articles] The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.

            M Offline
            M Offline
            mynametaken
            wrote on last edited by
            #5

            This one in Java always returned 1

            dos.write(certs.length);

            So i used

            bw.Write(1);

            L 1 Reply Last reply
            0
            • M mynametaken

              This one in Java always returned 1

              dos.write(certs.length);

              So i used

              bw.Write(1);

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              and when you read the documentation[^] you'll notice that silly Java write method takes an int and writes a byte!!!

              Luc Pattyn [My Articles] The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.

              M 1 Reply Last reply
              0
              • L Luc Pattyn

                and when you read the documentation[^] you'll notice that silly Java write method takes an int and writes a byte!!!

                Luc Pattyn [My Articles] The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.

                M Offline
                M Offline
                mynametaken
                wrote on last edited by
                #7

                I see but when I used

                bw.Write((byte)1);

                , data comes out very different, not even close to Java one

                1 Reply Last reply
                0
                • M mynametaken

                  I have the source in java that reads signature data from RSA (erxtracted from APK (Android Package) file) and it works flawlessly but i would like to convert to C# in order to use in my c# application. The issue is i'm getting different data from GetRawCertData() no matter what I have tried. This is the result: C#: AQAAADCCBYkwggNxoAMCAQICFQDmX5cziG0zO22ity1a/dKI6FnZyzANBgk... Java: AQAABY0wggWJMIIDcaADAgECAhUA5l+XM4htMzttorctWv3SiOhZ2cswDQYJKoZIhvcNA... I'm not familar with signatures, and i'm not even sure what excat format is it but I know it is signed with Java's KeyStore file. What should I do to get it right? it is very close though. Executing .jar file would be a workaround but I would like to avoid executing something externally This is my C# code

                  X509Certificate cert = X509Certificate.CreateFromSignedFile("BNDLTOOL.RSA");

                  MemoryStream ms = new MemoryStream();
                  BinaryWriter bw = new BinaryWriter(ms);
                  bw.Write(1);

                  byte[] data = cert.GetRawCertData();
                  bw.Write(data);
                  bw.Write(data.Length);

                  byte[] buffer = ms.ToArray();

                  Debug.WriteLine(Convert.ToBase64String(buffer));

                  This is Java code from a project that I found online

                  PKCS7 pkcs7 = new PKCS7(StreamUtil.readBytes(zipFile.getInputStream(ze)));
                  Certificate[] certs = pkcs7.getCertificates();
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  DataOutputStream dos = new DataOutputStream(baos);
                  dos.write(certs.length);
                  for (int i = 0; i < certs.length; i++) {
                  byte[] data = certs[i].getEncoded();
                  System.out.printf(" --SignatureHash[%d]: %08x\n", i, Arrays.hashCode(data));
                  dos.writeInt(data.length);
                  dos.write(data);
                  }
                  byte[] signatures = baos.toByteArray();
                  System.out.println(Base64.getEncoder().encodeToString(signatures));

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  mynametaken wrote:

                  bw.Write(data); bw.Write(data.Length);

                  That is not the same as the following. The order is different.

                  mynametaken wrote:

                  dos.writeInt(data.length); dos.write(data);

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups