Embedding Binary Data in a Text File – or Just Saving Binary Data as Text
Morning All! A colleague of mine was recently doing some Powershell GUI work and mentioned to me that it would be cool if he could use an image in the form. Which got me to thinking about how you would actually store binary data into a text file... encoding is the answer.
I've written the following two ConvertTo and ConvertFrom functions which you can use to to encode either binary files or byte arrays to a Base64 strings. You can then use them in here strings in your scripts and write them out to binary files again.... Obviously, this does pose a potential moral issue of someone using these to store malicious binary files in the scripts, but I didn't intend the code for that! Honest!
Here's the ConvertTo function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | Function ConvertTo-EncodedText([string]$FilePath,[Byte[]]$ByteArray,[string]$SaveTo) { <# .Synopsis Converts Files or Byte Arrays into a Base 64 Encoded String .Description Allow you to pass in a file path, or a byte array and it will be encoded into a base 64 string. This can then either be outputted directly or written to a text file. .Parameter FilePath Optional: The path to the file you would like to encode .Parameter ByteArray Optional: A variable containing a byte array that you would like to encode. .Parameter SaveTo Optional: A path of where you would like to save the encoded text file to. .Example ConvertTo-EncodedText -FilePath c:\MyBinaryFile.Jpg This will return a base 64 encoded string of the file c:\MyBinaryFile.Jpg .Example ConvertTo-EncodedText -FilePath c:\MyBinaryFile.Jpg -SaveTo c:\MyEncodedFile.Txt This will create a base64 encoded string of c:\MyBinaryFile.Jpg and save it to c:\MyEncodedFile.Txt .Example ConvertTo-EncodedText -ByteArray $MyByteArray This will create a base 64 encoded string of the ByteArray held in $MyByteArray .Notes Name: ConvertTo-EncodedText Author: Peter Rossi Last Edited: 19th December 2011 #> if($ByteArray) { $Data = $ByteArray } Else { [byte[]]$Data = Get-Content $FilePath -Encoding Byte } if($SaveTo) { [system.convert]::ToBase64String($Data) | Set-Content $SaveTo Return $Null } Else { Return [system.convert]::ToBase64String($Data) } } |
And this is the ConvertFrom function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | Function ConvertFrom-EncodedText($InputData,$FilePath,$SaveTo) { <# .Synopsis Converts Files or Base64 Encoded strings into Byte Arrays or binary files .Description Allow you to pass in a file path, or a string and it will be encoded into a byte array. This can then either be outputted directly or written to a binary file. .Parameter InputData Optional: A variable containing a base64 encoded string .Parameter FilePath Optional: The path to the file you would like to decode .Parameter SaveTo Optional: A path of where you would like to save the decoded binary file to. .Example ConvertFrom-EncodedText -FilePath c:\MyEncodedFile.Txt This will return a byte array of the file c:\MyEncodedFile.Txt .Example ConvertFrom-EncodedText -FilePath c:\MyEncodedFile.txt -SaveTo c:\MyBinaryFile.Jpg This will create a byte array of c:\MyEncodedFile.Jpg and save it to c:\MyBinaryFile.Txt .Example ConvertFrom-EncodedText -InputData $MyBase64String This will create a byte arry of the base 64 encoded string held in $MyBase64String .Notes Name: ConvertFrom-EncodedText Author: Peter Rossi Last Edited: 19th December 2011 #> if($InputData) { $Data = $InputData } Else { $Data = Get-Content $FilePath } if($SaveTo) { [system.convert]::FromBase64String($Data) | Set-Content $SaveTo -Encoding Byte } Else { Return [System.Convert]::FromBase64String($Data) } } |
Disclaimer
Follow Me
My Tweets
- RT @concentrateddon: Let #powershell v3 unroll some fun for you (and end some confusion) http://t.co/aEOZzhO3 06:23:50 PM April 23, 2012 from Tweet Button ReplyRetweetFavorite
- RT @shanselman: An engineer walks into a bar and orders 1.0E20 root beers. Bartender: "That's a root beer float." Engineer: "Make it a ... 08:56:21 AM March 24, 2012 from Twitter for iPhone ReplyRetweetFavorite



