| | 1 | | @using System.Text |
| | 2 | | @inject IJSRuntime JSRuntime |
| | 3 | |
|
| | 4 | | <div class="container"> |
| | 5 | | <div class="row"> |
| | 6 | | <div class="col"> |
| | 7 | | <h2>Base64 Encode/Decode</h2> |
| | 8 | | </div> |
| | 9 | | </div> |
| | 10 | | <div class="row"> |
| | 11 | | <div class="col"> |
| | 12 | | <div class="input-group"> |
| | 13 | | <textarea id="binary" class="form-control" rows="5" @bind="Input" placeholder="Base64"></textarea> |
| | 14 | | <span class="input-group-btn"> |
| | 15 | | <button id="btnDecode" name="btnDecode" class="btn btn-success" @onclick="Decode"><i class="fas fa-a |
| | 16 | | <button id="btnClear" name="btnClear" class="btn btn-danger float-right" @onclick="Clear"><i class=" |
| | 17 | | </span> |
| | 18 | | </div> |
| | 19 | | </div> |
| | 20 | | </div> |
| | 21 | |
|
| | 22 | | <div class="row"> |
| | 23 | | <div class="col"> |
| | 24 | | <div class="input-group"> |
| | 25 | | <textarea id="output" class="form-control" rows="5" @bind="Output" placeholder="String"></textarea> |
| | 26 | | <span class="input-group-btn"> |
| | 27 | | <button id="btnEncode" name="btnEncode" class="btn btn-success" @onclick="Encode"><i class="fas fa-a |
| | 28 | | @* <button id="btnClear" name="btnClear" class="btn btn-danger float-right" @onclick="Clear"><i clas |
| | 29 | | <button id="btnCopy" name="btnCopy" class="btn btn-info float-right" @onclick="Copy"><i class="far f |
| | 30 | | </span> |
| | 31 | | </div> |
| | 32 | | </div> |
| | 33 | | </div> |
| | 34 | |
|
| | 35 | | <div class="row"> |
| | 36 | | <div class="col"> |
| | 37 | | <p>Inspired from <a href="https://www.base64encode.org" target="_blank">base64encode</a></p> |
| | 38 | | <p>Inspired from <a href="https://www.base64decode.org" target="_blank">base64decode</a></p> |
| | 39 | | </div> |
| | 40 | | </div> |
| | 41 | |
|
| | 42 | | </div> |
| | 43 | |
|
| | 44 | | @code { |
| | 45 | | [Parameter] |
| 28 | 46 | | public string? Input { get; set; } |
| | 47 | |
|
| | 48 | | [Parameter] |
| 28 | 49 | | public string? Output { get; set; } |
| | 50 | |
|
| | 51 | | // protected override void OnInitialized() |
| | 52 | | // { |
| | 53 | | // Input = "QWxleEhlZGxleQ=="; // How self important //string.Empty; |
| | 54 | | // Output = string.Empty; |
| | 55 | | // } |
| | 56 | |
|
| | 57 | | void Encode() |
| 2 | 58 | | { |
| 3 | 59 | | if (string.IsNullOrEmpty(Output)) return; |
| 1 | 60 | | Input = Base64Encode(Output); |
| 2 | 61 | | } |
| | 62 | |
|
| | 63 | | void Decode() |
| 2 | 64 | | { |
| 3 | 65 | | if (string.IsNullOrEmpty(Input)) return; |
| 1 | 66 | | Output = Base64Decode(Input); |
| 2 | 67 | | } |
| | 68 | |
|
| | 69 | | private void Clear() |
| 0 | 70 | | { |
| 0 | 71 | | Input = string.Empty; |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | async Task Copy() |
| 0 | 75 | | { |
| 0 | 76 | | await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", Output); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | // https://stackoverflow.com/a/11743162/2895831 |
| | 80 | |
|
| | 81 | | public static string Base64Encode(string plainText) |
| 1 | 82 | | { |
| 1 | 83 | | var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); |
| 1 | 84 | | return System.Convert.ToBase64String(plainTextBytes); |
| 1 | 85 | | } |
| | 86 | |
|
| | 87 | | public static string Base64Decode(string base64EncodedData) |
| 1 | 88 | | { |
| 1 | 89 | | var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); |
| 1 | 90 | | return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); |
| 1 | 91 | | } |
| | 92 | | } |