Esempio n. 1
0
        /// <summary>デジタル署名を作成する</summary>
        /// <param name="data">デジタル署名を行なう対象データ</param>
        /// <returns>対象データに対してデジタル署名したデジタル署名部分のデータ</returns>
        public override byte[] Sign(byte[] data)
        {
            // ハッシュ
            byte[] hashedByte = this.HashAlgorithm.ComputeHash(data);
            // デジタル署名
            byte[] signedByte = null;

            if (this.AsymmetricAlgorithm is RSACryptoServiceProvider)
            {
                // RSAPKCS1SignatureFormatterオブジェクトを作成
                RSAPKCS1SignatureFormatter rsaFormatter = new RSAPKCS1SignatureFormatter(this.AsymmetricAlgorithm);

                rsaFormatter.SetHashAlgorithm(
                    RsaAndDsaCmnFunc.GetHashAlgorithmName(this.HashAlgorithm));
                signedByte = rsaFormatter.CreateSignature(hashedByte);
            }
            else if (this.AsymmetricAlgorithm is DSACryptoServiceProvider)
            {
                // DSASignatureFormatterオブジェクトを作成
                DSASignatureFormatter dsaFormatter = new DSASignatureFormatter(this.AsymmetricAlgorithm);

                // デジタル署名を作成
                dsaFormatter.SetHashAlgorithm("SHA1");
                signedByte = dsaFormatter.CreateSignature(hashedByte);
            }

            return(signedByte);
        }
Esempio n. 2
0
        /// <summary>Constructor</summary>
        /// <param name="eaa">EnumDigitalSignAlgorithm</param>
        public DigitalSignParam(EnumDigitalSignAlgorithm eaa)
        {
            AsymmetricAlgorithm aa = null;
            HashAlgorithm       ha = null;

            RsaAndDsaCmnFunc.CreateDigitalSignServiceProvider(eaa, out aa, out ha);

            this.AsymmetricAlgorithm = aa;
            this.HashAlgorithm       = ha;
        }
Esempio n. 3
0
 /// <summary>デジタル署名を検証する</summary>
 /// <param name="data">デジタル署名を行なった対象データ</param>
 /// <param name="sign">対象データに対してデジタル署名したデジタル署名部分のデータ</param>
 /// <returns>検証結果( true:検証成功, false:検証失敗 )</returns>
 public override bool Verify(byte[] data, byte[] sign)
 {
     if (this.AsymmetricAlgorithm is RSACryptoServiceProvider)
     {
         return(((RSACryptoServiceProvider)this.AsymmetricAlgorithm).
                VerifyData(data, RsaAndDsaCmnFunc.GetHashAlgorithmName(this.HashAlgorithm), sign));
     }
     else
     {
         return(((DSACryptoServiceProvider)this.AsymmetricAlgorithm).
                VerifyData(data, sign));
     }
 }
Esempio n. 4
0
        /// <summary>Constructor</summary>
        /// <param name="eaa">EnumDigitalSignAlgorithm</param>
        public DigitalSignXML(EnumDigitalSignAlgorithm eaa)
        {
            AsymmetricAlgorithm aa = null;
            HashAlgorithm       ha = null;

            RsaAndDsaCmnFunc.CreateDigitalSignServiceProvider(eaa, out aa, out ha);

            this.AsymmetricAlgorithm = aa;
            this.HashAlgorithm       = ha;

            // 秘密鍵をXML形式で取得
            this.XMLPrivateKey = this.AsymmetricAlgorithm.ToXmlString(true);
            // 公開鍵をXML形式で取得
            this.XMLPublicKey = this.AsymmetricAlgorithm.ToXmlString(false);
        }
Esempio n. 5
0
        /// <summary>デジタル署名を検証する</summary>
        /// <param name="data">デジタル署名を行なった対象データ</param>
        /// <param name="sign">対象データに対してデジタル署名したデジタル署名部分のデータ</param>
        /// <returns>検証結果( true:検証成功, false:検証失敗 )</returns>
        public override bool Verify(byte[] data, byte[] sign)
        {
            //// XMLPublicKeyプロパティ・プロシージャ(set)に移動
            //this.AsymmetricAlgorithm.FromXmlString(this.XMLPublicKey);

            if (this.AsymmetricAlgorithm is RSACryptoServiceProvider)
            {
                return(((RSACryptoServiceProvider)this.AsymmetricAlgorithm).VerifyData(
                           data, RsaAndDsaCmnFunc.GetHashAlgorithmName(this.HashAlgorithm), sign));
            }
            else
            {
                return(((DSACryptoServiceProvider)this.AsymmetricAlgorithm).VerifyData(data, sign));
            }
        }
