【发布时间】:2016-03-31 00:01:56
【问题描述】:
我正在尝试使用SH1 RSA 对消息进行编码,但除了有关RSA 的一些基本信息外,我没有安全主题方面的经验。我得到了一个私钥String。我已经设法编写了以下代码块来完成这项工作,但我不确定我是否安全且正确地完成了这项工作。
我不是专家,但我猜将我的私钥作为字符串放入代码中并不安全。谁能指导我?
String privateKeyString = "mykeyhere...";
byte[] privateKeyBytes = privateKeyString.getBytes();
String encodedPrivateKey = Base64.encodeToString(privateKeyBytes, Base64.URL_SAFE);
KeyFactory factory = KeyFactory.getInstance(RSA);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedPrivateKey.getBytes());
RSAPrivateKey privateKey = (RSAPrivateKey) factory.generatePrivate(keySpec);
Signature instance = Signature.getInstance(ALGORITHM);
instance.initSign(privateKey);
instance.update(content.getBytes());
return new String(instance.sign());
我的私钥格式如下:
"-----BEGIN PRIVATE KEY-----\n"+
"MIIE...\n"+
"cH0iRj...\n"+
"O0Hhj...\n"+
.
.
.
"fG6...\n"+
"B6/hF...\n"+
"3Mq38...\n"+
"-----END PRIVATE KEY-----\n"
【问题讨论】:
-
什么是privateKeyString?密码?还是真正的数字 RSA 私钥?并以什么格式给出? Base64?十六进制?其他?
-
@konstantinosChalkias,我添加了我的密钥格式。
-
你试过Keystore吗? stackoverflow.com/questions/9890313/…
标签: android security rsa private-key