Github上的弱密钥错误,用C#生成的4096 RSAkey

问题描述:

我正在根据RFC4716(或者至少我认为是这样)使用C#和标准密码库生成一个4096位的RSA KEY,但是git hub说我拥有一个键的大小错误,当我尝试将其添加到与我的帐户关联的键时返回以下错误。Github上的弱密钥错误,用C#生成的4096 RSAkey

Key is weak. GitHub recommends using ssh-keygen to generate a RSA key of at least 2048 bits.

这是生成代码的关键是:

public static void GenerateKeys() 
    { 
     // Create the CspParameters object and set the key container 
     // name used to store the RSA key pair. 
     CspParameters cp = new CspParameters(); 
     //cp.KeyContainerName = ContainerName; 

     CspKeyContainerInfo info = new CspKeyContainerInfo(cp); 
     //string filename = info.UniqueKeyContainerName; 

     // Create a new instance of RSACryptoServiceProvider that accesses 
     // the key container MyKeyContainerName. 
     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(4096, cp); 
     var test = rsa.KeySize; 

     using (StreamWriter privateKeyWriter = new StreamWriter(GitStandard.PrivateSSHKeyPath)) 
     { 
      ExportPrivateKey(rsa, privateKeyWriter); 
     } 

     using (StreamWriter publicKeyWriter = new StreamWriter(GitStandard.PublicSSHKeyPath)) 
     { 

      ExportPublicKeyOpenSSH(rsa, publicKeyWriter); 
     } 
    } 

方法ExportPublicKeyOpenSSH是代码的小改在this thread with answers on how to convert the key to RFC4716发现,我做不同的唯一的事情就是增加一个零(0)在模数转换之前。

private static void ExportPublicKeyOpenSSH(RSACryptoServiceProvider csp, TextWriter outputStream) 
     { 
      var parameters = csp.ExportParameters(false); 


      byte[] sshrsa_bytes = Encoding.Default.GetBytes("ssh-rsa"); 
      //initializing modulus array 
      byte[] n = new Byte[parameters.Modulus.Length + 1]; 
      //adding initial zero before modulus to conform with OpenSSH 
      n[0] = 0; 
      System.Buffer.BlockCopy(parameters.Modulus, 0, n, 1, parameters.Modulus.Length); 
      //byte[] n = parameters.Modulus; 
      byte[] e = parameters.Exponent; 
      System.Array.Resize<Byte>(ref n, n.Length + 1); 
      string base64; 
      using (var stream = new MemoryStream()) 
      { 
       stream.Write(ToBytes(sshrsa_bytes.Length), 0, 4); 
       stream.Write(sshrsa_bytes, 0, sshrsa_bytes.Length); 
       stream.Write(ToBytes(e.Length), 0, 4); 
       stream.Write(e, 0, e.Length); 
       stream.Write(ToBytes(n.Length), 0, 4); 
       stream.Write(n, 0, n.Length); 
       stream.Flush(); 
       base64 = Convert.ToBase64String(stream.ToArray()); 
      } 
      var result = string.Format("ssh-rsa {0}", base64); 
      outputStream.Write(result); 
     } 

产生什么关键的模样

支持SSH-RSA AAAAB3NzaC1yc2EAAAADAQABAAACAgD171Y9VeinRALRfU8adS2K0vYHGfKkQwqs8SOQbhURFNtazupsocmpW96dYF346UVVCiKQCYrCW6t0QpGE3ch7onqTvXBszA9mfcuLX9hlhesJqFyUTHxDUopCc2tc5fWYuZ4MeySKuOetBEmPfN3Eu + SWC8j3VS9YzIDjwhkBPcJoxOnv3l7pSxEzGBGQXwdGmL8TFxxsBhue1ajralYPXgJo1nra70ChHcr8PfJvIXigBYCkwnb0KuofbPyhHETo4fNJqCPa1rLjnKoz5iTpyak2SWnhD5FX0/t4juaL/OKNE4YSaAqpWwA9VS1i + y7doeSRc22tm5LHgSLmlxg6h5lPKm5emB840eMLOPvZLS/4uODzFPMo4NFC2ZwNwdlXhcQE9EVtz9EZox1isKpJgShqJPh0sHVH9RnCuBSxW5N79KtsvcXI2zAiLBczKukqU2rTkvYdV1Wkx4zHSvLe42PQuJvSwhwW1tlgyFemd2aRwGDltQyGT PNOZ28E6SGgvxYtB4nvcu8gLyxob4Hz3ysohDB0Z9ZEismSK/8eSeMrBPosTBO77tsjUk1L8v2lHXQ + p1raLpd3ETeae7vZjt6zMFCIhNKDvdJL9b0mIKLB26PMhWG4DzSTJGeIANjiNryWK7y0gdgdPs5953H1EJVRQ0wd2ceFFg2 + kpqlrQA =

使用命令ssh-keygen -l -f custom_rsa.pub测试密钥的有效性。

$ ssh-keygen -l -f custom_rsa.pub 
4104 SHA256:uGO4sHOXXuX1waf+8jrdsWr3/57npF5AuUKUgYVWbCI no comment (RSA) 
+2

您调整'n'以在左侧添加0(通过从索引1开始手动复制)并在右侧添加0(Array.Resize)。后者可能会让你陷入困境。此外,不要使用Encoding.Default,而是使用任何你想要的编码。'Encoding.ASCII',可能。 – bartonjs

+0

@bartonjs你完全正确。我应该注意到这一点,谢谢。你能发表答复吗? – Anika

您调整n既左边增加一个0(通过手动复制到它开始于索引1)和到右侧增加一个0(经由Array.Resize)。后者可能会让你陷入困境。

此外,(无关)你可能不应该使用Encoding.Default,而是你想要的任何编码。大概是。