Esempio n. 6
0
        /// <summary>公開鍵・暗号化サービスプロバイダの生成(param)</summary>
        /// <param name="param">
        /// - RSAParameters
        /// - DSAParameters
        /// </param>
        /// <param name="ha">HashAlgorithm(使用可能かチェック)</param>
        /// <returns>
        /// AsymmetricAlgorithm
        /// - RSACryptoServiceProvider
        /// - DSACryptoServiceProvider
        /// </returns>
        public static AsymmetricAlgorithm CreateAsymmetricAlgorithmFromParam(object param, HashAlgorithm ha)
        {
            if (param is RSAParameters)
            {
                // RSACryptoServiceProvider
                RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider();
                rsaCryptoServiceProvider.ImportParameters((RSAParameters)param);

                // HashAlgorithm
                string temp = RsaAndDsaCmnFunc.GetHashAlgorithmName(ha);
                if ("MD5, SHA1, SHA256, SHA384, SHA512".IndexOf(temp) != -1)
                {
                    return(rsaCryptoServiceProvider);
                }
                else
                {
                    throw new ArgumentException(
                              PublicExceptionMessage.ARGUMENT_INCORRECT,
                              "The hash algorithm parameter of rsa is incorrect.");
                }
            }
            else if (param is DSAParameters)
            {
                // DSACryptoServiceProvider
                DSACryptoServiceProvider dsaCryptoServiceProvider = new DSACryptoServiceProvider();
                dsaCryptoServiceProvider.ImportParameters((DSAParameters)param);

                // HashAlgorithm
                string temp = RsaAndDsaCmnFunc.GetHashAlgorithmName(ha);
                if (temp == "SHA1")
                {
                    return(dsaCryptoServiceProvider);
                }
                else
                {
                    throw new ArgumentException(
                              PublicExceptionMessage.ARGUMENT_INCORRECT,
                              "The hash algorithm parameter of dsa is incorrect.");
                }
            }
            else
            {
                throw new ArgumentException(
                          PublicExceptionMessage.ARGUMENT_INCORRECT,
                          "The algorithm parameters is incorrect.");
            }
        }
Esempio n. 7
0
        /// <summary>Constructor</summary>
        /// <param name="eaa">EnumDigitalSignAlgorithm</param>
        /// <param name="xmlKey">string</param>
        public DigitalSignXML(EnumDigitalSignAlgorithm eaa, string xmlKey)
        {
            AsymmetricAlgorithm aa = null;
            HashAlgorithm       ha = null;

            RsaAndDsaCmnFunc.CreateDigitalSignServiceProvider(eaa, out aa, out ha);

            if (aa is RSACryptoServiceProvider)
            {
                RSACryptoServiceProvider rsaCryptoServiceProvider = (RSACryptoServiceProvider)aa;
                rsaCryptoServiceProvider.FromXmlString(xmlKey);
                this.AsymmetricAlgorithm = rsaCryptoServiceProvider;
            }
            else if (aa is DSACryptoServiceProvider)
            {
                DSACryptoServiceProvider dsaCryptoServiceProvider = (DSACryptoServiceProvider)aa;
                dsaCryptoServiceProvider.FromXmlString(xmlKey);
                this.AsymmetricAlgorithm = dsaCryptoServiceProvider;
            }

            this.HashAlgorithm = ha;

            // 秘密鍵をXML形式で取得
            try
            {
                this.XMLPrivateKey = this.AsymmetricAlgorithm.ToXmlString(true);
            }
            catch (CryptographicException cex)
            {
                cex.GetType();
                // 潰す(xmlKeyが公開鍵のケース)
            }

            // 公開鍵をXML形式で取得
            this.XMLPublicKey = this.AsymmetricAlgorithm.ToXmlString(false);
        }
Esempio n. 8
0
 /// <summary>Constructor</summary>
 /// <param name="param">object</param>
 /// <param name="ha">HashAlgorithm</param>
 public DigitalSignParam(object param, HashAlgorithm ha)
 {
     this.AsymmetricAlgorithm = RsaAndDsaCmnFunc.CreateAsymmetricAlgorithmFromParam(param, ha);
     this.HashAlgorithm       = ha;
 }