Archived Forum Post

Index of archived forum posts

Question:

Public Key (RSA) fails with error P is not Prime

May 08 '13 at 18:17

I have seen the other question on this forum that indicates that a key should be regenerated. But I used the Chilkat libs to generate a new key (code below) and got the same error. I'm thinking I must be doing something wrong.

        var key = new SshKey();

        success = key.GenerateRsaKey(2048, 65537);
        if (!success)
            throw new Exception(string.Format("Chilkat failed! {0}", key.LastErrorText));

        var keystr = key.ToRfc4716PublicKey();
        if (keystr == null)
            throw new Exception(string.Format("Chilkat failed! {0}", key.LastErrorText));

        success = key.SaveText(keystr, "ShinyNewKey.pub");
        if (!success)
            throw new Exception(string.Format("Chilkat failed! {0}", key.LastErrorText));
        Console.ReadLine();
        return;

Then when I turn around and read in the key using the Chilkat funcs it fails with the p is not prime error.

Is there something obvious that I am doing wrong? Ultimately I am just trying to get the code below to work. I'm evaluating Chilkat for sftp since none of the other offerings seem to support the SSH2 public key with a user/pwd as well.

Any help is appreciated.

var pkey = key.LoadText(keyfile);
        if (pkey == null)
            throw new Exception(string.Format("Chilkat failed! {0}", key.LastErrorText));

        success = key.FromRfc4716PublicKey(pkey);            
        if (!success)
            throw new Exception(string.Format("Chilkat failed! {0}", key.LastErrorText));

        success = csftp.AuthenticatePwPk(user, pwd, key);
        if (!success)
            throw new Exception(string.Format("Chilkat failed! {0}", csftp.LastErrorText));

Accepted Answer

I found the problem: The software was trying to verify a public key as if it were a private key. The public key does not have the various parts that are present for a private key, and therefore there is no "P" to check for prime. The fix was easy -- to omit the verification for those parts.

Here's the new build: http://www.chilkatsoft.com/preRelease/ChilkatDotNet4-9.4.1-win32.zip

PS> Pre-release notes will also be logged to the Chilkat blog here: http://www.cknotes.com/?cat=371


Answer

Make sure you're using the very latest version of Chilkat.


Answer

I downloaded and ref'd the .net 4 version of the assembly today (5/7/2013) from the site. Is there a different version / place I should be accessing it from? My code is running .net 4, so I figured that was the right version for me to download.


Answer

Just noticed that you always like to see the LastErrorText (should have included that before). Here it is:

System.Exception was unhandled

HResult=-2146233088 Message=Chilkat failed! ChilkatLog: FromRfc4716PublicKey: DllDate: Dec 12 2012 UnlockPrefix: here we go.... Username: LT0050-1:edahl Architecture: Little Endian; 32-bit Language: .NET 4.0 VerboseLogging: 0 Parsed public key blob to XML. VerifyRsaKey: P is not prime. --VerifyRsaKey Failed. --FromRfc4716PublicKey --ChilkatLog

Source=EdeSample StackTrace: at EdeSample.Program.Main(String[] args) in c:Usersedahl.NWPSCDocumentsVisual Studio 2012ProjectsEdeSampleEdeSampleProgram.cs:line 80 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: