Skip to main content
Inspiring
March 9, 2023
Answered

Showing this error at browser when trying to open an PDF attachment file

  • March 9, 2023
  • 1 reply
  • 749 views

Facing issue somtimes showing this error at browser when trying to open an PDF attachment file:

An error occurred while trying to encrypt or decrypt your input string: Given final block not properly padded. Such issues can arise if a bad key is used during decryption..

 

At application.cfc:

<cfset application.sKey = generateSecretKey('AES')>

 

At view CFM:

<td>

<a target="_blank" href="#buildurl('newonlinesc.view_pdf&id=#encrypt("2||#rc.data.pass_no#", '#application.Key#', "AES", "HEX")#')#">View Emirates
</td>

 

Opening a PDF file (view_pdf.cfm):

<cftry>
<cfif structkeyexists(rc, 'type') AND len(rc.type)>
   <cfheader name="content-disposition" value="inline; filename=#rc.pass_no#_#rc.type#">
<cfif listlast(rc.type,'.') EQ 'pdf'>
    <cfset ctype = 'application/pdf'>
<cfelse>
    <cfset ctype = 'image/jpeg'>
</cfif>

   <cfcontent type="#ctype#" variable="#rc.docdata#">
<cfelse>
    <cflocation url="#buildurl('newonlinesc.list')#" addtoken="false">
</cfif>
<cfcatch>

    <cfdump var="#cfcatch#" abort="true"/>

</cfcatch>
</cftry>

    This topic has been closed for replies.
    Correct answer Faizan278243197lsr

    I am assuming that '#application.Key#' is a typing error or that your code defines two keys, application.Key and application.Keys. Otherwise, ColdFusion would have given you the message, "Element Key is undefined in APPLICATION." So, look into that.

     

    Additionally, split up the code and use the argument-names of the encrypt function:

    <cfset urlID=encrypt(string="2||#rc.data.pass_no#", key='#application.sKey#', algorithm="AES", encoding="HEX")>
    <cfset hrefUrl=buildurl('newonlinesc.view_pdf&id=#urlID#')>
    <cfoutput><a target="_blank" href="#hrefUrl#">View Emirates</a></cfoutput>

    Yes BKBK,

    You are right. The mistake I was doing in sKey and Key. Correction done there now hope this error may not repeat again. Many thanks for your guidance.
    In application.cfc variable seted with application.sKey but while doing encryption was mentioning application.Key

     

    1 reply

    Charlie Arehart
    Community Expert
    Community Expert
    March 9, 2023

    It seems your error is in the encrypt function. But your title suggest you think it's about pdf processing.

     

    I'd recommend first you confirm either way.

     

    Next, if it IS the encrypt function, I'd recommend you confirm that all those variables you pass into that hold the values you expect them to. (We can't know.)

     

    Finally, you show the application.cfc setting an application.skey, but then the encrypt code uses an application.key variable. Are you sure there's no mistake there? 

    /Charlie (troubleshooter, carehart. org)
    Inspiring
    March 9, 2023

    Charlie,
    Most of the time its working only sometimes getting error. Means there is nothing with code or encryption. Some other ways we can think why it's happening.

    Charlie Arehart
    Community Expert
    Community Expert
    March 9, 2023

    Hmm. I would come to a very different conclusion. It's specifically BECAUSE it fails only sometimes--and because it uses several variables--that I would stress the importance of knowing the value of those variables when it errors.

     

    You could wrap the encrypt in a cftry/CFCATCH and in the catch dump those vars. You could also dump them once when it WORKS to know for sure what good values look like. 

    /Charlie (troubleshooter, carehart. org)