DataPower

DataPower

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Encryption issue

    Posted 02/19/23 03:35 AM

    Dears,

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

    • Firstly, we must 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 these 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 = newX509EncodedKeySpec(publicKeyBytes) publicKey =publicKeyFactory.generatePublic(publicKeySpec) } catch (NoSuchAlgorithmException |InvalidKeySpecException e) { e.printStackTrace } return publicKey } public static Stringencode(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 newString(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'.




  • 2.  RE: Encryption issue

    Posted 02/20/23 07:00 AM

    You cannot deal with binary data unencoded in XML, and that is what the message is telling you.
    Use GatewayScript "Buffer" type to deal with binary data:
    https://www.ibm.com/docs/en/datapower-gateway/2018.4?topic=objects-buffer-object



    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support & Fixpack team lead
    IBM DataPower Gateways (⬚ᵈᵃᵗᵃ / ⣏⠆⡮⡆⢹⠁⡮⡆⡯⠂⢎⠆⡧⡇⣟⡃⡿⡃)
    https://stamm-wilbrandt.de/en/blog/
    ------------------------------