Handle CryptographicException

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
Wilbert
Posts: 2
Joined: Thu Jan 12, 2017 2:54 pm

Handle CryptographicException

Post by Wilbert »

If I try to load an encrypted file with ES2.Load() but there's something wrong with it (wrong password or wrong file) I get an Exception error like:
CryptographicException: Bad PKCS7 padding. Invalid length 53.

If I try to handle this with

Code: Select all

try
            {
                liStrLoad = ES2.LoadList<string>(Application.dataPath + "/Resources/control.dat?encrypt=true&password=" + strPassLocal);
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: '{0}'", e);
            }

The catch statement isn't run and the exception is handled silently

How do I do this ?
I want to send the user a message there's something wrong with the file.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Handle CryptographicException

Post by Joel »

Hi there,

I've just tried your code at my end and the exception appears to be caught fine at my end. If you're not using the latest version of Unity, you might want to try updating as there might be a bug at their end causing try/catch blocks to fail. It will also be worth checking that the Exception is indeed thrown by that method call. And you might also want to check on the Unity forums to see if anyone else is having issues with try/catch blocks being ignored.

I've also created a standalone test script below which also works fine for me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestCryptographicException : MonoBehaviour {

	// Use this for initialization
	void Start () 
	{
		ES2.Save(123, "myEncryptedFile.txt?tag=myTag&encrypt=true&password=myPass");
		try
		{
			ES2.Load<int>("myEncryptedFile.txt?tag=myTag&encrypt=true&password=myWrongPass");
		}
		catch(System.Exception e)
		{
			Debug.Log("Exception caught.");
		}
	}
}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Wilbert
Posts: 2
Joined: Thu Jan 12, 2017 2:54 pm

Re: Handle CryptographicException

Post by Wilbert »

It's working now.

Problem was that Console.WriteLine() doesn't work in Unity :shock:
(You learn something everyday..)

Thanks for the help!
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Handle CryptographicException

Post by Joel »

Glad you managed to solve your problem!

- Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Locked