我们正在开发一个有安全意识的应用程序,我们需要一个应用程序给我们发送用于连接的证书。
是否可以下载证书并在Android上发送。我们需要这样做,以便我们可以检查证书是否已被泄露。或者,最好从证书中检索详细信息,将其写入日志,然后发送。我们有一个可以发送详细信息的api
发布于 2017-07-05 18:23:14
我看你之前已经要求锁定了..。
您可以通过以下方式固定证书
Certificate[] certs = conn.getServerCertificates();
MessageDigest md = MessageDigest.getInstance("SHA-256");
for (Certificate cert : certs) {
X509Certificate x509Certificate = (X509Certificate) cert;
byte[] key = x509Certificate.getPublicKey().getEncoded();
md.update(key, 0, key.length);
byte[] hashBytes = md.digest();
StringBuilder hexHash = new StringBuilder();
for (byte hashByte : hashBytes) {
int k = 0xFF & hashByte;
String tmp = (k < 16) ? "0" : "";
tmp += Integer.toHexString(0xFF & hashByte);
hexHash.append(tmp);
}
// You can even log cert.toString()
Log.d(TAG, hexHash.toString()); get the hash from here
// If you create `pins` as a Set with all the valid pins from above
if (pins.contains(hexHash.toString())) {
return true;
}
}您可以在连接conn连接之后运行此命令,如果引脚不匹配,则可以在发送数据之前中止。注意:上面的代码固定证书的公钥,而不是证书。
如果你只是想要证书,你可以直接获取cert.toString()
https://stackoverflow.com/questions/44922912
复制相似问题