I have identical code being compiled and run under Mono (Unity 4.5) and MS .NET:
DSAParameters privateKey;
...
DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(csp);
dsa.PersistKeyInCsp = false;
dsa.ImportParameters(privateKey);
The private key is loaded from disk and the bytes being loaded, and the integers being set, in "privateKey" are identical in the Mono and MS .NET versions.
In the .NET version dsa.ImportParameters throws a "Bad Data" exception.
I have tried
- different .NET settings (3.5 minimal, AnyCPU/x86/x64)
- different keys (some throw, some don't but the .NET signatures are always (?) invalid when I validate them, whereas the ones generated with the same keys and for the same message in Mono work fine.)
Is there some fundamental underlying difference between the way MS DSA implementation and the one from Mono work, under the hood, which could cause this..?
Any light shed is most welcome...this is a bit of a head-scratcher...
Some more info:
- The key is generated in Java and exported after being converted from DER format to work with .NET (P1363; just the big-integers one after the other)
- The key is loaded from disc using BinaryReader and the integer values loaded as byte[] into the DSAParameters (P,Q,G and X)
- The public key is loaded in a Java app which uses the Oracle implementation to verify the signature
- All of the C# code is "symmetric", i.e. used as-is in both the Mono and the MS .NET apps.
Update 2
Alright, I am not going mad; this morning I tried again; same .NET app run on a Mac (using Mono Develop, plain vanilla) and one on Windows (Visual Studio 2010). Same private key bytes loaded, same code path (at least as far as C# is concerned) - same "bad data" exception on Windows only. Here's the key:
P: 17801190547854226652823756245015999014523215636912067427327445031444286578873
70207706126952521234630795671567847784664499706507709207278570500096683881440341
29745221171818506047231150039301079959358067395348717066319802262019714966524135
060945913707594956514672855690606794135837542707371727429551343320695239
Q: 864205495604807476120572616017955259175325408501
G: 17406820753240209518581198012352343653860449079456135097849583104059995348845
58231478515974089409507253077970949157594923683005742524387610370844734671801488
76118103083043754985190983472601550494691329488083395492313850000361646482644608
492304078721818959999056496097769368017749273708962006689187956744210730
X: 3505625379966178555918512548923624458026758122
...