1

As you can see in the post (Java):

getMFAResponseForSite - rendering array as a captcha image

and (C#)

Yodlee: Unable to convert image codes into captcha in getMFAResponseForSite(Captcha type) - C#

Yodlee API getMFAResponseForSite answers with a JSON containing the MFA form. In Python I am trying the following solution with no result:

import array
import base64

img_array = [66, 77, -98, -19, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40,...]
new_img_array = []

for x in img_array:
    new_img_array.append(abs(x))

img_byte_array = bytearray(new_img_array)
fh = open("path.jpg", "wb")
fh.write(img_byte_array)
fh.close()

I tryed to cast the bytes array directly, but it throws an error because byte values must be between 0-255

I hope someone knows how to solve this

Community
  • 1
  • 1
calvo215
  • 23
  • 6
  • Have a look at [this][1] question, this may help. [1]: http://stackoverflow.com/questions/5088671/convert-java-byte-array-to-python-byte-array – Apoorv Awasthi Aug 24 '15 at 16:02

3 Answers3

1

there are a number of extra steps being taken here, as well as imports that are not used. in addition, for me, the yodlee image data that is coming back is windows bmp data (not jpg). here is the essence of the answer:

with open('captcha.bmp', 'wb') as c:
    write(''.join(map(lambda x: chr(x % 256), img_array)))

or, as suggested in the linked post:

with open('captcha.bmp', 'wb') as c:
    write(str(bytearray(map(lambda x: chr(x % 256), img_array))))

this works directly on the fieldInfo.image array supplied by getMFAResponseForSite.

bobchase
  • 219
  • 3
  • 3
0

Thanks to user Apporv for his help, now I am answering my question:

Using the following post, I converted the Yodlee byte array to a Python one. Code is:

import array
import base64

img_array = [66, 77, -98, -19, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40,...]    

bin_data = ''.join(map(lambda x: chr(x % 256), img_array))
new_img_array = []

for x in bin_data:
    new_img_array.append(x)

img_byte_array = bytearray(new_img_array)
fh = open("path.jpg", "wb")
fh.write(img_byte_array)
fh.close()

That's it!

Community
  • 1
  • 1
calvo215
  • 23
  • 6
0

And c# version:

var array = new int[] { 66, 77, 110, -60, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0};
var byteList = new List<byte>();

foreach (var item in array)
{
    var value = (byte)(item < 0 ? byte.MaxValue + 1 + item : item);
    byteList.Add(value);
}

File.WriteAllBytes(@"captcha.jpg", byteList.ToArray());