Ejemplo n.º 1
0
        /// <summary>DecodeRedirect</summary>
        /// <param name="queryString">string</param>
        /// <returns>デコードされたsaml</returns>
        public static string DecodeRedirect(string queryString)
        {
            // EcodeRedirectの逆
            // --------------------------------------------------
            // Saml → URLデコード → Base64デコード
            //   → DEFLATE解凍 → XML宣言のエンコーディング → XML
            // --------------------------------------------------
            // Samlの抽出
            string saml = "";

            if (queryString.IndexOf("SAMLRequest") != -1)
            {
                saml = StringExtractor.GetParameterFromQueryString("SAMLRequest", queryString);
            }
            else if (queryString.IndexOf("SAMLResponse") != -1)
            {
                saml = StringExtractor.GetParameterFromQueryString("SAMLResponse", queryString);
            }
            else
            {
                return("");
            }

            byte[] tempByte = DeflateCompression.Decompress(
                CustomEncode.FromBase64String(CustomEncode.UrlDecode(saml)));

            //// XML宣言部分を取得するために、us_asciiでデコード
            //string tempString = CustomEncode.ByteToString(tempByte, CustomEncode.us_ascii);

            //// エンコーディング オブジェクトの取得
            //Encoding enc = XmlLib.GetEncodingFromXmlDeclaration(tempString);

            return(CustomEncode.ByteToString(tempByte, CustomEncode.us_ascii)); // enc.CodePage);
        }
 /// <summary>MemoryStream</summary>
 private static void MemoryStream()
 {
     // DeflateCompression
     byte[] input        = CustomEncode.StringToByte(TestDeflateCompression.hoge, CustomEncode.UTF_8);
     byte[] compressed   = DeflateCompression.Compress(input);
     byte[] decompressed = DeflateCompression.Decompress(compressed);
     if (TestDeflateCompression.hoge == CustomEncode.ByteToString(decompressed, CustomEncode.UTF_8))
     {
         MyDebug.OutputDebugAndConsole("DeflateCompression(MemoryStream)", "is working properly.");
     }
     else
     {
         MyDebug.OutputDebugAndConsole("DeflateCompression(MemoryStream)", "is not working properly.");
     }
 }
        internal static string Archive(string text, StorageModes mode)
        {
            var data = Encoding.UTF8.GetBytes(text);

            byte[] archivedData = null;
            switch (mode)
            {
            case StorageModes.CompressAndEncrypt:
                archivedData = AesEncryption.Encrypt(DeflateCompression.Compress(data));
                break;

            case StorageModes.Compress:
                archivedData = DeflateCompression.Compress(data);
                break;

            case StorageModes.Store:
                archivedData = data;
                break;
            }
            var dataWithEncryptionInfo = DataWithArchivingInfo.FromUnprocessedData(archivedData, mode);

            return(Convert.ToBase64String(dataWithEncryptionInfo.Data));
        }
        internal static string Unarchive(string text)
        {
            var data = Convert.FromBase64String(text);

            byte[] unarchivedData         = null;
            var    dataWithEncryptionInfo = DataWithArchivingInfo.FromProcessedData(data);
            var    mode = dataWithEncryptionInfo.Mode;

            switch (mode)
            {
            case StorageModes.CompressAndEncrypt:
                unarchivedData = DeflateCompression.Decompress(AesEncryption.Decrypt(dataWithEncryptionInfo.Data));
                break;

            case StorageModes.Compress:
                unarchivedData = DeflateCompression.Decompress(dataWithEncryptionInfo.Data);
                break;

            case StorageModes.Store:
                unarchivedData = dataWithEncryptionInfo.Data;
                break;
            }
            return(Encoding.UTF8.GetString(unarchivedData));
        }
