Implementation of Base64 Encryption/Decryption in Power App

Introduction

Base64 is a binary-to-text encoding scheme that is commonly used to encode binary data, especially when that data needs to be stored and transferred over media that are designed to deal with text. This encoding helps to ensure that the data remains intact without modification during transport.

How it works?

Base64 encoding schemes are commonly used when there is a need to encode binary data, especially when that data needs to be stored and transferred over media that are designed to deal with text. This encoding helps to ensure that the data remains intact without modification during transport.

Base64 is used commonly in several applications including email via MIME, as well as storing complex data in XML or JSON.In Base64 encoding, the basic set of 64 characters (A-Z, a-z, 0-9, + and /) are used along with the ‘=’ symbol which is used for padding at the end of the data.

Implementation of Base64 Encryption/Decryption in Power Apps using Power Fx

To implement Base64 Encryption/Description there are 3 ways

  • Power Automate
  • Custom Connector (C# Code)
  • Power Fx

In Canvas Apps, this can be implemented by using the Power Fx "With" function.The "With" function in Power Apps is a powerful tool that allows you to simplify complex formulas by assigning a name to an expression or value.

Power Fx Logic to convert text to Base64

{
    "With": {
        "InputText": "TextInput3_1.Text",
        "AsciiTable": "AddColumns(Sequence(2^8,0),\"char\",Char(Value))",
        "B64ToBin": {
            "Table": [
                {"b64": "A", "bin": "000000"},
                {"b64": "B", "bin": "000001"},
                {"b64": "C", "bin": "000010"},
                {"b64": "D", "bin": "000011"},
                {"b64": "E", "bin": "000100"},
                {"b64": "F", "bin": "000101"},
                {"b64": "G", "bin": "000110"},
                {"b64": "H", "bin": "000111"},
                {"b64": "I", "bin": "001000"},
                {"b64": "J", "bin": "001001"},
                {"b64": "K", "bin": "001010"},
                {"b64": "L", "bin": "001011"},
                {"b64": "M", "bin": "001100"},
                {"b64": "N", "bin": "001101"},
                {"b64": "O", "bin": "001110"},
                {"b64": "P", "bin": "001111"},
                {"b64": "Q", "bin": "010000"},
                {"b64": "R", "bin": "010001"},
                {"b64": "S", "bin": "010010"},
                {"b64": "T", "bin": "010011"},
                {"b64": "U", "bin": "010100"},
                {"b64": "V", "bin": "010101"},
                {"b64": "W", "bin": "010110"},
                {"b64": "X", "bin": "010111"},
                {"b64": "Y", "bin": "011000"},
                {"b64": "Z", "bin": "011001"},
                {"b64": "a", "bin": "011010"},
                {"b64": "b", "bin": "011011"},
                {"b64": "c", "bin": "011100"},
                {"b64": "d", "bin": "011101"},
                {"b64": "e", "bin": "011110"},
                {"b64": "f", "bin": "011111"},
                {"b64": "g", "bin": "100000"},
                {"b64": "h", "bin": "100001"},
                {"b64": "i", "bin": "100010"},
                {"b64": "j", "bin": "100011"},
                {"b64": "k", "bin": "100100"},
                {"b64": "l", "bin": "100101"},
                {"b64": "m", "bin": "100110"},
                {"b64": "n", "bin": "100111"},
                {"b64": "o", "bin": "101000"},
                {"b64": "p", "bin": "101001"},
                {"b64": "q", "bin": "101010"},
                {"b64": "r", "bin": "101011"},
                {"b64": "s", "bin": "101100"},
                {"b64": "t", "bin": "101101"},
                {"b64": "u", "bin": "101110"},
                {"b64": "v", "bin": "101111"},
                {"b64": "w", "bin": "110000"},
                {"b64": "x", "bin": "110001"},
                {"b64": "y", "bin": "110010"},
                {"b64": "z", "bin": "110011"},
                {"b64": "0", "bin": "110100"},
                {"b64": "1", "bin": "110101"},
                {"b64": "2", "bin": "110110"},
                {"b64": "3", "bin": "110111"},
                {"b64": "4", "bin": "111000"},
                {"b64": "5", "bin": "111001"},
                {"b64": "6", "bin": "111010"},
                {"b64": "7", "bin": "111011"},
                {"b64": "8", "bin": "111100"},
                {"b64": "9", "bin": "111101"},
                {"b64": "+", "bin": "111110"},
                {"b64": "/", "bin": "111111"}
            ]
        }
    },
    "With": {
        "BinRep": "Concat(AddColumns(ForAll(Split(InputText,\"\"), {Result: ThisRecord.Value}),\"dec\",LookUp(AsciiTable,char=Result).Value),Concat(Sequence(8,8,-1),Text(If(And(Mod(dec,Power(2,Value))>=Power(2,Value-1),Mod(dec,Power(2,Value))<Power(2,Value)),1,0)))&\"\",\"\"))",
        "b64string": "Concat(Sequence(RoundUp(Len(BinRep)/6,0),0),LookUp(B64ToBin,bin=Mid(BinRep&Left(\"000000\",6-Mod(Len(BinRep),6)),6*Value+1,6)).b64&\"\",\"\")",
        "result": "b64string&Left(\"====\",Mod(4-Mod(Len(b64string),4),4))"
    }
}

Power Fx logic to convert text from Base64

{
    "With": {
        "InputB64": "TextInput3.Text",
        "B64ToBin": {
            "Table": [
                {"b64": "A", "bin": "000000"},
                {"b64": "B", "bin": "000001"},
                {"b64": "C", "bin": "000010"},
                {"b64": "D", "bin": "000011"},
                {"b64": "E", "bin": "000100"},
                {"b64": "F", "bin": "000101"},
                {"b64": "G", "bin": "000110"},
                {"b64": "H", "bin": "000111"},
                {"b64": "I", "bin": "001000"},
                {"b64": "J", "bin": "001001"},
                {"b64": "K", "bin": "001010"},
                {"b64": "L", "bin": "001011"},
                {"b64": "M", "bin": "001100"},
                {"b64": "N", "bin": "001101"},
                {"b64": "O", "bin": "001110"},
                {"b64": "P", "bin": "001111"},
                {"b64": "Q", "bin": "010000"},
                {"b64": "R", "bin": "010001"},
                {"b64": "S", "bin": "010010"},
                {"b64": "T", "bin": "010011"},
                {"b64": "U", "bin": "010100"},
                {"b64": "V", "bin": "010101"},
                {"b64": "W", "bin": "010110"},
                {"b64": "X", "bin": "010111"},
                {"b64": "Y", "bin": "011000"},
                {"b64": "Z", "bin": "011001"},
                {"b64": "a", "bin": "011010"},
                {"b64": "b", "bin": "011011"},
                {"b64": "c", "bin": "011100"},
                {"b64": "d", "bin": "011101"},
                {"b64": "e", "bin": "011110"},
                {"b64": "f", "bin": "011111"},
                {"b64": "g", "bin": "100000"},
                {"b64": "h", "bin": "100001"},
                {"b64": "i", "bin": "100010"},
                {"b64": "j", "bin": "100011"},
                {"b64": "k", "bin": "100100"},
                {"b64": "l", "bin": "100101"},
                {"b64": "m", "bin": "100110"},
                {"b64": "n", "bin": "100111"},
                {"b64": "o", "bin": "101000"},
                {"b64": "p", "bin": "101001"},
                {"b64": "q", "bin": "101010"},
                {"b64": "r", "bin": "101011"},
                {"b64": "s", "bin": "101100"},
                {"b64": "t", "bin": "101101"},
                {"b64": "u", "bin": "101110"},
                {"b64": "v", "bin": "101111"},
                {"b64": "w", "bin": "110000"},
                {"b64": "x", "bin": "110001"},
                {"b64": "y", "bin": "110010"},
                {"b64": "z", "bin": "110011"},
                {"b64": "0", "bin": "110100"},
                {"b64": "1", "bin": "110101"},
                {"b64": "2", "bin": "110110"},
                {"b64": "3", "bin": "110111"},
                {"b64": "4", "bin": "111000"},
                {"b64": "5", "bin": "111001"},
                {"b64": "6", "bin": "111010"},
                {"b64": "7", "bin": "111011"},
                {"b64": "8", "bin": "111100"},
                {"b64": "9", "bin": "111101"},
                {"b64": "+", "bin": "111110"},
                {"b64": "/", "bin": "111111"}
            ]
        },
        "With": {
            "BinRep": "Concat(AddColumns(ForAll(Split(InputB64,\"\"), {Result: ThisRecord.Value}),\"Bin\",LookUp(B64ToBin,b64=Result).bin),Bin,\"\")",
            "binArray": "AddColumns(Sequence(RoundUp(Len(BinRep)/8,0),0),\"bin\",Mid(BinRep&Left(\"00000000\",Mod(8-Mod(Len(BinRep),8),8)),8*Value+1,8))",
            "result": "Concat(binArray,Char(Sum(AddColumns(Sequence(8,8,-1),\"dec\",Power(2,Value-1)*Value(Mid(bin,8-Value+1,1))),dec))&\"\",\"\")"
        }
    }
}

Output

Base encoder

Conclusion

While Base64 can be seen as a type of encryption, it is not a secure way to hide or protect data as it can be easily reversed. It is a form of encoding used to safely transmit data across systems that might otherwise corrupt the data.

It’s important to note that Base64 encoding is not a cryptographic or hash function, and should not be used for password or security purposes. It’s simply a way to encode binary data into a safe textual form. For secure encryption, consider using methods like AES or RSA encryption algorithms.


Similar Articles