// copied from /mcs/mcs/codegen.cs private void SetPublicKey(AssemblyName an, byte[] strongNameBlob) { // check for possible ECMA key if (strongNameBlob.Length == 16) { // will be rejected if not "the" ECMA key an.SetPublicKey(strongNameBlob); } else { // take it, with or without, a private key RSA rsa = CryptoConvert.FromCapiKeyBlob(strongNameBlob); // and make sure we only feed the public part to Sys.Ref byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob(rsa); // AssemblyName.SetPublicKey requires an additional header byte[] publicKeyHeader = new byte [12] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00 }; byte[] encodedPublicKey = new byte [12 + publickey.Length]; Buffer.BlockCopy(publicKeyHeader, 0, encodedPublicKey, 0, 12); Buffer.BlockCopy(publickey, 0, encodedPublicKey, 12, publickey.Length); an.SetPublicKey(encodedPublicKey); } }
public void ToCapiPublicKeyBlob() { RSA rsa = RSA.Create(); // full keypair rsa.FromXmlString(strongNameString); byte[] publicKey = CryptoConvert.ToCapiPublicKeyBlob(rsa); Assert.AreEqual(BitConverter.ToString(strongNamePublicKey, 12), BitConverter.ToString(publicKey), "PublicKey-1"); // public key only rsa.FromXmlString(strongNamePublicKeyString); publicKey = CryptoConvert.ToCapiPublicKeyBlob(rsa); Assert.AreEqual(BitConverter.ToString(strongNamePublicKey, 12), BitConverter.ToString(publicKey), "PublicKey-2"); }
public byte[] ExportCspBlob(bool includePrivateParameters) { byte[] blob = null; if (includePrivateParameters) { blob = CryptoConvert.ToCapiPrivateKeyBlob(this); } else { blob = CryptoConvert.ToCapiPublicKeyBlob(this); } return(blob); }
public void ToCapiPublicKeyBlob_DSA() { DSA dsa = DSA.Create(); // full keypair dsa.FromXmlString(dsaKeyPairString); byte[] pubkey = CryptoConvert.ToCapiPublicKeyBlob(dsa); Assert.AreEqual(BitConverter.ToString(dsaPubBlob), BitConverter.ToString(pubkey), "PublicKey-1"); // public key only dsa.FromXmlString(dsaPubKeyString); pubkey = CryptoConvert.ToCapiPublicKeyBlob(dsa); Assert.AreEqual(BitConverter.ToString(dsaPubBlob), BitConverter.ToString(pubkey), "PublicKey-2"); }
public byte[] ExportCspBlob(bool includePrivateParameters) { byte[] blob = null; if (includePrivateParameters) { blob = CryptoConvert.ToCapiPrivateKeyBlob(this); } else { blob = CryptoConvert.ToCapiPublicKeyBlob(this); } // ALGID (bytes 4-7) - default is KEYX // 00 24 00 00 (for CALG_RSA_SIGN) // 00 A4 00 00 (for CALG_RSA_KEYX) blob[5] = (byte)(((store != null) && (store.Parameters.KeyNumber == AT_SIGNATURE)) ? 0x24 : 0xA4); return(blob); }
public static byte [] GetPublicKey(WriterParameters parameters) { using (var rsa = parameters.CreateRSA()) { var cspBlob = CryptoConvert.ToCapiPublicKeyBlob(rsa); var publicKey = new byte [12 + cspBlob.Length]; Buffer.BlockCopy(cspBlob, 0, publicKey, 12, cspBlob.Length); // The first 12 bytes are documented at: // http://msdn.microsoft.com/library/en-us/cprefadd/html/grfungethashfromfile.asp // ALG_ID - Signature publicKey [1] = 36; // ALG_ID - Hash publicKey [4] = 4; publicKey [5] = 128; // Length of Public Key (in bytes) publicKey [8] = (byte)(cspBlob.Length >> 0); publicKey [9] = (byte)(cspBlob.Length >> 8); publicKey [10] = (byte)(cspBlob.Length >> 16); publicKey [11] = (byte)(cspBlob.Length >> 24); return(publicKey); } }
// // Either keyFile or keyContainer has to be non-null // void LoadPublicKey(string keyFile, string keyContainer) { if (keyContainer != null) { try { private_key = new StrongNameKeyPair(keyContainer); public_key = private_key.PublicKey; } catch { Error_AssemblySigning("The specified key container `" + keyContainer + "' does not exist"); } return; } bool key_file_exists = File.Exists(keyFile); // // For attribute based KeyFile do additional lookup // in output assembly path // if (!key_file_exists && Compiler.Settings.StrongNameKeyFile == null) { // // The key file can be relative to output assembly // string test_path = Path.Combine(Path.GetDirectoryName(file_name), keyFile); key_file_exists = File.Exists(test_path); if (key_file_exists) { keyFile = test_path; } } if (!key_file_exists) { Error_AssemblySigning("The specified key file `" + keyFile + "' does not exist"); return; } using (FileStream fs = new FileStream(keyFile, FileMode.Open, FileAccess.Read)) { byte[] snkeypair = new byte[fs.Length]; fs.Read(snkeypair, 0, snkeypair.Length); // check for ECMA key if (snkeypair.Length == 16) { public_key = snkeypair; return; } try { // take it, with or without, a private key RSA rsa = CryptoConvert.FromCapiKeyBlob(snkeypair); // and make sure we only feed the public part to Sys.Ref byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob(rsa); // AssemblyName.SetPublicKey requires an additional header byte[] publicKeyHeader = new byte[8] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00 }; // Encode public key public_key = new byte[12 + publickey.Length]; Buffer.BlockCopy(publicKeyHeader, 0, public_key, 0, publicKeyHeader.Length); // Length of Public Key (in bytes) int lastPart = public_key.Length - 12; public_key[8] = (byte)(lastPart & 0xFF); public_key[9] = (byte)((lastPart >> 8) & 0xFF); public_key[10] = (byte)((lastPart >> 16) & 0xFF); public_key[11] = (byte)((lastPart >> 24) & 0xFF); Buffer.BlockCopy(publickey, 0, public_key, 12, publickey.Length); } catch { Error_AssemblySigning("The specified key file `" + keyFile + "' has incorrect format"); return; } if (delay_sign) { return; } try { // TODO: Is there better way to test for a private key presence ? CryptoConvert.FromCapiPrivateKeyBlob(snkeypair); private_key = new StrongNameKeyPair(snkeypair); } catch { } } }