ヘッダー
C# サンプル集
 

Base64Urlをデコードする

2023/1/1

→ Visual Basic のサンプルに切り替える → Python のサンプルに切り替える

 

Base64UrlをUTF-8の文字列をデコードする

string base64 = "5b6z5bed5a625bq3QUJD";

//まず通常のBase64に変換する
int padCount = (base64.Length % 4) switch { 0 => 0, 2 => 2, _ => 1 };
base64 = base64.Replace("-", "+").Replace("_", "/") + new string('=', padCount);

//元の値に変換する
string value = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(base64));

System.Diagnostics.Debug.WriteLine(value); //徳川家康ABC

Debug.WriteLineが表示される場所

 

これでもできます。

string base64 = "5b6z5bed5a625bq3QUJD";
string value = System.Text.Encoding.UTF8.GetString(Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Decode(base64));

System.Diagnostics.Debug.WriteLine(value); //徳川家康ABC

Debug.WriteLineが表示される場所

メモ:ASP.NET以外で、この例を実行するには NuGet で Microsoft.AspNetCore.WebUtilities をインストールする必要があります。 → NuGetでパッケージをインストールする方法

 

 

Base64UrlをShift_JISの文字列にデコードする

#if NETCOREAPP
//.NET CoreでShift_JISなど追加のエンコーディングを扱うために必要です。
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif

string base64 = "k7-Q7InGjU5BQkM";

//まず通常のBase64に変換する
int padCount = (base64.Length % 4) switch { 0 => 0, 2 => 2, _ => 1 };
base64 = base64.Replace("-", "+").Replace("_", "/") + new string('=', padCount);

//元の値に変換する
string value = System.Text.Encoding.GetEncoding("Shift_JIS").GetString(Convert.FromBase64String(base64));

System.Diagnostics.Debug.WriteLine(value); //徳川家康ABC

Debug.WriteLineが表示される場所

 

これでもできます。

#if NETCOREAPP
//.NET CoreでShift_JISなど追加のエンコーディングを扱うために必要です。
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif

string base64 = "k7-Q7InGjU5BQkM";
string value = System.Text.Encoding.GetEncoding("Shift_JIS").GetString(Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Decode(base64));

System.Diagnostics.Debug.WriteLine(value); //徳川家康ABC

Debug.WriteLineが表示される場所

メモ:ASP.NET以外で、この例を実行するには NuGet で Microsoft.AspNetCore.WebUtilities をインストールする必要があります。 → NuGetでパッケージをインストールする方法

 

 

Base64Urlをオブジェクトにデコードする

オブジェクトの一例として、Base64Url を List<string> にデコードする例。

string base64 = "WyJBcHBsZSIsIlx1NUZCM1x1NURERFx1NUJCNlx1NUVCNyIsIjEyMzQ1Il0";

//まず通常のBase64に変換する
int padCount = (base64.Length % 4) switch { 0 => 0, 2 => 2, _ => 1 };
base64 = base64.Replace("-", "+").Replace("_", "/") + new string('=', padCount);

//JSONにする
string json = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(base64));

//values は "Apple", "徳川家康", "12345" を要素に持つリストになります。
List<string> values = System.Text.Json.JsonSerializer.Deserialize<List<string>>(json)!;

//リストの要素を , で結合して1つも文字列として出力します。
System.Diagnostics.Debug.WriteLine(string.Join(",", values)); //Apple,徳川家康,12345

Debug.WriteLineが表示される場所

メモ:List<string>に限らず、JsonSerializerで解析できるオブジェクトはこの例が通用します。

 

これでもできます。

string base64 = "WyJBcHBsZSIsIlx1NUZCM1x1NURERFx1NUJCNlx1NUVCNyIsIjEyMzQ1Il0";
string json = System.Text.Encoding.UTF8.GetString(Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Decode(base64));

//values は "Apple", "徳川家康", "12345" を要素に持つリストになります。
List<string> values = System.Text.Json.JsonSerializer.Deserialize<List<string>>(json)!;

//リストの要素を , で結合して1つも文字列として出力します。
System.Diagnostics.Debug.WriteLine(string.Join(",", values)); //Apple,徳川家康,12345

Debug.WriteLineが表示される場所

メモ:List<string>に限らず、JsonSerializerで解析できるオブジェクトはこの例が通用します。

メモ:ASP.NET以外で、この例を実行するには NuGet で Microsoft.AspNetCore.WebUtilities をインストールする必要があります。 → NuGetでパッケージをインストールする方法

 

 

Base64Urlをバイト型の配列にデコードする

string base64 = "EjRWeJq83vA";

//まず通常のBase64に変換する
int padCount = (base64.Length % 4) switch { 0 => 0, 2 => 2, _ => 1 };
base64 = base64.Replace("-", "+").Replace("_", "/") + new string('=', padCount);

//元の値に変換する
byte[] bytes = Convert.FromBase64String(base64);

//バイト型の配列を16進数表記の文字列で出力します。
System.Diagnostics.Debug.WriteLine(Convert.ToHexString(bytes)); //123456789ABCDEF0

Debug.WriteLineが表示される場所

 

これでもできます。

string base64 = "EjRWeJq83vA";
byte[] bytes = Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder.Decode(base64);

//バイト型の配列を16進数表記の文字列で出力します。
System.Diagnostics.Debug.WriteLine(Convert.ToHexString(bytes)); //123456789ABCDEF0

Debug.WriteLineが表示される場所

メモ:ASP.NET以外で、この例を実行するには NuGet で Microsoft.AspNetCore.WebUtilities をインストールする必要があります。 → NuGetでパッケージをインストールする方法