java - android java如何保护/保护我的硬编码密码
问题描述
我想做 CertPinning,用 clientcertificate 发送请求。为此,我需要将密码硬编码以在密钥库初始化中使用它:
keyManagerFactory.init(keyStore, Password.toCharArray());
我该如何保护它?
File pKeyFile;
InputStream caInput = new BufferedInputStream(OkHttpUtils.class.getClassLoader().getResourceAsStream("assets/acptamancli.pfx"));
String Password= "MyPassword!";
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("X509");
KeyStore keyStore = KeyStore.getInstance("PKCS12");
// InputStream keyInput = new FileInputStream(caInput);
keyStore.load(caInput, Password.toCharArray());
caInput.close();
keyManagerFactory.init(keyStore, Password.toCharArray());
SSLContext context = SSLContext.getInstance("TLS");
context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
SSLSocketFactory sockFact = context.getSocketFactory();
con.setSSLSocketFactory(sockFact);
/* Send the request */
OutputStream outputStream = con.getOutputStream();
outputStream.write(requestParams.getBytes("UTF-8"));
outputStream.close();
InputStream inputStream;
if(comparePKeys) {
// Check for errors
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inputStream = con.getInputStream();
} else {
inputStream = con.getErrorStream();
}
} else{
inputStream = con.getErrorStream();
}
// Process the response
InputStream is = inputStream;
int i;
BufferedReader reader;
String line = null;
reader = new BufferedReader(new InputStreamReader(inputStream));
String result = reader.readLine();
inputStream.close();
callback.invoke(null, result);
} catch (Exception e) {
e.printStackTrace();
}
解决方案
你没有。分发包中存在的任何东西都可以被拥有它的人访问。
请注意,您混合了两个独立的概念,证书固定和客户端证书。客户端证书很少在内部公司环境之外使用,固定的公共证书不需要隐藏。
推荐阅读
- c - 如何正确管理 pthread
- ruby - 对我正在执行依赖注入的方法进行单元测试
- nativescript - 使用 nativescript angular 为 RadCharts 中的每个条形图显示不同的图例
- python - Numpy:在循环中使用 npz 格式的 savez 保存多个数组
- javascript - 带有返回函数和 sinon 的存根函数?
- python-3.x - 所有用户的 Pip3 安装模块
- javascript - React 的 createContext 工厂函数有什么意义?
- r - Geosphere 的距离矩阵:避免重复计算
- javascript - Javascript在按钮单击时运行2个函数,带有多个按钮和相同的类
- python - 加速 CSV 文件上的 python 代码操作