I'm trying to use the Cryptography.Cipher class to decrypt a text I previously encrypted using the following configs:
This is the code I have atm:
hidden function convertStringToByteArray(plainText as String) as ByteArray { var options = { :fromRepresentation => StringUtil.REPRESENTATION_STRING_PLAIN_TEXT, :toRepresentation => StringUtil.REPRESENTATION_BYTE_ARRAY, :encoding => StringUtil.CHAR_ENCODING_UTF8 }; var result = StringUtil.convertEncodedString(plainText, options); return result; } hidden function convertByteArrayToString(byteArray as ByteArray ) as String { var options = { :fromRepresentation => StringUtil.REPRESENTATION_BYTE_ARRAY, :toRepresentation => StringUtil.REPRESENTATION_STRING_PLAIN_TEXT, :encoding => StringUtil.CHAR_ENCODING_UTF8 }; var result = StringUtil.convertEncodedString(byteArray, options); return result; } function decryptKey(deviceId as String) as String { var key = convertStringToByteArray("ec41e0ef9dc3469bbb0cd7849e00a0e7"); System.println(key.toString()); var iv = convertStringToByteArray("0000000000000000"); var cipher = new Toybox.Cryptography.Cipher({ :algorithm => Toybox.Cryptography.CIPHER_AES256, :mode => Toybox.Cryptography.MODE_CBC, :key => key, :iv => iv }); var encryptedString = convertStringToByteArray(deviceId); var encryptedBytes = cipher.decrypt(encryptedString); var result = convertByteArrayToString(encryptedBytes); return result; }
But, when I call mu function like this:
I get an exception in the convertByteArrayToString function in var result = StringUtil.convertEncodedString(byteArray, options);.
> Error: System Error
> Details: Failed invoking <symbol>
I can't see anything wrong, but perhaps I'm doing something wrong, but I should get back my initial plain text ce5f33a8-4a88-42ed-8dbc-cfedd118ab4b.
Any take on this?
Thanks in advance for the help.