Search code examples
yodlee

Yodlee REST API ssh encoding


I am implementing addSiteAccount1 to the rest api. The PKI means the username and password must be ssh encoded using a public key file. I have the public key file and I can generate an encrypted binary stream (eg "f\xBDZ\x16\xF5\xE6\xC42 .....". How do I encode this to be POSTed to addSiteAccount1 please? The Yodlee RSA encryption utility seems to generate hex(?) but my hex-encoded stringencrypted_str.unpack('H*') gives me an error response: "Decryption failure for FieldInfo:FieldInfoSingle".


Solution

  • This can also happens if one of the value field you are sending is not encrypted. You should be using BouncyCastle in this case. Adding code here as well for reference-

    public static String encrypt(String plainText) throws Exception{
    
    Security.addProvider(new BouncyCastleProvider());
    String pub = "-----BEGIN PUBLIC KEY-----"+
            "\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtUS7ZJpnbcu8B+mfGrr0Gz6A23lS893mEFNnuR+frbtWDsoIHTfN4yhfbslkzsAMp3ENvM6Ic/0nHEvftrZxFXSrN7n3xZ+mdzOV/u8rqZoB7MEu6mZvdg3zfj7dGglq/fqlYxzHLlxDHjeCrY0dSD0ZAX1zCm3IZ0ufbMBqTrsSaHAuDlIXaQlJXmz3/Y+YfynJZXth/ats1gTBQhMIU9lWutMa4iKkeehn+P9ja4pC9NUlB9W4pojF2Qs+pY4kgTb9+SP8WjnhoSAmJMQGbYwY3HOZyfuOqAmdjoh9Y0LEZ3tq5NGD0b+T7L+P/FuIzvjYZYq6g/FaWaPcVrVLpwIDAQAB"+
            "\n-----END PUBLIC KEY-----";
    System.out.println(pub);
    String strt= pub;
    StringReader fileReader= new StringReader(strt); 
    PEMReader pemReader= new PEMReader(fileReader);
    PublicKey pk= (PublicKey)pemReader.readObject();
    
    Cipher c = Cipher.getInstance(RSA_ECB_PKCS5);
    PublicKey publicKey = pk;
    c.init(Cipher.ENCRYPT_MODE, transformKey(publicKey,     
    "RSA", new BouncyCastleProvider()));
    byte[] encValue= new byte[0];
    try {
        encValue = c.doFinal(plainText.getBytes());
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    String encrypted = DatatypeConverter.printHexBinary(encValue);  
    System.out.println("Encrypted value: "+encrypted);
    return encrypted;
    

    }