Not all the keys can be restored after a flawless attack - if you got some by watching ads or buying them for orbs, they won't be restored. Also, if you had to open the locks in two parts (you watched an ad to get more lockpicks and it made the game reconnect for some reason and you continued opening the locks after finding the same dungeon in the Attack mode after reconnecting), the lockpicks you used before reconnecting won't be restored.