在文件上点右键->属性->数字签名->时间戳,请问这个时间戳的值如何获取

解决方案 »

  1.   

    DateTime DateTime1;
    DateTime1 = System.IO.File.GetCreationTime("Z:\\C#\\shijianchuo\\123.txt");
    label1.Text = DateTime1.ToString();
      

  2.   

    DateTime DateTime1;
    DateTime1 = System.IO.File.GetCreationTime("Z:\\C#\\shijianchuo\\123.txt");
    label1.Text = DateTime1.ToString("yyyyMMdd HHmmss");
      

  3.   

    感谢各位的回答,虽然都不是我想要的,不过我现在已经找到答案了
    [StructLayout(LayoutKind.Sequential)]
            private struct BLOB
            {
                public int cbData;
                public IntPtr pbData;
            }        [StructLayout(LayoutKind.Sequential)]
            private struct CRYPT_ALGORITHM_IDENTIFIER
            {
                public String pszObjId;
                BLOB Parameters;
            }        [StructLayout(LayoutKind.Sequential)]
            private struct CERT_ID
            {
                public int dwIdChoice;
                public BLOB IssuerSerialNumberOrKeyIdOrHashId;
            }        [StructLayoutAttribute(LayoutKind.Sequential)]
            private struct SIGNER_SUBJECT_INFO
            {
                /// DWORD->unsigned int 
                public uint cbSize;            /// DWORD* 
                public System.IntPtr pdwIndex;            /// DWORD->unsigned int 
                public uint dwSubjectChoice;            /// SubjectChoiceUnion 
                public SubjectChoiceUnion Union1;
            }        [StructLayoutAttribute(LayoutKind.Explicit)]
            private struct SubjectChoiceUnion
            {            /// SIGNER_FILE_INFO* 
                [FieldOffsetAttribute(0)]
                public System.IntPtr pSignerFileInfo;            /// SIGNER_BLOB_INFO* 
                [FieldOffsetAttribute(0)]
                public System.IntPtr pSignerBlobInfo;
            }        [StructLayout(LayoutKind.Sequential)]
            private struct CERT_NAME_BLOB
            {
                public uint cbData;
                [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
                public byte[] pbData;
            }        [StructLayout(LayoutKind.Sequential)]
            private struct CRYPT_INTEGER_BLOB
            {
                public UInt32 cbData;
                public IntPtr pbData;
            }        [StructLayout(LayoutKind.Sequential)]
            private struct CRYPT_ATTR_BLOB
            {
                public uint cbData;
                [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
                public byte[] pbData;
            }        [StructLayout(LayoutKind.Sequential)]
            private struct CRYPT_ATTRIBUTE
            {
                [MarshalAs(UnmanagedType.LPStr)]
                public string pszObjId;
                public uint cValue;
                [MarshalAs(UnmanagedType.LPStruct)]
                public CRYPT_ATTR_BLOB rgValue;
            }        [StructLayout(LayoutKind.Sequential)]
            private struct CMSG_SIGNER_INFO
            {
                public int dwVersion;
                private CERT_NAME_BLOB Issuer;
                CRYPT_INTEGER_BLOB SerialNumber;
                CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm;
                CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm;
                BLOB EncryptedHash;
                CRYPT_ATTRIBUTE[] AuthAttrs;
                CRYPT_ATTRIBUTE[] UnauthAttrs;
            }        [DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern Boolean CryptQueryObject(
                int dwObjectType,
                IntPtr pvObject,
                int dwExpectedContentTypeFlags,
                int dwExpectedFormatTypeFlags,
                int dwFlags,
                out int pdwMsgAndCertEncodingType,
                out int pdwContentType,
                out int pdwFormatType,
                ref IntPtr phCertStore,
                ref IntPtr phMsg,
                ref IntPtr ppvContext);
            [DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern Boolean CryptMsgGetParam(
                IntPtr hCryptMsg,
                int dwParamType,
                int dwIndex,
                IntPtr pvData,
                ref int pcbData
            );        [DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern Boolean CryptMsgGetParam(
                IntPtr hCryptMsg,
                int dwParamType,
                int dwIndex,
                [In, Out] byte[] vData,
                ref int pcbData
            );        [DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool CryptDecodeObject(
              uint CertEncodingType,
              UIntPtr lpszStructType,
              byte[] pbEncoded,
              uint cbEncoded,
              uint flags,
              [In, Out] byte[] pvStructInfo,
              ref uint cbStructInfo);
      

  4.   

    private const int CRYPT_ASN_ENCODING = 0x00000001;
            private const int CRYPT_NDR_ENCODING = 0x00000002;
            private const int X509_ASN_ENCODING = 0x00000001;
            private const int X509_NDR_ENCODING = 0x00000002;
            private const int PKCS_7_ASN_ENCODING = 0x00010000;
            private const int PKCS_7_NDR_ENCODING = 0x00020000;        private static UIntPtr PKCS7_SIGNER_INFO = new UIntPtr(500);
            private static UIntPtr CMS_SIGNER_INFO = new UIntPtr(501);        private static string szOID_RSA_signingTime = "1.2.840.113549.1.9.5";
            private static string szOID_RSA_counterSign = "1.2.840.113549.1.9.6";        //+------------------------------------------------------------------------- 
            //  Get parameter types and their corresponding data structure definitions. 
            //-------------------------------------------------------------------------- 
            private const int CMSG_TYPE_PARAM = 1;
            private const int CMSG_CONTENT_PARAM = 2;
            private const int CMSG_BARE_CONTENT_PARAM = 3;
            private const int CMSG_INNER_CONTENT_TYPE_PARAM = 4;
            private const int CMSG_SIGNER_COUNT_PARAM = 5;
            private const int CMSG_SIGNER_INFO_PARAM = 6;
            private const int CMSG_SIGNER_CERT_INFO_PARAM = 7;
            private const int CMSG_SIGNER_HASH_ALGORITHM_PARAM = 8;
            private const int CMSG_SIGNER_AUTH_ATTR_PARAM = 9;
            private const int CMSG_SIGNER_UNAUTH_ATTR_PARAM = 10;
            private const int CMSG_CERT_COUNT_PARAM = 11;
            private const int CMSG_CERT_PARAM = 12;
            private const int CMSG_CRL_COUNT_PARAM = 13;
            private const int CMSG_CRL_PARAM = 14;
            private const int CMSG_ENVELOPE_ALGORITHM_PARAM = 15;
            private const int CMSG_RECIPIENT_COUNT_PARAM = 17;
            private const int CMSG_RECIPIENT_INDEX_PARAM = 18;
            private const int CMSG_RECIPIENT_INFO_PARAM = 19;
            private const int CMSG_HASH_ALGORITHM_PARAM = 20;
            private const int CMSG_HASH_DATA_PARAM = 21;
            private const int CMSG_COMPUTED_HASH_PARAM = 22;
            private const int CMSG_ENCRYPT_PARAM = 26;
            private const int CMSG_ENCRYPTED_DIGEST = 27;
            private const int CMSG_ENCODED_SIGNER = 28;
            private const int CMSG_ENCODED_MESSAGE = 29;
            private const int CMSG_VERSION_PARAM = 30;
            private const int CMSG_ATTR_CERT_COUNT_PARAM = 31;
            private const int CMSG_ATTR_CERT_PARAM = 32;
            private const int CMSG_CMS_RECIPIENT_COUNT_PARAM = 33;
            private const int CMSG_CMS_RECIPIENT_INDEX_PARAM = 34;
            private const int CMSG_CMS_RECIPIENT_ENCRYPTED_KEY_INDEX_PARAM = 35;
            private const int CMSG_CMS_RECIPIENT_INFO_PARAM = 36;
            private const int CMSG_UNPROTECTED_ATTR_PARAM = 37;
            private const int CMSG_SIGNER_CERT_ID_PARAM = 38;
            private const int CMSG_CMS_SIGNER_INFO_PARAM = 39;
            //------------------------------------------------------------------------- 
            //dwObjectType for CryptQueryObject 
            //------------------------------------------------------------------------- 
            private const int CERT_QUERY_OBJECT_FILE = 0x00000001;
            private const int CERT_QUERY_OBJECT_BLOB = 0x00000002;        //------------------------------------------------------------------------- 
            //dwContentType for CryptQueryObject 
            //------------------------------------------------------------------------- 
            //encoded single certificate 
            private const int CERT_QUERY_CONTENT_CERT = 1;
            //encoded single CTL 
            private const int CERT_QUERY_CONTENT_CTL = 2;
            //encoded single CRL 
            private const int CERT_QUERY_CONTENT_CRL = 3;
            //serialized store 
            private const int CERT_QUERY_CONTENT_SERIALIZED_STORE = 4;
            //serialized single certificate 
            private const int CERT_QUERY_CONTENT_SERIALIZED_CERT = 5;
            //serialized single CTL 
            private const int CERT_QUERY_CONTENT_SERIALIZED_CTL = 6;
            //serialized single CRL 
            private const int CERT_QUERY_CONTENT_SERIALIZED_CRL = 7;
            //a PKCS#7 signed message 
            private const int CERT_QUERY_CONTENT_PKCS7_SIGNED = 8;
            //a PKCS#7 message, such as enveloped message.  But it is not a signed message, 
            private const int CERT_QUERY_CONTENT_PKCS7_UNSIGNED = 9;
            //a PKCS7 signed message embedded in a file 
            private const int CERT_QUERY_CONTENT_PKCS7_SIGNED_EMBED = 10;
            //an encoded PKCS#10 
            private const int CERT_QUERY_CONTENT_PKCS10 = 11;
            //an encoded PKX BLOB 
            private const int CERT_QUERY_CONTENT_PFX = 12;
            //an encoded CertificatePair (contains forward and/or reverse cross certs) 
            private const int CERT_QUERY_CONTENT_CERT_PAIR = 13;        //------------------------------------------------------------------------- 
            //dwExpectedConentTypeFlags for CryptQueryObject 
            //------------------------------------------------------------------------- 
            //encoded single certificate 
            private const int CERT_QUERY_CONTENT_FLAG_CERT = (1 << CERT_QUERY_CONTENT_CERT);        //encoded single CTL 
            private const int CERT_QUERY_CONTENT_FLAG_CTL = (1 << CERT_QUERY_CONTENT_CTL);        //encoded single CRL 
            private const int CERT_QUERY_CONTENT_FLAG_CRL = (1 << CERT_QUERY_CONTENT_CRL);        //serialized store 
            private const int CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE = (1 << CERT_QUERY_CONTENT_SERIALIZED_STORE);        //serialized single certificate 
            private const int CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT = (1 << CERT_QUERY_CONTENT_SERIALIZED_CERT);        //serialized single CTL 
            private const int CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL = (1 << CERT_QUERY_CONTENT_SERIALIZED_CTL);        //serialized single CRL 
            private const int CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL = (1 << CERT_QUERY_CONTENT_SERIALIZED_CRL);        //an encoded PKCS#7 signed message 
            private const int CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED = (1 << CERT_QUERY_CONTENT_PKCS7_SIGNED);        //an encoded PKCS#7 message.  But it is not a signed message 
            private const int CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED = (1 << CERT_QUERY_CONTENT_PKCS7_UNSIGNED);        //the content includes an embedded PKCS7 signed message 
            private const int CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED = (1 << CERT_QUERY_CONTENT_PKCS7_SIGNED_EMBED);        //an encoded PKCS#10 
            private const int CERT_QUERY_CONTENT_FLAG_PKCS10 = (1 << CERT_QUERY_CONTENT_PKCS10);        //an encoded PFX BLOB 
            private const int CERT_QUERY_CONTENT_FLAG_PFX = (1 << CERT_QUERY_CONTENT_PFX);        //an encoded CertificatePair (contains forward and/or reverse cross certs) 
            private const int CERT_QUERY_CONTENT_FLAG_CERT_PAIR = (1 << CERT_QUERY_CONTENT_CERT_PAIR);        //content can be any type 
            private const int CERT_QUERY_CONTENT_FLAG_ALL =
                CERT_QUERY_CONTENT_FLAG_CERT |
                CERT_QUERY_CONTENT_FLAG_CTL |
                CERT_QUERY_CONTENT_FLAG_CRL |
                CERT_QUERY_CONTENT_FLAG_SERIALIZED_STORE |
                CERT_QUERY_CONTENT_FLAG_SERIALIZED_CERT |
                CERT_QUERY_CONTENT_FLAG_SERIALIZED_CTL |
                CERT_QUERY_CONTENT_FLAG_SERIALIZED_CRL |
                CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED |
                CERT_QUERY_CONTENT_FLAG_PKCS7_UNSIGNED |
                CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED |
                CERT_QUERY_CONTENT_FLAG_PKCS10 |
                CERT_QUERY_CONTENT_FLAG_PFX |
                CERT_QUERY_CONTENT_FLAG_CERT_PAIR;
      

  5.   

    private const int CERT_QUERY_FORMAT_BINARY = 1;        //the content is base64 encoded 
            private const int CERT_QUERY_FORMAT_BASE64_ENCODED = 2;        //the content is ascii hex encoded with "{ASN}" prefix 
            private const int CERT_QUERY_FORMAT_ASN_ASCII_HEX_ENCODED = 3;        //------------------------------------------------------------------------- 
            //dwExpectedFormatTypeFlags for CryptQueryObject 
            //------------------------------------------------------------------------- 
            //the content is in binary format 
            private const int CERT_QUERY_FORMAT_FLAG_BINARY = (1 << CERT_QUERY_FORMAT_BINARY);        //the content is base64 encoded 
            private const int CERT_QUERY_FORMAT_FLAG_BASE64_ENCODED = (1 << CERT_QUERY_FORMAT_BASE64_ENCODED);        //the content is ascii hex encoded with "{ASN}" prefix 
            private const int CERT_QUERY_FORMAT_FLAG_ASN_ASCII_HEX_ENCODED = (1 << CERT_QUERY_FORMAT_ASN_ASCII_HEX_ENCODED);        //the content can be of any format 
            private const int CERT_QUERY_FORMAT_FLAG_ALL =
                CERT_QUERY_FORMAT_FLAG_BINARY |
                CERT_QUERY_FORMAT_FLAG_BASE64_ENCODED |
                CERT_QUERY_FORMAT_FLAG_ASN_ASCII_HEX_ENCODED;        public static string GetTimestamp(string fileName)
            {
                try
                {
                    int encodingType;
                    int contentType;
                    int formatType;
                    IntPtr certStore = IntPtr.Zero;
                    IntPtr cryptMsg = IntPtr.Zero;
                    IntPtr context = IntPtr.Zero;                if (!CryptQueryObject(
                        CERT_QUERY_OBJECT_FILE,
                        Marshal.StringToHGlobalUni(fileName),
                        CERT_QUERY_CONTENT_FLAG_ALL,
                        CERT_QUERY_FORMAT_FLAG_ALL,
                        0,
                        out encodingType,
                        out contentType,
                        out formatType,
                        ref certStore,
                        ref cryptMsg,
                        ref context))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }
                    int cbData = 0;
                    if (!CryptMsgGetParam(
                        cryptMsg,
                        CMSG_ENCODED_MESSAGE,//Crypt32.CMSG_SIGNER_INFO_PARAM, 
                        0,
                        IntPtr.Zero,
                        ref cbData))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }                var vData = new byte[cbData];                // Get the encoded message. 
                    if (!CryptMsgGetParam(
                        cryptMsg,
                        CMSG_ENCODED_MESSAGE,//Crypt32.CMSG_SIGNER_INFO_PARAM, 
                        0,
                        vData,
                        ref cbData))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }                var signedCms = new SignedCms();
                    signedCms.Decode(vData);                foreach (var signerInfo in signedCms.SignerInfos)
                    {
                        foreach (var unsignedAttribute in signerInfo.UnsignedAttributes)
                        {
                            if (unsignedAttribute.Oid.Value == szOID_RSA_counterSign)
                            {
                                foreach (var counterSignInfo in signerInfo.CounterSignerInfos)
                                {
                                    foreach (var signedAttribute in counterSignInfo.SignedAttributes)
                                    {
                                        if (signedAttribute.Oid.Value == szOID_RSA_signingTime)
                                        {
                                            Pkcs9SigningTime signingTime = (Pkcs9SigningTime)signedAttribute.Values[0];
                                            return signingTime.SigningTime.AddHours(8).ToString();
                                        }
                                    }
                                }
                            }
                        }
                    }            }
                catch (Exception)
                {
                    return "";
                }
                return "";
            }
        }