DataPower

 View Only
  • 1.  RSA Encryption

    Posted Mon February 06, 2023 07:18 AM
    Edited by Riham Lamei Mon February 06, 2023 07:22 AM

    Dears,

    I'm implementing a case that I need to encrypt a value. Using x509 key so to use this key,

    • Firstly we have to decode it base64.
    • Then encrypt the decoded key using RSA algorithm.
    • Thereby Generate the Public Key.
    This PublicKey will be used to encrypt a message sensitive field using RSA algorithm.


    The attached java code implements this requirements perfectly, However, I am not able to implement it correctly on the DP.
    Your help is highly appreciated to do this on the Data Power.

    public static String loadPublicKey(String pubKey) {
    
    byte[] publicKeyBytes = Base64.decodeBase64(pubKey);
    PublicKey publicKey = null;
    KeyFactor publicKeyFactory;
    
    try {
    publicKeyFactory = KeyFactory.getInstance("RSA");
    EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes)
    publicKey = publicKeyFactory.generatePublic(publicKeySpec)
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
    e.printStackTrace
    }
    
    return publicKey
    }
    
    
    
    public static String encode(String toEncode, String publicKeyBase64) {
    PublicKey publicKey = loadPublicKey(publicKeyBase64);
    byte[] bytes = null;
    
    try {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    bytes = cipher.doFinal(toEncode.getBytes(StrndardCharsets.UTF_8));
    } catch (InvalidKeySpecException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException e) {
    e.printStackTrace
    }
    
    return new String(Base64.encodeBase64(bytes))
    }

    Note:

    After trying to decode the key using xslt here is the error shows 'Valid base64 passed to dp:decode resulted in non UTF8'.



    ------------------------------
    Riham Lamei
    ------------------------------


  • 2.  RE: RSA Encryption

    Posted Thu March 02, 2023 12:49 PM

    Are you trying to create public key at run time?  Could you paste an example of the payload with the field that you're trying to encrypt ? 



    ------------------------------
    Andrew White
    ------------------------------