StringUtil.convertEncodedString returns System Error: Failed invoking <symbol>

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:

var originalPlainText = decryptKey("8qyw5f3Ufmx42DEq+oezC/YkWaxwNWlMYPqMor5reYLUThgXtbaeYrYHhY/jDWNB");

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.
/Juan
  • Yes, exactly what I'm doing, using Storage and saving the epoch time, Time.now().value(), and yes I will compare against the current one.

    Thank you again.

  • Hi there @jim_m_58, going back to this subject, I would love a suggestion here. The thing is that I'm testing my Watch Face from a Beta version installed from IQ Connect. After the installation, I can see that for some reason the installation date is not being store properly in the Application.Storage. 

    This is my code:

    class MyWatchFaceApp extends Application.AppBase {
    
        function initialize() {
            AppBase.initialize();
        }
        
        ...
        
        function onAppInstall() as Void {
            storeInstallationDate();
        }
    
        function onAppUpdate() as Void {
            storeInstallationDate();
        }
    
        ...
    
        hidden function storeInstallationDate() as Void {
            var epochInstalledOn = Application.Storage.getValue(INSTALLED_ON_DATE_STORAGE_KEY);
            if (epochInstalledOn == null) {
                var epoch = Time.now().value();
                Application.Storage.setValue(INSTALLED_ON_DATE_STORAGE_KEY, epoch);
            }
        }
    }


    However, when I run the app in the simulator, I can see that there is an installation date from yesterday in the menu item I have for that in the app settings.

    What can possibly be wrong here?

    I can't yet make the onAppInstall or onAppUpdate execute whenever I want in the simulator. How can I uninstall the watchface everytime before debugging to test this feature?

    Thanks in advance!
     

  • try Simulator > File > Reset All App Data

    Looks like it kept the state from yesterdays' run.

  • Hi the Reset All App Data didn't work, but the Delete All Apps it did. Thanks for your response.