Ejemplo n.º 5
0
        /// <summary>EncodeAndSignRedirect</summary>
        /// <param name="type">SAML2Enum.RequestOrResponse</param>
        /// <param name="saml">string</param>
        /// <param name="relayState">string</param>
        /// <param name="dsRSAwithSHA1">DigitalSign</param>
        /// <returns>RedirectBinding用クエリ文字列</returns>
        public static string EncodeAndSignRedirect(
            SAML2Enum.RequestOrResponse type,
            string saml, string relayState,
            DigitalSign dsRSAwithSHA1 = null)
        {
            // --------------------------------------------------
            // - XML → XML宣言のエンコーディング → DEFLATE圧縮
            // -   → Base64エンコード → URLエンコード →  クエリ文字列テンプレへ組込
            // -      → コレをASCIIエンコード → 署名 → Base64エンコード
            // -         → URLエンコード →  Signatureパラメタ追加。
            // --------------------------------------------------
            // ・ヘッダも必要
            //   "<?xml version="1.0" encoding="UTF-8"?>"
            // ・クエリ文字列テンプレート
            //   ・SAMLRequest=value&RelayState=value&SigAlg=value
            //   ・SAMLResponse=value&RelayState=value&SigAlg=value
            // ・クエリ文字列署名
            //   ・クエリ文字列パラメタ値が空文字列の場合は、パラメタ自体を署名の演算から除外する。
            //   ・署名対象は上記テンプレートの文字列で、署名は、SignatureパラメタとしてURLに追加。

            string queryString         = "";
            string queryStringTemplate = "";

            #region クエリ文字列テンプレート生成
            if (string.IsNullOrEmpty(saml))
            {
                return("");
            }
            else
            {
                // 第1 QSパラメタ
                switch (type)
                {
                case SAML2Enum.RequestOrResponse.Request:
                    queryStringTemplate += "SAMLRequest={SAML}";
                    break;

                case SAML2Enum.RequestOrResponse.Response:
                    queryStringTemplate += "SAMLResponse={SAML}";
                    break;
                }

                // 第2 QSパラメタ
                if (string.IsNullOrEmpty(relayState))
                {
                    // RelayStateパラメタなし
                }
                else
                {
                    queryStringTemplate += "&RelayState={RelayState}";
                }

                // 第3 QSパラメタ
                if (dsRSAwithSHA1 == null)
                {
                    // SigAlg, Signatureパラメタなし
                }
                else
                {
                    // 第3 QSパラメタ
                    queryStringTemplate += "&SigAlg=" + CustomEncode.UrlEncode(SAML2Const.RSAwithSHA1);
                }
            }
            #endregion

            #region エンコーディング

            //// エンコーディング オブジェクトの取得
            //Encoding enc = XmlLib.GetEncodingFromXmlDeclaration(saml);

            // XML (→ XML宣言のエンコーディングではなく、asciiエンコーディングに変更)
            // → DEFLATE圧縮 → Base64エンコード → URLエンコード
            saml = CustomEncode.UrlEncode(CustomEncode.ToBase64String(
                                              DeflateCompression.Compress(CustomEncode.StringToByte(saml, CustomEncode.us_ascii))));
            #endregion

            #region 組込 & 署名

            // 署名対象となるクエリ文字列の生成(クエリ文字列のテンプレートへ組込
            queryString = queryStringTemplate;

            // - SAMLReXXXXXパラメタ
            queryString = queryString.Replace("{SAML}", saml);

            // - RelayStateパラメタ
            if (!string.IsNullOrEmpty(relayState))
            {
                queryString = queryString.Replace("{RelayState}", CustomEncode.UrlEncode(relayState));
            }

            // - Signatureパラメタ
            if (dsRSAwithSHA1 != null)
            {
                // ASCIIエンコード → 署名 → Base64エンコード → URLエンコード →  Signatureパラメタ追加。
                string signature = CustomEncode.UrlEncode(CustomEncode.ToBase64String(
                                                              dsRSAwithSHA1.Sign(CustomEncode.StringToByte(
                                                                                     queryString, CustomEncode.us_ascii)))); // enc.CodePage))));

                queryString = queryString + "&Signature=" + signature;
            }
            #endregion

            return(queryString);
        }
        /// <summary>FileStream</summary>
        private static void FileStream()
        {
            StreamWriter sr             = null;
            string       inFilePath     = "";
            string       outFilePath    = "";
            string       resultFilePath = "";

            #region outFilePathを明示しないパターン

            // inFilePathの作成
            inFilePath = Path.GetTempFileName();
            sr         = File.CreateText(inFilePath);
            sr.WriteLine(TestDeflateCompression.hoge);
            sr.Close();

            // 圧縮(outFilePath
            outFilePath = DeflateCompression.Compress(inFilePath);
            // 解凍(resultFilePath
            resultFilePath = DeflateCompression.Decompress(outFilePath);

            // ファイルの比較
            if (ResourceLoader.LoadAsString(inFilePath, Encoding.UTF8)
                == ResourceLoader.LoadAsString(resultFilePath, Encoding.UTF8))
            {
                MyDebug.OutputDebugAndConsole("DeflateCompression(FileStream)-1", "is working properly.");
            }
            else
            {
                MyDebug.OutputDebugAndConsole("DeflateCompression(FileStream)-1", "is not working properly.");
            }

            // ファイルの削除
            File.Delete(inFilePath);
            File.Delete(outFilePath);
            File.Delete(resultFilePath);
            #endregion

            #region outFilePathを明示するパターン

            // inFilePathの作成
            inFilePath = Path.GetTempFileName();
            sr         = File.CreateText(inFilePath);
            sr.WriteLine(TestDeflateCompression.hoge);
            sr.Close();

            // 圧縮(outFilePath
            outFilePath = Path.GetTempFileName();
            outFilePath = outFilePath.Remove(outFilePath.Length - 4); // 拡張し無し
            outFilePath = DeflateCompression.Compress(inFilePath, "zz", outFilePath);

            // 解凍(resultFilePath
            resultFilePath = Path.GetTempFileName();
            resultFilePath = resultFilePath.Remove(outFilePath.Length - 4) + ".dcmp"; // 拡張し有り
            resultFilePath = DeflateCompression.Decompress(outFilePath, "", resultFilePath);

            // ファイルの比較
            if (ResourceLoader.LoadAsString(inFilePath, Encoding.UTF8)
                == ResourceLoader.LoadAsString(resultFilePath, Encoding.UTF8))
            {
                MyDebug.OutputDebugAndConsole("DeflateCompression(FileStream)-2", "is working properly.");
            }
            else
            {
                MyDebug.OutputDebugAndConsole("DeflateCompression(FileStream)-2", "is not working properly.");
            }

            // ファイルの削除
            File.Delete(inFilePath);
            File.Delete(outFilePath);
            File.Delete(resultFilePath);
            #endregion
        }