Read signature data from APK's RSA signature file
-
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)); -
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)); -
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));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.
-
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.
%08x is not related to it. I have edited the question
-
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.
This one in Java always returned 1
dos.write(certs.length);
So i used
bw.Write(1);
-
This one in Java always returned 1
dos.write(certs.length);
So i used
bw.Write(1);
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.
-
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.
I see but when I used
bw.Write((byte)1);
, data comes out very different, not even close to Java one
-
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));