Skip to content Skip to sidebar Skip to footer

Rsa Using Spongycastle

My knowledge of encryption is very basic, so apologies for any ignorance on my part. Within an Android app I am currently trying to mimic the execution of this command using the Sp

Solution 1:

This was eventually solved using the following methods:

private void stripHeaders(){

    public_key = public_key.replace("-----BEGIN PUBLIC KEY-----", "");
    public_key = public_key.replace("-----END PUBLIC KEY-----", "");

}

public byte[] encryptWithPublicKey(String encrypt) throws Exception {
    byte[] message = encrypt.getBytes("UTF-8");
    stripHeaders(); 
    PublicKey apiPublicKey= getRSAPublicKeyFromString(); 
    Cipher rsaCipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
    rsaCipher.init(Cipher.ENCRYPT_MODE, apiPublicKey); 
    return rsaCipher.doFinal(message);
}

private PublicKey getRSAPublicKeyFromString() throws Exception{
    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "SC"); 
    byte[] publicKeyBytes = Base64.decode(public_key.getBytes("UTF-8"), Base64.DEFAULT); 
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes); 
    return keyFactory.generatePublic(x509KeySpec);
}

Post a Comment for "Rsa Using Spongycastle"