我的Android应用程序中有此代码,用于生成RSA KeyPair。此代码在某些设备上的Android 8上生成相同的密钥对。设备包括Lenovo K8,Nokia 3。
仅在这些设备上,相同型号之间生成的密钥对相同。 如果我不使用AndroidKeyStore Provider而是使用BouncyCastle,则密钥是唯一的。但我想使用android提供程序,并且仍然生成唯一的密钥。
KeyPairGenerator keyPairGenerator = null;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder("somealias", KeyProperties.PURPOSE_SIGN)
.setDigests(KeyProperties.DIGEST_SHA256)
.setKeySize(2048)
.build();
keyPairGenerator.initialize(spec);
} catch(Exception e) {
throw new KeyStoreException(e);
}
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
byte[] encodedP = publicKey.getEncoded();
每次都需要在所有设备上获取唯一密钥。
使用此代码行连续两次运行,encodeP的Base64编码值:
字符串basePub = Base64.getEncoder()。encodeToString(encodedP);
运行#1
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9RmCTb9FJG8rS9ldKiLQsQ7wNtKMvr8aLB + 5V7VlNxKkL14ZE + gBMbsHg0QNEL7SL + UiorDeHT5gMg1pHQrq / dJY8WehjXmOsjol8DZaX0qsOLKAqCP0mYK90OjbyZOoGNs7qq7crtSv + oF8aDFzUcTbx / SghEQM9ZjGXSgckpPUsMX3zzf3cZvwZPbTG6y9qAR43nlEAIRaeAPtz8hh / FunW5UeXajsHpyoD9im4HRgm3OzoZYdHXd6kSy1eCmqNJ4DzVmq9EVpX5YTBb3RqeADk7pVGck + eJK4d91Vk5cdsykX6ITCfQwFi1duywHkAZjIG6lXxsTW1HoC96qIfwIDAQAB
RUN#2
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9RmCTb9FJG8rS9ldKiLQsQ7wNtKMvr8aLB + 5V7VlNxKkL14ZE + gBMbsHg0QNEL7SL + UiorDeHT5gMg1pHQrq / dJY8WehjXmOsjol8DZaX0qsOLKAqCP0mYK90OjbyZOoGNs7qq7crtSv + oF8aDFzUcTbx / SghEQM9ZjGXSgckpPUsMX3zzf3cZvwZPbTG6y9qAR43nlEAIRaeAPtz8hh / FunW5UeXajsHpyoD9im4HRgm3OzoZYdHXd6kSy1eCmqNJ4DzVmq9EVpX5YTBb3RqeADk7pVGck + eJK4d91Vk5cdsykX6ITCfQwFi1duywHkAZjIG6lXxsTW1HoC96qIfwIDAQAB