import java.security.*;
public class MyKeyGenerator {
private KeyPairGenerator keyGen;
private KeyPair pair;
private PrivateKey privateKey;
private PublicKey publicKey;
private Context context;
public MyKeyGenerator(Context context, int length)throws Exception{
this.context =context;
this.keyGen = KeyPairGenerator.getInstance("RSA");
this.keyGen.initialize(length);
}
public void createKeys(){
this.pair = this.keyGen.generateKeyPair();
this.privateKey = pair.getPrivate();
this.publicKey = pair.getPublic();
}
public PrivateKey getPrivateKey(){
return this.privateKey;
}
public PublicKey getPublicKey(){
return this.publicKey;
}
public String getPrivateKeyStr(){
byte b [] = this.getPrivateKey().getEncoded();
return new String(b));
}
public String getPublicKeyStr(){
byte b [] = this.getPublicKey().getEncoded();
return new String(b));
}
}
您好,我已经搜索过如何转换或获取公钥或私钥的字符串表示形式,大多数答案都很旧,仅用于如何转换 String pubKey ="...."; 成钥匙。我尝试生成密钥并获取编码的字节,并尝试将字节转换为字符串,如上面的代码所示,但我不确定我是否通过简单地将编码的字节转换为字符串来以正确的方式执行此操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句