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));
}
}
您好,我已经在SO中搜索了如何转换或获取公钥或私钥的String表示形式,大多数答案都非常古老,仅是如何转换String pubKey =“ ....”;变成钥匙。 我尝试生成密钥并获取编码的字节,并尝试将字节转换为String,如上面的代码所示,但是我不确定是否通过简单地将编码的字节转换为String来以正确的方式进行操作。
答案 0 :(得分:1)