SabreTools / NDecrypt

Cartridge encrypt/decrypt
MIT License
43 stars 6 forks source link

.NET 6 version wrongly decrypts 3ds roms #7

Closed Brankale closed 1 year ago

Brankale commented 2 years ago

When you try to decrypt a 3DS rom with the .NET 6 version, the final file is corrupted and if you try to re-encrypt it you don't get the original file

.NET 4.8 and 5.0 versions are not affected by this problem.

mnadareski commented 2 years ago

I'm not seeing the typical issues that were caused by the stream reading/writing change in .NET 6. I'll have to dig in.

mnadareski commented 2 years ago

Hmm, so I have the library compiled to netstandard2.0 so there must be something up with the commandline program?

mnadareski commented 2 years ago

I made a few smaller changes that are semi-related to .NET 6 that may help with this. Not a guarantee, but please try it out. I'll try to test locally when possible.

mnadareski commented 2 years ago

Local testing on a very small 3DS game worked on both decrypt and re-encrypt using the newest .NET 6 build on Windows 10. Hopefully that works for you as well.

Brankale commented 2 years ago

If you can upload here the executable in a zip file you would save me a ton of time. If you can't, I'll try to make it myself as soon as I can find the time.

mnadareski commented 2 years ago

Here you go: net6.0.zip

Brankale commented 2 years ago

Unfornately these changes don't solve the issue. However I did further testing and: 1) encryption seems to work fine (both the old and the new version) 2) decryption works only on some games. For example Super Mario 3D Land decryption works but in games like Pokemon Sun/Ultramoon or Zelda Majora's Mask, decryption is not working. 3) I did the test only on one game but it seems that the decryption problem is at the beginning of the file in the first couple of MB (probably the header). Take this info with a grain of salt.

mnadareski commented 2 years ago

Were the file sizes different as well? I may have to look into write operations if that's the case. It feels like this isn't a decrypt/encrypt library issue at this point, rather something yet harder to track down.

Brankale commented 2 years ago

Yes

super mario 3d land was the only one below 1GB (512MB to be specific)

Zelda was 1 GB if I remember correctly and Pokemon games are 4GB in size

I thought about the file size too, but currently i dont have the tools to verify it

Brankale commented 2 years ago

Since the .NET 6 is currently buggy, is it possible to remove this version from the github releases tab? I ask for this because this program is used to submit hashes on DAT-o-MATIC for rom dumps verification and game preservation purposes. Since the program can output wrong values a user who is unaware of this problem can accidentally submit wrong data to the website. Alternatively you can put a warning somewhere.

mnadareski commented 2 years ago

An entirely reasonable ask. Let me see what I can do.

mnadareski commented 2 years ago

It's been removed from the release page and a note was put up. Good call.

mnadareski commented 2 years ago

I've been looking into this pretty heavily to see if there's anything in my code that is causing this issue. Unfortunately, I've narrowed it down to (seemingly) the library I use for AES encryption support, where their internal code may not deal properly with byte reading changes in .NET 6.0. I'll be trying to narrow it down even further, but that's the best I have right now.

mnadareski commented 2 years ago

I may have narrowed it down further to something adjacent to the encryption. There were things happening async that made it seem like the encryption and decryption weren't working.

mnadareski commented 2 years ago

I think I was able to fix the issue in https://github.com/SabreTools/NDecrypt/commit/7d263886daedb4a4336cd1524d7d664e11314586

Please see the attached build. net6.0.zip

Brankale commented 2 years ago

I’ll look into that in the next couple of days as soon as I can. Sorry but i’m pretty busy at the moment

Brankale commented 1 year ago

I've tried with a couple of games and the issue seems to be fixed

mnadareski commented 1 year ago

That's great news! This will probably mean a new stable release. Thank you for your patience.

mnadareski commented 1 year ago

Officially released as part of https://github.com/SabreTools/NDecrypt/releases/tag/0.2.4