+1 vote
by (200 points)

Hi,

I'm trying to create a SshPublicKey object from a public key string that has been generated using the ssh-keygen command in a cmd. I get an exception that says "Error while decoding key. ---> System.InvalidOperationException: Data too long."

this is the code I'm using:

        var stream = new MemoryStream();
        var writer = new StreamWriter(stream);
        writer.Write(publicKeyString);
        writer.Flush();
        stream.Position = 0;

        return new SshPublicKey(stream);

The public key that was generated is in this format:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJrQZgKrcqeRsSSpTR4J5w013Ax2ZThIWhIZrSravuRo3XojUxunwDVoTku3PvJGbcL8sxFfCHI70myCk/6S7TK2rwUAmQoEJxV+DGwldBpkRoPRHy8eK31CDLHbkPpCIYJzacoteXZQXztLSKnBDqyhvQ5ILSTlnsYgNrJxoyDPUumXoD3ZAi7ReSOqDOqxOwkT5Nr9I7u1AYv4mDx8kStnWYQBBVbdSNADJJMQ/YG85Rhe4hS3B5zt/8TB+w/EIvej9Fh6+qDTVbgt+J/JrL6TL+EFXP+mtJKVTVYo09Nk5BA30V16ect48CGmvas+NPyn4k/ZYNH4TJRbk4YzvR corp\mattias.andries@PF-MAndries1

However a public key that is in this format does seem to work:

---- BEGIN SSH2 PUBLIC KEY ----
Comment: ""
AAAAB3NzaC1yc2EAAAABJQAAAQEAg8+3TU/U8OSEocpEH/V1s4BvtjrHt1li5uH3
GBU4EzpC4cgVQUBXliWwQhVQcseSpwtBoK6n/WN+LRGqdBzILkl9ahFj1KXi9zW1
dMeSEIElU6R7Tb9wBBVFUng+U0fiYt47QmfiONgEXm70ZrrHXPKKnu/XG3PboCDI
7IzhWSzrbTGcFTj3c/ZkJVIZkiZnOzvMD0SWXEdkD0mKAuoYunBYucSILjYbnL/P
AaftEwZtu8/GdS4hWseNBM3fv8xDBF/7Z5CzNAlDTrGVaKQqThMTlKzxcalsmzvL
gwBvITPypX5VIzrVYAXPHirR0+tAhr+/wbcO23WP662N0GwIHw==
---- END SSH2 PUBLIC KEY ----

Any ideas on why the first key format gets the exception but the second key works fine?

Thanks.

1 Answer

0 votes
by (147k points)
selected by
 
Best answer

Hi,

The first format is the format of OpenSSH's authenticated_keys file, which is supposed to contain a list of public keys, and can be loaded using SshPublicKey.LoadPublicKeys static method.

by (200 points)
So the reason why I asked is because clients send us their public keys for us to store in a database that we retrieve when they try to log in to check against their private key. We save the public keys as strings.

Would I have to retrieve the public key, save it in a file, then call the SshPublicKey.LoadPublicKeys method? Does this work with the second format as well? Our clients use all kinds of different formats and we should support all of them.

Thanks.
by (147k points)
We can make the SshPublicKey constructor accept this format as well, but then what about authorized_keys with multiple entries? Throw an exception? Only load the first one? None if this looks like a good option... Or perhaps it would be better to make SshPublicKey.LoadPublicKeys accept the other stand-alone key formats as well? We'll consider this for one of the next releases.
by (200 points)
Thanks! We'll work around it for now. Appreciate the help
by (750 points)
Specify an  index parameter to select the key or just support one key at a time.
...