LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Decoding of Base64 Encoded Data

    Employee
    Posted 05-30-2016 07:40

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: zeeshan.sabir

    Hi,

    I am executing a webservice using HTTP Node that results a data field which contains Base64 encoded data as shown below. How can I decode this kind of data? Any help will be much appreciated.

    MTY0Ozk1MDUwNzI4MzsxMDIwMjY5NjIyOzE3LjA0LjIwMTY7MT AwMTg5NDkwOzs7OzsyMDE2MDQxNzs7Ozs7O082MTsxMDAwMDAw MDAwMDUxOTExMzA7QzJWOw0KMT

    Desired Result after Decoding:
    164;950507283;1020269622;17.04.2016;100189490;;;;; 20160417;;;;;;O61;100000000005191130;C2V;


    --
    Best Regards
    Zeeshan


  • 2.  RE: Decoding of Base64 Encoded Data

    Employee
    Posted 06-01-2016 03:01

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: awilliams1024

    Hi Zeeshan,

    If the data that has been encoded is textual in nature the following may provide you with a solution (copy and paste the code below into the BRE canvas):

    node:Base64_Decoder_5
    bretype:core::Filter
    editor:Label=Base64 Decoder
    editor:sortkey=574d47a940d63e39_5
    input:@40fd2c74167f1ca2/=
    output:@40fd2c7420761db6/=
    prop:Script=<<EOX
    if firstExec then {
    	base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    }
    
    ## Get the base64 string to be converted
    x_ = {{^Field^}}
    
    ## Remove any line breaks
    x_ = x_.replace("\r","").replace("\n","")
    
    ## Get the length of the string
    len_x = strlen(x_)
    
    ## Verify the string length is a multiple of 4 characters
    if mod(len_x, 4) != 0 then abort("Invalid base64 string (incorrect length).\n")
    
    ## If the input string is NULL return a NULL string
    if(isNull(x_)) then {
    	returnStr = str(null)
    }
    ## If the input string is empty return an empty string
    else if(len_x == 0) then {
    	returnStr = ""
    } 
    else {
    	## Get the number of padding characters in the base64 string
    	padStr = ""
    	numEquals = 0
    	if(substr(x_, len_x - 2, 1) == "=") then {
    		padStr = "AA"
    	}
    	else if(substr(x_, len_x - 1, 1) == "=") then {
    			padStr = "A"
    	} 
    	returnStr = ""
    	## Replace the padding characters with "A" characters
    	x_ = substr(x_, 0, len_x - strlen(padStr)) + padStr
    	
    	## Increment over the base64 string 4 characters at a time
    	i = 0
    	while i < len_x {
    		## Get the four characters to be converted in this iteration
    		firstChar = substr(x_, i, 1)
    		secondChar = substr(x_, i+1, 1)
    		thirdChar = substr(x_, i+2, 1)
    		fourthChar = substr(x_, i+3, 1)
    		## Each of these four characters represents a 6-bit index in the
    	    ## base64 characters list which, when concatenated, will give the
    	    ## 24-bit number for the original 3 characters
    		idx = bitwiseLeftShift(strFind(base64Chars, firstChar), 18)
            idx = idx + bitwiseLeftShift(strFind(base64Chars, secondChar), 12)
    		idx = idx + bitwiseLeftShift(strFind(base64Chars, thirdChar), 6)
    		idx = idx + strFind(base64Chars, fourthChar)
    		
    		## Add the converted characters to the return string
    		returnStr = returnStr 
    					+ chr(bitwiseAnd(bitwiseRightShift(idx, 16), 255)) 
    					+ chr(bitwiseAnd(bitwiseRightShift(idx, 8), 255))
    					+ chr(bitwiseAnd(idx, 255))
    		
    		i = i + 4
    	}
    }
    
    emit returnStr as "decoded_Data",*
    exclude {{^Field^}}
    
    EOX
    editor:XY=490,160
    editor:UsageDoc=Decodes a Base64 encoded string field.
    editor:Doc=Decodes a Base64 encoded string field.
    editor:propdef=Field|string|Input||Not Blank
    editor:propdoc=Field=The field containing the base64 encoded data.
    end:Base64_Decoder_5
    Regards,
    Adrian


  • 3.  RE: Decoding of Base64 Encoded Data

    Employee
    Posted 06-01-2016 08:44

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: zeeshan.sabir

    Hi Adrian,

    Thanks a lot! It worked like a charm


    --
    Kind Regards
    Zeeshan


  • 4.  RE: Decoding of Base64 Encoded Data

    Employee
    Posted 06-16-2016 03:57

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: awilliams1024

    Another alternative approach could be to use this node which is based on the Python node.

    node:Test_Data
    bretype:core::Static Data
    editor:Label=Test Data
    editor:sortkey=57627ae514bf79ca
    output:@40fe6c55598828e5/=
    prop:StaticData=<<EOX
    testData:string
    ""
    "Zg=="
    "Zm8="
    "Zm9v"
    "Zm9vYg=="
    "Zm9vYmE="
    "Zm9vYmFy"
    EOX
    editor:XY=240,140
    end:Test_Data
    
    node:Base64_Decoder
    bretype:core::Composite
    editor:Label=Base64 Decoder
    editor:sortkey=57627c2a0ea96330
    editor:icon=Python.ico
    input:57627c3e4b0a011e/=Test_Data.40fe6c55598828e5
    inputxy:0=110,90
    inputrotation:0=0
    output:57627c4b7c2740af/=
    outlink:0=/=base64DecodeImp.510d1e8b08c5651f
    outputxy:0=510,90
    outputrotation:0=0
    inclockxy:Inclock=20,30
    outclockxy:Outclock=400,30
    prop:DefaultAction=Edit
    prop:Input Field Name=testData
    prop:Output As String=true
    editor:XY=380,140
    editor:UsageDoc=Decodes a base64 encoded string.
    editor:Doc=Decodes a base64 encoded string.
    editor:propdef=Input Field Name|string|General||Not Blank
    editor:propdef=Output Field Name|string|General||None
    editor:propdef=Output As String|boolean|General||Not Blank
    editor:propdoc=Input Field Name=The field containing the base64 encoded data.
    editor:propdoc=Output Field Name=The name of the output field containing the decoded data.
    editor:propdoc=Output Field Name=
    editor:propdoc=Output Field Name=
    editor:propdoc=Output Field Name=If the name is not specified, the default field name is 'decoded_data'.
    editor:propdoc=Output As String=Specifies whether the data type of the output field containing the decoded data
    editor:propdoc=Output As String=should be string or unicode.
    editor:propdoc=Output As String=
    editor:propdoc=Output As String=If set to 'true' the field has a data type of string. If false, the data type is unicode.
    node:base64DecodeImp
    bretype:core::Python
    editor:Label=base64DecodeImp
    editor:sortkey=510d1e0e27e55c56_2
    input:510d1e952e4e6e80/in1=^.57627c3e4b0a011e
    output:510d1e8b08c5651f/out1=
    inclock:/=Validate_Input_Data_Type
    prop:InputFieldName={{^Input Field Name^}}
    prop:Mandatory=false
    prop:OutputAsString={{^Output As String^}}
    prop:OutputFieldName={{^Output Field Name=decoded_Data^}}
    prop:Python2Implementation=<<EOX
    import braininfo
    import os
    import tempfile
    
    
    PROPERTY_BASE = "ls.brain.node.base64Decode"
    
    def setup(brainNodeControlObj, BrainNodeClass):
    	''' Must return a class that inherits from BrainNodeClass. '''	
    	class BrainNode(BrainNodeClass):
    		def initialize(self):
    			''' Called at node initialization, before first pump.'''
    			super(BrainNode, self).initialize()
    			
    			self.logMedium("Initializing the base64Decode node")
    			
    			# Obtain the values set on the node parameters
    			self.OutputAsString = self.properties.getBool(PROPERTY_BASE + ".OutputAsString")
    			self.inputFieldName = self.properties.getString(PROPERTY_BASE + ".inputFieldName")
    			
    			#Example using textual substitution
    			self.outputFieldName = "{{^OutputFieldName^}}"
    			
    			# Ensure the required field exists on all inputs and store the index of this
    			# field in each input
    			self.inputFieldIndices = []
    			for input in self.inputs:
    				idx = input.metadata.find(self.inputFieldName)
    				if idx == -1:
    					raise braininfo.BrainNodeException, "Specified InputFieldName: %s does not exist on input: %s " % (self.inputFieldName, input.name)
    				
    				self.inputFieldIndices.append(idx)
    			
    			#Setup the output metadata
    			om = self.newMetadata()
    			if self.OutputAsString:
    				om.append(self.outputFieldName, "string")
    			else:
    				om.append(self.outputFieldName, "unicode")
    			
    			self.outputs[0].metadata = om
    
    		def finalize(self, val):
    			''' Called at node end, after last pump call. '''
    			super(BrainNode, self).finalize(val)
    
    		def pump(self, quant):
    			''' Will continue to be called until False is returned.
    
    			Only a small amount of work should be done in pump without checking
    			quant.permitsRunning.  When that returns False, return from pump
    			with a True value to be called again, a False value when finished.'''
    			while quant.permitsRunning(self):
    				
    				allDone = True
    				decodedData = ""
    				
    				# read from the first input only
    				for inp in range(0,1):
    					
    					inputRec = self.inputs[inp].read()
    					
    					if not inputRec is None:
    						allDone = False
    						# decode the data
    						decodedData = inputRec[self.inputFieldIndices[inp]].decode('base64')
    
    				
    				# when complete				
    				if allDone:
    					return False
    				
    				else:
    					outputRec = self.outputs[0].newRecord()
    					outputRec[0] = str(decodedData)
    					self.outputs[0].write(outputRec)
    					
    			# finished your quantum, you want pump called again
    			return True  
    
    	# If you implement your logic here, you don't need code below. If you want
    	# to inherit, and add more logic at next level, uncomment below code
    	
    	# py3File = brainNodeControlObj.properties.getString("ls.brain.node.BrainPython.python3ImplementationFile") 
    	# braininfo.loadModule(py3File, "py3", brainNodeControlObj)
    	# import py3
    	# BrainNodeImpl = py3.setup(brainNodeControlObj, BrainNode)
    	# return BrainNodeImpl
    
    	return BrainNode
    EOX
    editor:XY=390,90
    editor:propdef=InputFieldName|string|General|ls.brain.node.base64Decode.inputFieldName|Not Blank
    editor:propdef=OutputFieldName|string|General|ls.brain.node.base64Decode.outputFieldName|Not Blank
    editor:propdef=OutputAsString|boolean|General|ls.brain.node.base64Decode.outputAsString|Not Blank
    end:base64DecodeImp
    
    node:Validate_Input_Data_Type
    bretype:core::Filter
    editor:Label=Validate Input Data Type
    editor:sortkey=57627f2a11647285
    input:@40fd2c74167f1ca2/=^.57627c3e4b0a011e
    output:@40fd2c7420761db6/=
    prop:Script=<<EOX
    
    thisfieldType = typeName(fieldType("1:{{^Input Field Name^}}"))
    
    if( not(thisfieldType == "string" or thisfieldType == "unicode")) then abort("Input Field Name must be a string or unicode field.\n")
    
    emit "Success"as "Status"
    
    consume(1, true)
    EOX
    editor:XY=250,150
    end:Validate_Input_Data_Type
    
    end:Base64_Decoder


    Regards,
    Adrian


  • 5.  RE: Decoding of Base64 Encoded Data

    Employee
    Posted 11-01-2016 07:18

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Wizardous

    Thank you Adrian for the code and Zeeshan for asking
    It helped me alot.

    Regards
    �zg�n