public void ToCapiKeyBlob_DSA() { DSA dsa = DSA.Create(); dsa.FromXmlString(dsaKeyPairString); byte[] keypair = CryptoConvert.ToCapiKeyBlob(dsa, true); AssertEquals("KeyPair", dsaPrivBlob, keypair); byte[] pubkey = CryptoConvert.ToCapiKeyBlob(dsa, false); Assert.AreEqual(BitConverter.ToString(dsaPubBlob), BitConverter.ToString(pubkey), "PublicKey"); }
public void ToCapiKeyBlob_RSA() { RSA rsa = RSA.Create(); rsa.FromXmlString(strongNameString); byte[] keypair = CryptoConvert.ToCapiKeyBlob(rsa, true); AssertEquals("KeyPair", strongName, keypair); byte[] publicKey = CryptoConvert.ToCapiKeyBlob(rsa, false); Assert.AreEqual(BitConverter.ToString(strongNamePublicKey, 12), BitConverter.ToString(publicKey), "PublicKey"); }
public void ToCapiKeyBlob_AsymmetricAlgorithm() { AsymmetricAlgorithm rsa = RSA.Create(); rsa.FromXmlString(strongNameString); byte[] keypair = CryptoConvert.ToCapiKeyBlob(rsa, true); AssertEquals("RSA-KeyPair", strongName, keypair); byte[] publicKey = CryptoConvert.ToCapiKeyBlob(rsa, false); Assert.AreEqual(BitConverter.ToString(strongNamePublicKey, 12), BitConverter.ToString(publicKey), "RSA-PublicKey"); AsymmetricAlgorithm dsa = DSA.Create(); dsa.FromXmlString(dsaKeyPairString); AssertEquals("DSA-KeyPair", dsaPrivBlob, CryptoConvert.ToCapiKeyBlob(dsa, true)); Assert.AreEqual(BitConverter.ToString(dsaPubBlob), BitConverter.ToString(CryptoConvert.ToCapiKeyBlob(dsa, false)), "DSA-PublicKey"); }
public void ToCapiKeyBlob_DSANull() { DSA dsa = null; CryptoConvert.ToCapiKeyBlob(dsa, false); }
public void ToCapiKeyBlob_RSANull() { RSA rsa = null; CryptoConvert.ToCapiKeyBlob(rsa, false); }
public void ToCapiKeyBlob_AsymmetricNull() { AsymmetricAlgorithm aa = null; CryptoConvert.ToCapiKeyBlob(aa, false); }
static int Process(string[] args) { int i = 0; string param = args [i]; bool quiet = ((param == "-quiet") || (param == "-q")); if (quiet) { i++; } else { Header(); } LoadConfig(quiet); StrongName sn = null; AssemblyName an = null; RSACryptoServiceProvider rsa = null; CspParameters csp = new CspParameters(); csp.ProviderName = defaultCSP; switch (args [i++]) { case "-c": // Change global CSP provider options defaultCSP = args [i]; return(SaveConfig()); case "-d": // Delete specified key container csp.KeyContainerName = args [i]; rsa = new RSACryptoServiceProvider(csp); rsa.PersistKeyInCsp = false; if (!quiet) { Console.WriteLine("Keypair in container {0} has been deleted", args [i]); } break; case "-D": StrongName a1 = new StrongName(); byte[] h1 = a1.Hash(args [i++]); StrongName a2 = new StrongName(); byte[] h2 = a2.Hash(args [i++]); if (Compare(h1, h2)) { Console.WriteLine("Both assembly are identical (same digest for metadata)"); // TODO: if equals then compare signatures } else { Console.WriteLine("Assemblies are not identical (different digest for metadata)"); } break; case "-e": // Export public key from assembly an = AssemblyName.GetAssemblyName(args [i++]); WriteToFile(args[i], an.GetPublicKey()); if (!quiet) { Console.WriteLine("Public Key extracted to file {0}", args [i]); } break; case "-i": // import keypair from SNK to container sn = new StrongName(ReadFromFile(args [i++])); csp.KeyContainerName = args [i]; rsa = new RSACryptoServiceProvider(csp); rsa.ImportParameters(sn.RSA.ExportParameters(true)); break; case "-k": // Create a new strong name key pair // (a new RSA keypair automagically if none is present) int size = 1024; if (i < args.Length + 2) { try { size = Int32.Parse(args[i++]); } catch { // oops, that wasn't a valid key size (assume 1024 bits) i--; } } sn = new StrongName(size); WriteToFile(args[i], CryptoConvert.ToCapiKeyBlob(sn.RSA, true)); if (!quiet) { Console.WriteLine("A new {0} bits strong name keypair has been generated in file '{1}'.", size, args [i]); } break; case "-m": Console.WriteLine("Unimplemented option"); break; case "-o": byte[] infileD = ReadFromFile(args [i++]); WriteCSVToFile(args [i], infileD, "D"); if (!quiet) { Console.WriteLine("Output CSV file is {0} (decimal format)", args [i]); } break; case "-oh": byte[] infileX2 = ReadFromFile(args [i++]); WriteCSVToFile(args [i], infileX2, "X2"); if (!quiet) { Console.WriteLine("Output CVS file is {0} (hexadecimal format)", args [i]); } break; case "-p": // Extract public key from SNK or PKCS#12/PFX file sn = new StrongName(GetKeyFromFile(args [i++])); WriteToFile(args[i], sn.PublicKey); if (!quiet) { Console.WriteLine("Public Key extracted to file {0}", args [i]); } break; case "-pc": // Extract public key from container csp.KeyContainerName = args [i++]; rsa = new RSACryptoServiceProvider(csp); sn = new StrongName(rsa); WriteToFile(args[i], sn.PublicKey); if (!quiet) { Console.WriteLine("Public Key extracted to file {0}", args [i]); } break; case "-R": string filename = args [i++]; if (!ReSign(filename, GetKeyFromFile(args [i]))) { return(1); } break; case "-Rc": filename = args [i++]; csp.KeyContainerName = args [i]; rsa = new RSACryptoServiceProvider(csp); if (!ReSign(filename, rsa)) { return(1); } break; case "-t": // Show public key token from file sn = new StrongName(ReadFromFile(args [i])); // note: ignore quiet Console.WriteLine("Public Key Token: " + ToString(sn.PublicKeyToken), Environment.NewLine); break; case "-tp": // Show public key and public key token from assembly sn = new StrongName(ReadFromFile(args [i])); // note: ignore quiet Console.WriteLine("Public Key:" + ToString(sn.PublicKey)); Console.WriteLine("{0}Public Key Token: " + ToString(sn.PublicKeyToken), Environment.NewLine); break; case "-T": // Show public key token from assembly an = AssemblyName.GetAssemblyName(args [i++]); // note: ignore quiet byte [] pkt = an.GetPublicKeyToken(); if (pkt == null) { Console.WriteLine("{0} does not represent a strongly named assembly.", args [i - 1]); } else { Console.WriteLine("Public Key Token: " + ToString(pkt)); } break; case "-Tp": // Show public key and public key token from assembly an = AssemblyName.GetAssemblyName(args [i++]); byte [] token = an.GetPublicKeyToken(); if (token == null) { Console.WriteLine("{0} does not represent a strongly named assembly.", args [i - 1]); } else { Console.WriteLine("Public Key:" + ToString(an.GetPublicKey())); Console.WriteLine("{0}Public Key Token: " + ToString(token), Environment.NewLine); } break; case "-v": filename = args [i++]; return(Verify(filename, false)); case "-vf": filename = args [i++]; return(Verify(filename, true)); // force verification case "-Vl": Console.WriteLine(new StrongNameManager().ToString()); break; case "-Vr": Console.WriteLine("Unimplemented option"); break; case "-Vu": Console.WriteLine("Unimplemented option"); break; case "-Vx": // we must remove <verificationSettings> from each config files Console.WriteLine("Unimplemented option"); break; case "-?": case "-h": Help((i < args.Length) ? args [i] : null); break; default: if (!quiet) { Console.WriteLine("Unknown option {0}", args [i - 1]); } return(1); } return(0); }