encryption.html 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. <!DOCTYPE html>
  2. <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
  3. <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Encryption Library &mdash; CodeIgniter 3.1.11 documentation</title>
  8. <link rel="shortcut icon" href="../_static/ci-icon.ico"/>
  9. <link href='https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Roboto+Slab:400,700|Inconsolata:400,700&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
  10. <link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
  11. <link rel="index" title="Index"
  12. href="../genindex.html"/>
  13. <link rel="search" title="Search" href="../search.html"/>
  14. <link rel="top" title="CodeIgniter 3.1.11 documentation" href="../index.html"/>
  15. <link rel="up" title="Libraries" href="index.html"/>
  16. <link rel="next" title="File Uploading Class" href="file_uploading.html"/>
  17. <link rel="prev" title="Encrypt Class" href="encrypt.html"/>
  18. <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
  19. </head>
  20. <body class="wy-body-for-nav" role="document">
  21. <div id="nav">
  22. <div id="nav_inner">
  23. <div id="pulldown-menu" class="ciNav">
  24. <ul>
  25. <li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">Welcome to CodeIgniter</a></li>
  26. </ul>
  27. <ul>
  28. <li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul>
  29. <li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li>
  30. <li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</a></li>
  31. <li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
  32. <li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
  33. </ul>
  34. </li>
  35. </ul>
  36. <ul>
  37. <li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul>
  38. <li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li>
  39. <li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li>
  40. <li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li>
  41. <li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li>
  42. <li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li>
  43. <li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li>
  44. </ul>
  45. </li>
  46. </ul>
  47. <ul>
  48. <li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a><ul>
  49. <li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
  50. <li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
  51. <li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
  52. <li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
  53. </ul>
  54. </li>
  55. </ul>
  56. <ul>
  57. <li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing to CodeIgniter</a><ul>
  58. <li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li>
  59. <li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</a></li>
  60. </ul>
  61. </li>
  62. </ul>
  63. <ul>
  64. <li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
  65. <li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
  66. <li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">Controllers</a></li>
  67. <li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">Reserved Names</a></li>
  68. <li class="toctree-l2"><a class="reference internal" href="../general/views.html">Views</a></li>
  69. <li class="toctree-l2"><a class="reference internal" href="../general/models.html">Models</a></li>
  70. <li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helpers</a></li>
  71. <li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">Using CodeIgniter Libraries</a></li>
  72. <li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">Creating Libraries</a></li>
  73. <li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">Using CodeIgniter Drivers</a></li>
  74. <li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">Creating Drivers</a></li>
  75. <li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">Creating Core System Classes</a></li>
  76. <li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">Creating Ancillary Classes</a></li>
  77. <li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">Hooks - Extending the Framework Core</a></li>
  78. <li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">Auto-loading Resources</a></li>
  79. <li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Common Functions</a></li>
  80. <li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">Compatibility Functions</a></li>
  81. <li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI Routing</a></li>
  82. <li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
  83. <li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Caching</a></li>
  84. <li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">Profiling Your Application</a></li>
  85. <li class="toctree-l2"><a class="reference internal" href="../general/cli.html">Running via the CLI</a></li>
  86. <li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
  87. <li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
  88. <li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
  89. <li class="toctree-l2"><a class="reference internal" href="../general/security.html">Security</a></li>
  90. <li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP Style Guide</a></li>
  91. </ul>
  92. </li>
  93. </ul>
  94. <ul class="current">
  95. <li class="toctree-l1 current"><a class="reference internal" href="index.html">Libraries</a><ul class="current">
  96. <li class="toctree-l2"><a class="reference internal" href="benchmark.html">Benchmarking Class</a></li>
  97. <li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li>
  98. <li class="toctree-l2"><a class="reference internal" href="calendar.html">Calendaring Class</a></li>
  99. <li class="toctree-l2"><a class="reference internal" href="cart.html">Shopping Cart Class</a></li>
  100. <li class="toctree-l2"><a class="reference internal" href="config.html">Config Class</a></li>
  101. <li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li>
  102. <li class="toctree-l2"><a class="reference internal" href="encrypt.html">Encrypt Class</a></li>
  103. <li class="toctree-l2 current"><a class="current reference internal" href="#">Encryption Library</a></li>
  104. <li class="toctree-l2"><a class="reference internal" href="file_uploading.html">File Uploading Class</a></li>
  105. <li class="toctree-l2"><a class="reference internal" href="form_validation.html">Form Validation</a></li>
  106. <li class="toctree-l2"><a class="reference internal" href="ftp.html">FTP Class</a></li>
  107. <li class="toctree-l2"><a class="reference internal" href="image_lib.html">Image Manipulation Class</a></li>
  108. <li class="toctree-l2"><a class="reference internal" href="input.html">Input Class</a></li>
  109. <li class="toctree-l2"><a class="reference internal" href="javascript.html">Javascript Class</a></li>
  110. <li class="toctree-l2"><a class="reference internal" href="language.html">Language Class</a></li>
  111. <li class="toctree-l2"><a class="reference internal" href="loader.html">Loader Class</a></li>
  112. <li class="toctree-l2"><a class="reference internal" href="migration.html">Migrations Class</a></li>
  113. <li class="toctree-l2"><a class="reference internal" href="output.html">Output Class</a></li>
  114. <li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination Class</a></li>
  115. <li class="toctree-l2"><a class="reference internal" href="parser.html">Template Parser Class</a></li>
  116. <li class="toctree-l2"><a class="reference internal" href="security.html">Security Class</a></li>
  117. <li class="toctree-l2"><a class="reference internal" href="sessions.html">Session Library</a></li>
  118. <li class="toctree-l2"><a class="reference internal" href="table.html">HTML Table Class</a></li>
  119. <li class="toctree-l2"><a class="reference internal" href="trackback.html">Trackback Class</a></li>
  120. <li class="toctree-l2"><a class="reference internal" href="typography.html">Typography Class</a></li>
  121. <li class="toctree-l2"><a class="reference internal" href="unit_testing.html">Unit Testing Class</a></li>
  122. <li class="toctree-l2"><a class="reference internal" href="uri.html">URI Class</a></li>
  123. <li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li>
  124. <li class="toctree-l2"><a class="reference internal" href="xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li>
  125. <li class="toctree-l2"><a class="reference internal" href="zip.html">Zip Encoding Class</a></li>
  126. </ul>
  127. </li>
  128. </ul>
  129. <ul>
  130. <li class="toctree-l1"><a class="reference internal" href="../database/index.html">Database Reference</a><ul>
  131. <li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
  132. <li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
  133. <li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
  134. <li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
  135. <li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
  136. <li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
  137. <li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
  138. <li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
  139. <li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
  140. <li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
  141. <li class="toctree-l2"><a class="reference internal" href="../database/caching.html">Query Caching</a></li>
  142. <li class="toctree-l2"><a class="reference internal" href="../database/forge.html">Database Manipulation with Database Forge</a></li>
  143. <li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities Class</a></li>
  144. <li class="toctree-l2"><a class="reference internal" href="../database/db_driver_reference.html">Database Driver Reference</a></li>
  145. </ul>
  146. </li>
  147. </ul>
  148. <ul>
  149. <li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
  150. <li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
  151. <li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">CAPTCHA Helper</a></li>
  152. <li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
  153. <li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
  154. <li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">Directory Helper</a></li>
  155. <li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li>
  156. <li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li>
  157. <li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File Helper</a></li>
  158. <li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
  159. <li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
  160. <li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
  161. <li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">Language Helper</a></li>
  162. <li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
  163. <li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">Path Helper</a></li>
  164. <li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
  165. <li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">Smiley Helper</a></li>
  166. <li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String Helper</a></li>
  167. <li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
  168. <li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">Typography Helper</a></li>
  169. <li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
  170. <li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
  171. </ul>
  172. </li>
  173. </ul>
  174. </div>
  175. </div>
  176. </div>
  177. <div id="nav2">
  178. <a href="#" id="openToc">
  179. <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAAAf/bAIQABAMDAwMDBAMDBAYEAwQGBwUEBAUHCAYGBwYGCAoICQkJCQgKCgwMDAwMCgwMDQ0MDBERERERFBQUFBQUFBQUFAEEBQUIBwgPCgoPFA4ODhQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU/8AAEQgAKwCaAwERAAIRAQMRAf/EAHsAAQAABwEBAAAAAAAAAAAAAAABAwQFBgcIAgkBAQAAAAAAAAAAAAAAAAAAAAAQAAEDAwICBwYEAgsAAAAAAAIBAwQAEQUSBiEHkROTVNQWGDFBUVIUCHEiMtOUFWGBobHRQlMkZIRVEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwDSC+ygkOOaUoKigUCgUCgUCgUCgUCgUCgUCgkuGguIP9FBMFb0Hqg7We+3jlmIqqYFf4ub+/QYlnOR/LqIBKGFUbf8qWv971BytQXXE7Y3Lnm3HsFhp2TaZJAdchRXpIgSpdEJWxJEW3xoKV7F5OMy7JkQn2o7D6w33XGjEAkoiqrJEqIiOIiKuhePCgqp22dyYyS3CyWHnQ5joG61HkRnmnTbaFSMhExRVQRRVJU9iUHjE7ez+fJ0MFipmUNhBV8YUd2SoIV9KkjQla9ltegttBdPLW4/qocL+UTfrMiHW4+P9M71shuyrqaHTcxsl7jegpsji8nh5ZwMvDfgTm0RTjSmjYdFCS6KoOIipdFunCgmNYTMv457MMY6U7iI6oMieDDhRm1VbIhuoOkbqtuK0Hpzb+eZcYZexUxt6UyUqK2cd0SdjtgrhOgijcgERUlJOCIl6CpgbP3blRI8XgMjNARAyKNDfeRBdFDBVUAXgQrqH4pxoJTu2NysY97LP4ac1io5q1InHFeGO24LnVKJuKOkSQ/yKir+rh7aCLG1dzypZQI2FnvTgccYOM3FeN0XWERXAUEFVQgQkUktdLpegm+Td3/Xli/L+S/mYNJIOF9G/wBeLKrZHFb0akG6W1WtQWSg3Dyg5e7V3fipE3O4/wCrktyzYA+ufas2LbZIlmnAT2kvuoN1wft95augilglX/tzP3qCu9O3LL/wV/i5v79BvmTADq14UGu91467Z6U9y0HzH/ncj/U/sT/CgynZG7I2NezpZGUjIycJkYkZSG+uQ81pbBNKLxJfjwoMqZ3/ALYHl35AJ7/cuwHcu5k7r1Q5pHetBjquqVVJWGxj9Zrtcl/Ggy3dHMvauR3HFZj5nHNxSyW5JISYDMoIwx8tFIGHZhPNaykGapr6rUAiicEoMG21lMRj8buPAz8xhJrr7uOeiPTCyAwXUaGR1mgozbTusOsFLEiJ7fbQa/h7gcjy2H3V6xppwDNtUSxCJIqp7valBuWVzJ22xuCROXNNZiJkMtms0DbjUkAZjzoDrTMd9dDRI44ZC2YsrYdKWP2WDT2S3N9dNdlRYrGMYc06IURXSYb0igrpWS485xVNS6nF4rwslkoMwnbpgZLB7bmt5uMweAhDEl4B5uSLzzqTnnyVpW2jaJHRMSIjdDiiotvy3DOE5rYTEbkl5yFn28k7JyG4c7AU2HtLH1uKfaiMPI40CdYbpNtmLdwTSn5rewLNld+7TLdeal4WarWBkbVKBjgdElMJJwAAY5fl4kB3b1fp4XvagsGS3FjJfLzDNtS8aeXx7LzT7TyzByQE5PccRGRC0ZRUDRV6y62vbjagzLmJzS2vuPK43JY6aP1TW6Jz+RIWyFtyC06y3EkiiinAo7YCqfq1AqqnGgsOH3lhZO8d1pmcpB8j5XIm9OYlBJSQ/FSS4427DKO0RC8AlcEMhFdViRR1WDWR5t3WXVuL1d106kG9vdeye2g60+1FDyW0shIcXVpyroXt8I8dfd+NB1vioAdWnD3UF1+gD4UFc6CEKpagxXN43rwJLUHz7yX2c8zokt9uHlsPIhA4aRnnHJTLptIS6CNsY7iASpxUUMkReGpfbQW0vtN5pitvrsN28rwtBD0nc0+/Yft5XhaB6TuaXfsP28rwtA9J3NPv2H7eV4Wgek7mn37D9vK8LQPSdzT79h+3leFoHpO5pd+w/byvC0D0nc0u/Yft5XhaB6TuaXfsP28rwtA9J3NLv2H7eV4Wgek7ml37D9vK8LQPSdzS79h+3leFoHpO5p9+w/byvC0E9r7Reazy2HIYVPxkS/CUHVn26cosxyv2g7h89LYmZSXOenvLEQ1YaQ222RATcQCP8rSGqqA8S02W2pQ6FhMoAIlqCtsnwoCpdKClejI4i3Sgtb+GBxVuNBSFt1pV/RQefLjPyUDy4z8lA8uM/JQPLjPyUDy4z8lA8uM/JQPLjPyUDy4z8lA8uM/JQPLjPyUDy4z8lA8utJ/koJ7WCbBU/LQXOPAFq1koK8B0pag90CggtBBf6qB0UDooHRQOigdFA6KB0UDooHRQOigdFA6KB0UDooI0EaBQf//Z" title="Toggle Table of Contents" alt="Toggle Table of Contents" />
  180. </a>
  181. </div>
  182. <div class="wy-grid-for-nav">
  183. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  184. <div class="wy-side-nav-search">
  185. <a href="../index.html" class="fa fa-home"> CodeIgniter</a>
  186. <div role="search">
  187. <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
  188. <input type="text" name="q" placeholder="Search docs" />
  189. <input type="hidden" name="check_keywords" value="yes" />
  190. <input type="hidden" name="area" value="default" />
  191. </form>
  192. </div>
  193. </div>
  194. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  195. <ul>
  196. <li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">Welcome to CodeIgniter</a></li>
  197. </ul>
  198. <ul>
  199. <li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul>
  200. <li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li>
  201. <li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</a></li>
  202. <li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
  203. <li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
  204. </ul>
  205. </li>
  206. </ul>
  207. <ul>
  208. <li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul>
  209. <li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li>
  210. <li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li>
  211. <li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li>
  212. <li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li>
  213. <li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li>
  214. <li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li>
  215. </ul>
  216. </li>
  217. </ul>
  218. <ul>
  219. <li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a><ul>
  220. <li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
  221. <li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
  222. <li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
  223. <li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
  224. </ul>
  225. </li>
  226. </ul>
  227. <ul>
  228. <li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing to CodeIgniter</a><ul>
  229. <li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li>
  230. <li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</a></li>
  231. </ul>
  232. </li>
  233. </ul>
  234. <ul>
  235. <li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
  236. <li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
  237. <li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">Controllers</a></li>
  238. <li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">Reserved Names</a></li>
  239. <li class="toctree-l2"><a class="reference internal" href="../general/views.html">Views</a></li>
  240. <li class="toctree-l2"><a class="reference internal" href="../general/models.html">Models</a></li>
  241. <li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helpers</a></li>
  242. <li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">Using CodeIgniter Libraries</a></li>
  243. <li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">Creating Libraries</a></li>
  244. <li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">Using CodeIgniter Drivers</a></li>
  245. <li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">Creating Drivers</a></li>
  246. <li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">Creating Core System Classes</a></li>
  247. <li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">Creating Ancillary Classes</a></li>
  248. <li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">Hooks - Extending the Framework Core</a></li>
  249. <li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">Auto-loading Resources</a></li>
  250. <li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Common Functions</a></li>
  251. <li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">Compatibility Functions</a></li>
  252. <li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI Routing</a></li>
  253. <li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
  254. <li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Caching</a></li>
  255. <li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">Profiling Your Application</a></li>
  256. <li class="toctree-l2"><a class="reference internal" href="../general/cli.html">Running via the CLI</a></li>
  257. <li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
  258. <li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
  259. <li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
  260. <li class="toctree-l2"><a class="reference internal" href="../general/security.html">Security</a></li>
  261. <li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP Style Guide</a></li>
  262. </ul>
  263. </li>
  264. </ul>
  265. <ul class="current">
  266. <li class="toctree-l1 current"><a class="reference internal" href="index.html">Libraries</a><ul class="current">
  267. <li class="toctree-l2"><a class="reference internal" href="benchmark.html">Benchmarking Class</a></li>
  268. <li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li>
  269. <li class="toctree-l2"><a class="reference internal" href="calendar.html">Calendaring Class</a></li>
  270. <li class="toctree-l2"><a class="reference internal" href="cart.html">Shopping Cart Class</a></li>
  271. <li class="toctree-l2"><a class="reference internal" href="config.html">Config Class</a></li>
  272. <li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li>
  273. <li class="toctree-l2"><a class="reference internal" href="encrypt.html">Encrypt Class</a></li>
  274. <li class="toctree-l2 current"><a class="current reference internal" href="#">Encryption Library</a></li>
  275. <li class="toctree-l2"><a class="reference internal" href="file_uploading.html">File Uploading Class</a></li>
  276. <li class="toctree-l2"><a class="reference internal" href="form_validation.html">Form Validation</a></li>
  277. <li class="toctree-l2"><a class="reference internal" href="ftp.html">FTP Class</a></li>
  278. <li class="toctree-l2"><a class="reference internal" href="image_lib.html">Image Manipulation Class</a></li>
  279. <li class="toctree-l2"><a class="reference internal" href="input.html">Input Class</a></li>
  280. <li class="toctree-l2"><a class="reference internal" href="javascript.html">Javascript Class</a></li>
  281. <li class="toctree-l2"><a class="reference internal" href="language.html">Language Class</a></li>
  282. <li class="toctree-l2"><a class="reference internal" href="loader.html">Loader Class</a></li>
  283. <li class="toctree-l2"><a class="reference internal" href="migration.html">Migrations Class</a></li>
  284. <li class="toctree-l2"><a class="reference internal" href="output.html">Output Class</a></li>
  285. <li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination Class</a></li>
  286. <li class="toctree-l2"><a class="reference internal" href="parser.html">Template Parser Class</a></li>
  287. <li class="toctree-l2"><a class="reference internal" href="security.html">Security Class</a></li>
  288. <li class="toctree-l2"><a class="reference internal" href="sessions.html">Session Library</a></li>
  289. <li class="toctree-l2"><a class="reference internal" href="table.html">HTML Table Class</a></li>
  290. <li class="toctree-l2"><a class="reference internal" href="trackback.html">Trackback Class</a></li>
  291. <li class="toctree-l2"><a class="reference internal" href="typography.html">Typography Class</a></li>
  292. <li class="toctree-l2"><a class="reference internal" href="unit_testing.html">Unit Testing Class</a></li>
  293. <li class="toctree-l2"><a class="reference internal" href="uri.html">URI Class</a></li>
  294. <li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li>
  295. <li class="toctree-l2"><a class="reference internal" href="xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li>
  296. <li class="toctree-l2"><a class="reference internal" href="zip.html">Zip Encoding Class</a></li>
  297. </ul>
  298. </li>
  299. </ul>
  300. <ul>
  301. <li class="toctree-l1"><a class="reference internal" href="../database/index.html">Database Reference</a><ul>
  302. <li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
  303. <li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
  304. <li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
  305. <li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
  306. <li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
  307. <li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
  308. <li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
  309. <li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
  310. <li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
  311. <li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
  312. <li class="toctree-l2"><a class="reference internal" href="../database/caching.html">Query Caching</a></li>
  313. <li class="toctree-l2"><a class="reference internal" href="../database/forge.html">Database Manipulation with Database Forge</a></li>
  314. <li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities Class</a></li>
  315. <li class="toctree-l2"><a class="reference internal" href="../database/db_driver_reference.html">Database Driver Reference</a></li>
  316. </ul>
  317. </li>
  318. </ul>
  319. <ul>
  320. <li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
  321. <li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
  322. <li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">CAPTCHA Helper</a></li>
  323. <li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
  324. <li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
  325. <li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">Directory Helper</a></li>
  326. <li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li>
  327. <li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li>
  328. <li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File Helper</a></li>
  329. <li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
  330. <li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
  331. <li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
  332. <li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">Language Helper</a></li>
  333. <li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
  334. <li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">Path Helper</a></li>
  335. <li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
  336. <li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">Smiley Helper</a></li>
  337. <li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String Helper</a></li>
  338. <li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
  339. <li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">Typography Helper</a></li>
  340. <li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
  341. <li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
  342. </ul>
  343. </li>
  344. </ul>
  345. </div>
  346. &nbsp;
  347. </nav>
  348. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  349. <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
  350. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  351. <a href="../index.html">CodeIgniter</a>
  352. </nav>
  353. <div class="wy-nav-content">
  354. <div class="rst-content">
  355. <div role="navigation" aria-label="breadcrumbs navigation">
  356. <ul class="wy-breadcrumbs">
  357. <li><a href="../index.html">Docs</a> &raquo;</li>
  358. <li><a href="index.html">Libraries</a> &raquo;</li>
  359. <li>Encryption Library</li>
  360. <li class="wy-breadcrumbs-aside">
  361. </li>
  362. <div style="float:right;margin-left:5px;" id="closeMe">
  363. <img title="Classic Layout" alt="classic layout" src="data:image/gif;base64,R0lGODlhFAAUAJEAAAAAADMzM////wAAACH5BAUUAAIALAAAAAAUABQAAAImlI+py+0PU5gRBRDM3DxbWoXis42X13USOLauUIqnlsaH/eY6UwAAOw==" />
  364. </div>
  365. </ul>
  366. <hr/>
  367. </div>
  368. <div role="main" class="document">
  369. <div class="section" id="encryption-library">
  370. <h1>Encryption Library<a class="headerlink" href="#encryption-library" title="Permalink to this headline">¶</a></h1>
  371. <div class="admonition important">
  372. <p class="first admonition-title">Important</p>
  373. <p class="last">DO NOT use this or any other <em>encryption</em> library for
  374. user password storage! Passwords must be <em>hashed</em> instead, and you
  375. should do that via PHP’s own <a class="reference external" href="http://php.net/password">Password Hashing extension</a>.</p>
  376. </div>
  377. <p>The Encryption Library provides two-way data encryption. To do so in
  378. a cryptographically secure way, it utilizes PHP extensions that are
  379. unfortunately not always available on all systems.
  380. You must meet one of the following dependencies in order to use this
  381. library:</p>
  382. <ul class="simple">
  383. <li><a class="reference external" href="http://php.net/openssl">OpenSSL</a></li>
  384. <li><a class="reference external" href="http://php.net/mcrypt">MCrypt</a> (and <cite>MCRYPT_DEV_URANDOM</cite> availability)</li>
  385. </ul>
  386. <p>If neither of the above dependencies is met, we simply cannot offer
  387. you a good enough implementation to meet the high standards required
  388. for proper cryptography.</p>
  389. <div class="contents local topic" id="contents">
  390. <ul class="simple">
  391. <li><a class="reference internal" href="#using-the-encryption-library" id="id2">Using the Encryption Library</a><ul>
  392. <li><a class="reference internal" href="#initializing-the-class" id="id3">Initializing the Class</a></li>
  393. <li><a class="reference internal" href="#default-behavior" id="id4">Default behavior</a></li>
  394. <li><a class="reference internal" href="#setting-your-encryption-key" id="id5">Setting your encryption_key</a></li>
  395. <li><a class="reference internal" href="#supported-encryption-ciphers-and-modes" id="id6">Supported encryption ciphers and modes</a><ul>
  396. <li><a class="reference internal" href="#portable-ciphers" id="id7">Portable ciphers</a></li>
  397. <li><a class="reference internal" href="#driver-specific-ciphers" id="id8">Driver-specific ciphers</a></li>
  398. <li><a class="reference internal" href="#encryption-modes" id="id9">Encryption modes</a></li>
  399. </ul>
  400. </li>
  401. <li><a class="reference internal" href="#message-length" id="id10">Message Length</a></li>
  402. <li><a class="reference internal" href="#configuring-the-library" id="id11">Configuring the library</a></li>
  403. <li><a class="reference internal" href="#encrypting-and-decrypting-data" id="id12">Encrypting and decrypting data</a><ul>
  404. <li><a class="reference internal" href="#how-it-works" id="id13">How it works</a></li>
  405. <li><a class="reference internal" href="#using-custom-parameters" id="id14">Using custom parameters</a></li>
  406. <li><a class="reference internal" href="#supported-hmac-authentication-algorithms" id="id15">Supported HMAC authentication algorithms</a></li>
  407. </ul>
  408. </li>
  409. </ul>
  410. </li>
  411. <li><a class="reference internal" href="#class-reference" id="id16">Class Reference</a></li>
  412. </ul>
  413. </div>
  414. <div class="custom-index container"></div><div class="section" id="using-the-encryption-library">
  415. <h2><a class="toc-backref" href="#id2">Using the Encryption Library</a><a class="headerlink" href="#using-the-encryption-library" title="Permalink to this headline">¶</a></h2>
  416. <div class="section" id="initializing-the-class">
  417. <h3><a class="toc-backref" href="#id3">Initializing the Class</a><a class="headerlink" href="#initializing-the-class" title="Permalink to this headline">¶</a></h3>
  418. <p>Like most other classes in CodeIgniter, the Encryption library is
  419. initialized in your controller using the <code class="docutils literal"><span class="pre">$this-&gt;load-&gt;library()</span></code>
  420. method:</p>
  421. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">load</span><span class="o">-&gt;</span><span class="na">library</span><span class="p">(</span><span class="s1">&#39;encryption&#39;</span><span class="p">);</span>
  422. </pre></div>
  423. </div>
  424. <p>Once loaded, the Encryption library object will be available using:</p>
  425. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span>
  426. </pre></div>
  427. </div>
  428. </div>
  429. <div class="section" id="default-behavior">
  430. <h3><a class="toc-backref" href="#id4">Default behavior</a><a class="headerlink" href="#default-behavior" title="Permalink to this headline">¶</a></h3>
  431. <p>By default, the Encryption Library will use the AES-128 cipher in CBC
  432. mode, using your configured <em>encryption_key</em> and SHA512 HMAC authentication.</p>
  433. <div class="admonition note">
  434. <p class="first admonition-title">Note</p>
  435. <p class="last">AES-128 is chosen both because it is proven to be strong and
  436. because of its wide availability across different cryptographic
  437. software and programming languages’ APIs.</p>
  438. </div>
  439. <p>However, the <em>encryption_key</em> is not used as is.</p>
  440. <p>If you are somewhat familiar with cryptography, you should already know
  441. that a HMAC also requires a secret key and using the same key for both
  442. encryption and authentication is a bad practice.</p>
  443. <p>Because of that, two separate keys are derived from your already configured
  444. <em>encryption_key</em>: one for encryption and one for authentication. This is
  445. done via a technique called <a class="reference external" href="http://en.wikipedia.org/wiki/HKDF">HMAC-based Key Derivation Function</a> (HKDF).</p>
  446. </div>
  447. <div class="section" id="setting-your-encryption-key">
  448. <h3><a class="toc-backref" href="#id5">Setting your encryption_key</a><a class="headerlink" href="#setting-your-encryption-key" title="Permalink to this headline">¶</a></h3>
  449. <p>An <em>encryption key</em> is a piece of information that controls the
  450. cryptographic process and permits a plain-text string to be encrypted,
  451. and afterwards - decrypted. It is the secret “ingredient” in the whole
  452. process that allows you to be the only one who is able to decrypt data
  453. that you’ve decided to hide from the eyes of the public.
  454. After one key is used to encrypt data, that same key provides the <strong>only</strong>
  455. means to decrypt it, so not only must you chose one carefully, but you
  456. must not lose it or you will also lose access to the data.</p>
  457. <p>It must be noted that to ensure maximum security, such key <em>should</em> not
  458. only be as strong as possible, but also often changed. Such behavior
  459. however is rarely practical or possible to implement, and that is why
  460. CodeIgniter gives you the ability to configure a single key that is to be
  461. used (almost) every time.</p>
  462. <p>It goes without saying that you should guard your key carefully. Should
  463. someone gain access to your key, the data will be easily decrypted. If
  464. your server is not totally under your control it’s impossible to ensure
  465. key security so you may want to think carefully before using it for
  466. anything that requires high security, like storing credit card numbers.</p>
  467. <p>Your encryption key <strong>must</strong> be as long as the encyption algorithm in use
  468. allows. For AES-128, that’s 128 bits or 16 bytes (charcters) long.
  469. You will find a table below that shows the supported key lengths of
  470. different ciphers.</p>
  471. <p>The key should be as random as possible and it <strong>must not</strong> be a regular
  472. text string, nor the output of a hashing function, etc. In order to create
  473. a proper key, you must use the Encryption library’s <code class="docutils literal"><span class="pre">create_key()</span></code> method</p>
  474. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// $key will be assigned a 16-byte (128-bit) random key</span>
  475. <span class="nv">$key</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">create_key</span><span class="p">(</span><span class="mi">16</span><span class="p">);</span>
  476. </pre></div>
  477. </div>
  478. <p>The key can be either stored in your <em>application/config/config.php</em>, or
  479. you can design your own storage mechanism and pass the key dynamically
  480. when encrypting/decrypting.</p>
  481. <p>To save your key to your <em>application/config/config.php</em>, open the file
  482. and set:</p>
  483. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$config</span><span class="p">[</span><span class="s1">&#39;encryption_key&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;YOUR KEY&#39;</span><span class="p">;</span>
  484. </pre></div>
  485. </div>
  486. <p>You’ll notice that the <code class="docutils literal"><span class="pre">create_key()</span></code> method outputs binary data, which
  487. is hard to deal with (i.e. a copy-paste may damage it), so you may use
  488. <code class="docutils literal"><span class="pre">bin2hex()</span></code>, <code class="docutils literal"><span class="pre">hex2bin()</span></code> or Base64-encoding to work with the key in
  489. a more friendly manner. For example:</p>
  490. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// Get a hex-encoded representation of the key:</span>
  491. <span class="nv">$key</span> <span class="o">=</span> <span class="nb">bin2hex</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">create_key</span><span class="p">(</span><span class="mi">16</span><span class="p">));</span>
  492. <span class="c1">// Put the same value in your config with hex2bin(),</span>
  493. <span class="c1">// so that it is still passed as binary to the library:</span>
  494. <span class="nv">$config</span><span class="p">[</span><span class="s1">&#39;encryption_key&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">hex2bin</span><span class="p">(</span><span class="o">&lt;</span><span class="nx">your</span> <span class="nx">hex</span><span class="o">-</span><span class="nx">encoded</span> <span class="nb">key</span><span class="o">&gt;</span><span class="p">);</span>
  495. </pre></div>
  496. </div>
  497. </div>
  498. <div class="section" id="supported-encryption-ciphers-and-modes">
  499. <span id="ciphers-and-modes"></span><h3><a class="toc-backref" href="#id6">Supported encryption ciphers and modes</a><a class="headerlink" href="#supported-encryption-ciphers-and-modes" title="Permalink to this headline">¶</a></h3>
  500. <div class="admonition note">
  501. <p class="first admonition-title">Note</p>
  502. <p class="last">The terms ‘cipher’ and ‘encryption algorithm’ are interchangeable.</p>
  503. </div>
  504. <div class="section" id="portable-ciphers">
  505. <h4><a class="toc-backref" href="#id7">Portable ciphers</a><a class="headerlink" href="#portable-ciphers" title="Permalink to this headline">¶</a></h4>
  506. <p>Because MCrypt and OpenSSL (also called drivers throughout this document)
  507. each support different sets of encryption algorithms and often implement
  508. them in different ways, our Encryption library is designed to use them in
  509. a portable fashion, or in other words - it enables you to use them
  510. interchangeably, at least for the ciphers supported by both drivers.</p>
  511. <p>It is also implemented in a way that aims to match the standard
  512. implementations in other programming languages and libraries.</p>
  513. <p>Here’s a list of the so called “portable” ciphers, where
  514. “CodeIgniter name” is the string value that you’d have to pass to the
  515. Encryption library to use that cipher:</p>
  516. <table border="1" class="docutils">
  517. <colgroup>
  518. <col width="24%" />
  519. <col width="18%" />
  520. <col width="28%" />
  521. <col width="31%" />
  522. </colgroup>
  523. <thead valign="bottom">
  524. <tr class="row-odd"><th class="head">Cipher name</th>
  525. <th class="head">CodeIgniter name</th>
  526. <th class="head">Key lengths (bits / bytes)</th>
  527. <th class="head">Supported modes</th>
  528. </tr>
  529. </thead>
  530. <tbody valign="top">
  531. <tr class="row-even"><td>AES-128 / Rijndael-128</td>
  532. <td>aes-128</td>
  533. <td>128 / 16</td>
  534. <td>CBC, CTR, CFB, CFB8, OFB, ECB</td>
  535. </tr>
  536. <tr class="row-odd"><td>AES-192</td>
  537. <td>aes-192</td>
  538. <td>192 / 24</td>
  539. <td>CBC, CTR, CFB, CFB8, OFB, ECB</td>
  540. </tr>
  541. <tr class="row-even"><td>AES-256</td>
  542. <td>aes-256</td>
  543. <td>256 / 32</td>
  544. <td>CBC, CTR, CFB, CFB8, OFB, ECB</td>
  545. </tr>
  546. <tr class="row-odd"><td>DES</td>
  547. <td>des</td>
  548. <td>56 / 7</td>
  549. <td>CBC, CFB, CFB8, OFB, ECB</td>
  550. </tr>
  551. <tr class="row-even"><td>TripleDES</td>
  552. <td>tripledes</td>
  553. <td>56 / 7, 112 / 14, 168 / 21</td>
  554. <td>CBC, CFB, CFB8, OFB</td>
  555. </tr>
  556. <tr class="row-odd"><td>Blowfish</td>
  557. <td>blowfish</td>
  558. <td>128-448 / 16-56</td>
  559. <td>CBC, CFB, OFB, ECB</td>
  560. </tr>
  561. <tr class="row-even"><td>CAST5 / CAST-128</td>
  562. <td>cast5</td>
  563. <td>88-128 / 11-16</td>
  564. <td>CBC, CFB, OFB, ECB</td>
  565. </tr>
  566. <tr class="row-odd"><td>RC4 / ARCFour</td>
  567. <td>rc4</td>
  568. <td>40-2048 / 5-256</td>
  569. <td>Stream</td>
  570. </tr>
  571. </tbody>
  572. </table>
  573. <div class="admonition important">
  574. <p class="first admonition-title">Important</p>
  575. <p class="last">Because of how MCrypt works, if you fail to provide a key
  576. with the appropriate length, you might end up using a different
  577. algorithm than the one configured, so be really careful with that!</p>
  578. </div>
  579. <div class="admonition note">
  580. <p class="first admonition-title">Note</p>
  581. <p class="last">In case it isn’t clear from the above table, Blowfish, CAST5
  582. and RC4 support variable length keys. That is, any number in the
  583. shown ranges is valid, although in bit terms that only happens
  584. in 8-bit increments.</p>
  585. </div>
  586. <div class="admonition note">
  587. <p class="first admonition-title">Note</p>
  588. <p class="last">Even though CAST5 supports key lengths lower than 128 bits
  589. (16 bytes), in fact they will just be zero-padded to the
  590. maximum length, as specified in <a class="reference external" href="http://tools.ietf.org/rfc/rfc2144.txt">RFC 2144</a>.</p>
  591. </div>
  592. <div class="admonition note">
  593. <p class="first admonition-title">Note</p>
  594. <p class="last">Blowfish supports key lengths as small as 32 bits (4 bytes), but
  595. our tests have shown that only lengths of 128 bits (16 bytes) or
  596. higher are properly supported by both MCrypt and OpenSSL. It is
  597. also a bad practice to use such low-length keys anyway.</p>
  598. </div>
  599. </div>
  600. <div class="section" id="driver-specific-ciphers">
  601. <h4><a class="toc-backref" href="#id8">Driver-specific ciphers</a><a class="headerlink" href="#driver-specific-ciphers" title="Permalink to this headline">¶</a></h4>
  602. <p>As noted above, MCrypt and OpenSSL support different sets of encryption
  603. ciphers. For portability reasons and because we haven’t tested them
  604. properly, we do not advise you to use the ones that are driver-specific,
  605. but regardless, here’s a list of most of them:</p>
  606. <table border="1" class="docutils">
  607. <colgroup>
  608. <col width="15%" />
  609. <col width="10%" />
  610. <col width="32%" />
  611. <col width="44%" />
  612. </colgroup>
  613. <thead valign="bottom">
  614. <tr class="row-odd"><th class="head">Cipher name</th>
  615. <th class="head">Driver</th>
  616. <th class="head">Key lengths (bits / bytes)</th>
  617. <th class="head">Supported modes</th>
  618. </tr>
  619. </thead>
  620. <tbody valign="top">
  621. <tr class="row-even"><td>AES-128</td>
  622. <td>OpenSSL</td>
  623. <td>128 / 16</td>
  624. <td>CBC, CTR, CFB, CFB8, OFB, ECB, XTS</td>
  625. </tr>
  626. <tr class="row-odd"><td>AES-192</td>
  627. <td>OpenSSL</td>
  628. <td>192 / 24</td>
  629. <td>CBC, CTR, CFB, CFB8, OFB, ECB, XTS</td>
  630. </tr>
  631. <tr class="row-even"><td>AES-256</td>
  632. <td>OpenSSL</td>
  633. <td>256 / 32</td>
  634. <td>CBC, CTR, CFB, CFB8, OFB, ECB, XTS</td>
  635. </tr>
  636. <tr class="row-odd"><td>Rijndael-128</td>
  637. <td>MCrypt</td>
  638. <td>128 / 16, 192 / 24, 256 / 32</td>
  639. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  640. </tr>
  641. <tr class="row-even"><td>Rijndael-192</td>
  642. <td>MCrypt</td>
  643. <td>128 / 16, 192 / 24, 256 / 32</td>
  644. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  645. </tr>
  646. <tr class="row-odd"><td>Rijndael-256</td>
  647. <td>MCrypt</td>
  648. <td>128 / 16, 192 / 24, 256 / 32</td>
  649. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  650. </tr>
  651. <tr class="row-even"><td>GOST</td>
  652. <td>MCrypt</td>
  653. <td>256 / 32</td>
  654. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  655. </tr>
  656. <tr class="row-odd"><td>Twofish</td>
  657. <td>MCrypt</td>
  658. <td>128 / 16, 192 / 24, 256 / 32</td>
  659. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  660. </tr>
  661. <tr class="row-even"><td>CAST-128</td>
  662. <td>MCrypt</td>
  663. <td>40-128 / 5-16</td>
  664. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  665. </tr>
  666. <tr class="row-odd"><td>CAST-256</td>
  667. <td>MCrypt</td>
  668. <td>128 / 16, 192 / 24, 256 / 32</td>
  669. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  670. </tr>
  671. <tr class="row-even"><td>Loki97</td>
  672. <td>MCrypt</td>
  673. <td>128 / 16, 192 / 24, 256 / 32</td>
  674. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  675. </tr>
  676. <tr class="row-odd"><td>SaferPlus</td>
  677. <td>MCrypt</td>
  678. <td>128 / 16, 192 / 24, 256 / 32</td>
  679. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  680. </tr>
  681. <tr class="row-even"><td>Serpent</td>
  682. <td>MCrypt</td>
  683. <td>128 / 16, 192 / 24, 256 / 32</td>
  684. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  685. </tr>
  686. <tr class="row-odd"><td>XTEA</td>
  687. <td>MCrypt</td>
  688. <td>128 / 16</td>
  689. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  690. </tr>
  691. <tr class="row-even"><td>RC2</td>
  692. <td>MCrypt</td>
  693. <td>8-1024 / 1-128</td>
  694. <td>CBC, CTR, CFB, CFB8, OFB, OFB8, ECB</td>
  695. </tr>
  696. <tr class="row-odd"><td>RC2</td>
  697. <td>OpenSSL</td>
  698. <td>8-1024 / 1-128</td>
  699. <td>CBC, CFB, OFB, ECB</td>
  700. </tr>
  701. <tr class="row-even"><td>Camellia-128</td>
  702. <td>OpenSSL</td>
  703. <td>128 / 16</td>
  704. <td>CBC, CFB, CFB8, OFB, ECB</td>
  705. </tr>
  706. <tr class="row-odd"><td>Camellia-192</td>
  707. <td>OpenSSL</td>
  708. <td>192 / 24</td>
  709. <td>CBC, CFB, CFB8, OFB, ECB</td>
  710. </tr>
  711. <tr class="row-even"><td>Camellia-256</td>
  712. <td>OpenSSL</td>
  713. <td>256 / 32</td>
  714. <td>CBC, CFB, CFB8, OFB, ECB</td>
  715. </tr>
  716. <tr class="row-odd"><td>Seed</td>
  717. <td>OpenSSL</td>
  718. <td>128 / 16</td>
  719. <td>CBC, CFB, OFB, ECB</td>
  720. </tr>
  721. </tbody>
  722. </table>
  723. <div class="admonition note">
  724. <p class="first admonition-title">Note</p>
  725. <p class="last">If you wish to use one of those ciphers, you’d have to pass
  726. its name in lower-case to the Encryption library.</p>
  727. </div>
  728. <div class="admonition note">
  729. <p class="first admonition-title">Note</p>
  730. <p class="last">You’ve probably noticed that all AES cipers (and Rijndael-128)
  731. are also listed in the portable ciphers list. This is because
  732. drivers support different modes for these ciphers. Also, it is
  733. important to note that AES-128 and Rijndael-128 are actually
  734. the same cipher, but <strong>only</strong> when used with a 128-bit key.</p>
  735. </div>
  736. <div class="admonition note">
  737. <p class="first admonition-title">Note</p>
  738. <p class="last">CAST-128 / CAST-5 is also listed in both the portable and
  739. driver-specific ciphers list. This is because OpenSSL’s
  740. implementation doesn’t appear to be working correctly with
  741. key sizes of 80 bits and lower.</p>
  742. </div>
  743. <div class="admonition note">
  744. <p class="first admonition-title">Note</p>
  745. <p class="last">RC2 is listed as supported by both MCrypt and OpenSSL.
  746. However, both drivers implement them differently and they
  747. are not portable. It is probably worth noting that we only
  748. found one obscure source confirming that it is MCrypt that
  749. is not properly implementing it.</p>
  750. </div>
  751. </div>
  752. <div class="section" id="encryption-modes">
  753. <span id="id1"></span><h4><a class="toc-backref" href="#id9">Encryption modes</a><a class="headerlink" href="#encryption-modes" title="Permalink to this headline">¶</a></h4>
  754. <p>Different modes of encryption have different characteristics and serve
  755. for different purposes. Some are stronger than others, some are faster
  756. and some offer extra features.
  757. We are not going in depth into that here, we’ll leave that to the
  758. cryptography experts. The table below is to provide brief informational
  759. reference to our more experienced users. If you are a beginner, just
  760. stick to the CBC mode - it is widely accepted as strong and secure for
  761. general purposes.</p>
  762. <table border="1" class="docutils">
  763. <colgroup>
  764. <col width="6%" />
  765. <col width="9%" />
  766. <col width="9%" />
  767. <col width="76%" />
  768. </colgroup>
  769. <thead valign="bottom">
  770. <tr class="row-odd"><th class="head">Mode name</th>
  771. <th class="head">CodeIgniter name</th>
  772. <th class="head">Driver support</th>
  773. <th class="head">Additional info</th>
  774. </tr>
  775. </thead>
  776. <tbody valign="top">
  777. <tr class="row-even"><td>CBC</td>
  778. <td>cbc</td>
  779. <td>MCrypt, OpenSSL</td>
  780. <td>A safe default choice</td>
  781. </tr>
  782. <tr class="row-odd"><td>CTR</td>
  783. <td>ctr</td>
  784. <td>MCrypt, OpenSSL</td>
  785. <td>Considered as theoretically better than CBC, but not as widely available</td>
  786. </tr>
  787. <tr class="row-even"><td>CFB</td>
  788. <td>cfb</td>
  789. <td>MCrypt, OpenSSL</td>
  790. <td>N/A</td>
  791. </tr>
  792. <tr class="row-odd"><td>CFB8</td>
  793. <td>cfb8</td>
  794. <td>MCrypt, OpenSSL</td>
  795. <td>Same as CFB, but operates in 8-bit mode (not recommended).</td>
  796. </tr>
  797. <tr class="row-even"><td>OFB</td>
  798. <td>ofb</td>
  799. <td>MCrypt, OpenSSL</td>
  800. <td>N/A</td>
  801. </tr>
  802. <tr class="row-odd"><td>OFB8</td>
  803. <td>ofb8</td>
  804. <td>MCrypt</td>
  805. <td>Same as OFB, but operates in 8-bit mode (not recommended).</td>
  806. </tr>
  807. <tr class="row-even"><td>ECB</td>
  808. <td>ecb</td>
  809. <td>MCrypt, OpenSSL</td>
  810. <td>Ignores IV (not recommended).</td>
  811. </tr>
  812. <tr class="row-odd"><td>XTS</td>
  813. <td>xts</td>
  814. <td>OpenSSL</td>
  815. <td>Usually used for encrypting random access data such as RAM or hard-disk storage.</td>
  816. </tr>
  817. <tr class="row-even"><td>Stream</td>
  818. <td>stream</td>
  819. <td>MCrypt, OpenSSL</td>
  820. <td>This is not actually a mode, it just says that a stream cipher is being used. Required because of the general cipher+mode initialization process.</td>
  821. </tr>
  822. </tbody>
  823. </table>
  824. </div>
  825. </div>
  826. <div class="section" id="message-length">
  827. <h3><a class="toc-backref" href="#id10">Message Length</a><a class="headerlink" href="#message-length" title="Permalink to this headline">¶</a></h3>
  828. <p>It’s probably important for you to know that an encrypted string is usually
  829. longer than the original, plain-text string (depending on the cipher).</p>
  830. <p>This is influenced by the cipher algorithm itself, the IV prepended to the
  831. cipher-text and the HMAC authentication message that is also prepended.
  832. Furthermore, the encrypted message is also Base64-encoded so that it is safe
  833. for storage and transmission, regardless of a possible character set in use.</p>
  834. <p>Keep this information in mind when selecting your data storage mechanism.
  835. Cookies, for example, can only hold 4K of information.</p>
  836. </div>
  837. <div class="section" id="configuring-the-library">
  838. <span id="configuration"></span><h3><a class="toc-backref" href="#id11">Configuring the library</a><a class="headerlink" href="#configuring-the-library" title="Permalink to this headline">¶</a></h3>
  839. <p>For usability, performance, but also historical reasons tied to our old
  840. <a class="reference internal" href="encrypt.html"><span class="doc">Encrypt Class</span></a>, the Encryption library is designed to
  841. use repeatedly the same driver, encryption cipher, mode and key.</p>
  842. <p>As noted in the “Default behavior” section above, this means using an
  843. auto-detected driver (OpenSSL has a higher priority), the AES-128 ciper
  844. in CBC mode, and your <code class="docutils literal"><span class="pre">$config['encryption_key']</span></code> value.</p>
  845. <p>If you wish to change that however, you need to use the <code class="docutils literal"><span class="pre">initialize()</span></code>
  846. method. It accepts an associative array of parameters, all of which are
  847. optional:</p>
  848. <table border="1" class="docutils">
  849. <colgroup>
  850. <col width="15%" />
  851. <col width="85%" />
  852. </colgroup>
  853. <thead valign="bottom">
  854. <tr class="row-odd"><th class="head">Option</th>
  855. <th class="head">Possible values</th>
  856. </tr>
  857. </thead>
  858. <tbody valign="top">
  859. <tr class="row-even"><td>driver</td>
  860. <td>‘mcrypt’, ‘openssl’</td>
  861. </tr>
  862. <tr class="row-odd"><td>cipher</td>
  863. <td>Cipher name (see <a class="reference internal" href="#ciphers-and-modes"><span class="std std-ref">Supported encryption ciphers and modes</span></a>)</td>
  864. </tr>
  865. <tr class="row-even"><td>mode</td>
  866. <td>Encryption mode (see <a class="reference internal" href="#encryption-modes"><span class="std std-ref">Encryption modes</span></a>)</td>
  867. </tr>
  868. <tr class="row-odd"><td>key</td>
  869. <td>Encryption key</td>
  870. </tr>
  871. </tbody>
  872. </table>
  873. <p>For example, if you were to change the encryption algorithm and
  874. mode to AES-256 in CTR mode, this is what you should do:</p>
  875. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">initialize</span><span class="p">(</span>
  876. <span class="k">array</span><span class="p">(</span>
  877. <span class="s1">&#39;cipher&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;aes-256&#39;</span><span class="p">,</span>
  878. <span class="s1">&#39;mode&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;ctr&#39;</span><span class="p">,</span>
  879. <span class="s1">&#39;key&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;&lt;a 32-character random string&gt;&#39;</span>
  880. <span class="p">)</span>
  881. <span class="p">);</span>
  882. </pre></div>
  883. </div>
  884. <p>Note that we only mentioned that you want to change the ciper and mode,
  885. but we also included a key in the example. As previously noted, it is
  886. important that you choose a key with a proper size for the used algorithm.</p>
  887. <p>There’s also the ability to change the driver, if for some reason you
  888. have both, but want to use MCrypt instead of OpenSSL:</p>
  889. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// Switch to the MCrypt driver</span>
  890. <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">initialize</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;driver&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;mcrypt&#39;</span><span class="p">));</span>
  891. <span class="c1">// Switch back to the OpenSSL driver</span>
  892. <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">initialize</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;driver&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;openssl&#39;</span><span class="p">));</span>
  893. </pre></div>
  894. </div>
  895. </div>
  896. <div class="section" id="encrypting-and-decrypting-data">
  897. <h3><a class="toc-backref" href="#id12">Encrypting and decrypting data</a><a class="headerlink" href="#encrypting-and-decrypting-data" title="Permalink to this headline">¶</a></h3>
  898. <p>Encrypting and decrypting data with the already configured library
  899. settings is simple. As simple as just passing the string to the
  900. <code class="docutils literal"><span class="pre">encrypt()</span></code> and/or <code class="docutils literal"><span class="pre">decrypt()</span></code> methods:</p>
  901. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$plain_text</span> <span class="o">=</span> <span class="s1">&#39;This is a plain-text message!&#39;</span><span class="p">;</span>
  902. <span class="nv">$ciphertext</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">encrypt</span><span class="p">(</span><span class="nv">$plain_text</span><span class="p">);</span>
  903. <span class="c1">// Outputs: This is a plain-text message!</span>
  904. <span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">decrypt</span><span class="p">(</span><span class="nv">$ciphertext</span><span class="p">);</span>
  905. </pre></div>
  906. </div>
  907. <p>And that’s it! The Encryption library will do everything necessary
  908. for the whole process to be cryptographically secure out-of-the-box.
  909. You don’t need to worry about it.</p>
  910. <div class="admonition important">
  911. <p class="first admonition-title">Important</p>
  912. <p class="last">Both methods will return FALSE in case of an error.
  913. While for <code class="docutils literal"><span class="pre">encrypt()</span></code> this can only mean incorrect
  914. configuration, you should always check the return value
  915. of <code class="docutils literal"><span class="pre">decrypt()</span></code> in production code.</p>
  916. </div>
  917. <div class="section" id="how-it-works">
  918. <h4><a class="toc-backref" href="#id13">How it works</a><a class="headerlink" href="#how-it-works" title="Permalink to this headline">¶</a></h4>
  919. <p>If you must know how the process works, here’s what happens under
  920. the hood:</p>
  921. <ul class="simple">
  922. <li><code class="docutils literal"><span class="pre">$this-&gt;encryption-&gt;encrypt($plain_text)</span></code><ol class="arabic">
  923. <li>Derive an encryption key and a HMAC key from your configured
  924. <em>encryption_key</em> via HKDF, using the SHA-512 digest algorithm.</li>
  925. <li>Generate a random initialization vector (IV).</li>
  926. <li>Encrypt the data via AES-128 in CBC mode (or another previously
  927. configured cipher and mode), using the above-mentioned derived
  928. encryption key and IV.</li>
  929. <li>Prepend said IV to the resulting cipher-text.</li>
  930. <li>Base64-encode the resulting string, so that it can be safely
  931. stored or transferred without worrying about character sets.</li>
  932. <li>Create a SHA-512 HMAC authentication message using the derived
  933. HMAC key to ensure data integrity and prepend it to the Base64
  934. string.</li>
  935. </ol>
  936. </li>
  937. <li><code class="docutils literal"><span class="pre">$this-&gt;encryption-&gt;decrypt($ciphertext)</span></code><ol class="arabic">
  938. <li>Derive an encryption key and a HMAC key from your configured
  939. <em>encryption_key</em> via HKDF, using the SHA-512 digest algorithm.
  940. Because your configured <em>encryption_key</em> is the same, this
  941. will produce the same result as in the <code class="docutils literal"><span class="pre">encrypt()</span></code> method
  942. above - otherwise you won’t be able to decrypt it.</li>
  943. <li>Check if the string is long enough, separate the HMAC out of
  944. it and validate if it is correct (this is done in a way that
  945. prevents timing attacks against it). Return FALSE if either of
  946. the checks fails.</li>
  947. <li>Base64-decode the string.</li>
  948. <li>Separate the IV out of the cipher-text and decrypt the said
  949. cipher-text using that IV and the derived encryption key.</li>
  950. </ol>
  951. </li>
  952. </ul>
  953. </div>
  954. <div class="section" id="using-custom-parameters">
  955. <span id="custom-parameters"></span><h4><a class="toc-backref" href="#id14">Using custom parameters</a><a class="headerlink" href="#using-custom-parameters" title="Permalink to this headline">¶</a></h4>
  956. <p>Let’s say you have to interact with another system that is out
  957. of your control and uses another method to encrypt data. A
  958. method that will most certainly not match the above-described
  959. sequence and probably not use all of the steps either.</p>
  960. <p>The Encryption library allows you to change how its encryption
  961. and decryption processes work, so that you can easily tailor a
  962. custom solution for such situations.</p>
  963. <div class="admonition note">
  964. <p class="first admonition-title">Note</p>
  965. <p class="last">It is possible to use the library in this way, without
  966. setting an <em>encryption_key</em> in your configuration file.</p>
  967. </div>
  968. <p>All you have to do is to pass an associative array with a few
  969. parameters to either the <code class="docutils literal"><span class="pre">encrypt()</span></code> or <code class="docutils literal"><span class="pre">decrypt()</span></code> method.
  970. Here’s an example:</p>
  971. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// Assume that we have $ciphertext, $key and $hmac_key</span>
  972. <span class="c1">// from on outside source</span>
  973. <span class="nv">$message</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">decrypt</span><span class="p">(</span>
  974. <span class="nv">$ciphertext</span><span class="p">,</span>
  975. <span class="k">array</span><span class="p">(</span>
  976. <span class="s1">&#39;cipher&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;blowfish&#39;</span><span class="p">,</span>
  977. <span class="s1">&#39;mode&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;cbc&#39;</span><span class="p">,</span>
  978. <span class="s1">&#39;key&#39;</span> <span class="o">=&gt;</span> <span class="nv">$key</span><span class="p">,</span>
  979. <span class="s1">&#39;hmac_digest&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;sha256&#39;</span><span class="p">,</span>
  980. <span class="s1">&#39;hmac_key&#39;</span> <span class="o">=&gt;</span> <span class="nv">$hmac_key</span>
  981. <span class="p">)</span>
  982. <span class="p">);</span>
  983. </pre></div>
  984. </div>
  985. <p>In the above example, we are decrypting a message that was encrypted
  986. using the Blowfish cipher in CBC mode and authenticated via a SHA-256
  987. HMAC.</p>
  988. <div class="admonition important">
  989. <p class="first admonition-title">Important</p>
  990. <p class="last">Note that both ‘key’ and ‘hmac_key’ are used in this
  991. example. When using custom parameters, encryption and HMAC keys
  992. are not derived like the default behavior of the library is.</p>
  993. </div>
  994. <p>Below is a list of the available options.</p>
  995. <p>However, unless you really need to and you know what you are doing,
  996. we advise you to not change the encryption process as this could
  997. impact security, so please do so with caution.</p>
  998. <table border="1" class="docutils">
  999. <colgroup>
  1000. <col width="12%" />
  1001. <col width="14%" />
  1002. <col width="26%" />
  1003. <col width="49%" />
  1004. </colgroup>
  1005. <thead valign="bottom">
  1006. <tr class="row-odd"><th class="head">Option</th>
  1007. <th class="head">Default value</th>
  1008. <th class="head">Mandatory / Optional</th>
  1009. <th class="head">Description</th>
  1010. </tr>
  1011. </thead>
  1012. <tbody valign="top">
  1013. <tr class="row-even"><td>cipher</td>
  1014. <td>N/A</td>
  1015. <td>Yes</td>
  1016. <td>Encryption algorithm (see <a class="reference internal" href="#ciphers-and-modes"><span class="std std-ref">Supported encryption ciphers and modes</span></a>).</td>
  1017. </tr>
  1018. <tr class="row-odd"><td>mode</td>
  1019. <td>N/A</td>
  1020. <td>Yes</td>
  1021. <td>Encryption mode (see <a class="reference internal" href="#encryption-modes"><span class="std std-ref">Encryption modes</span></a>).</td>
  1022. </tr>
  1023. <tr class="row-even"><td>key</td>
  1024. <td>N/A</td>
  1025. <td>Yes</td>
  1026. <td>Encryption key.</td>
  1027. </tr>
  1028. <tr class="row-odd"><td>hmac</td>
  1029. <td>TRUE</td>
  1030. <td>No</td>
  1031. <td>Whether to use a HMAC.
  1032. Boolean. If set to FALSE, then <em>hmac_digest</em> and
  1033. <em>hmac_key</em> will be ignored.</td>
  1034. </tr>
  1035. <tr class="row-even"><td>hmac_digest</td>
  1036. <td>sha512</td>
  1037. <td>No</td>
  1038. <td>HMAC message digest algorithm (see <a class="reference internal" href="#digests"><span class="std std-ref">Supported HMAC authentication algorithms</span></a>).</td>
  1039. </tr>
  1040. <tr class="row-odd"><td>hmac_key</td>
  1041. <td>N/A</td>
  1042. <td>Yes, unless <em>hmac</em> is FALSE</td>
  1043. <td>HMAC key.</td>
  1044. </tr>
  1045. <tr class="row-even"><td>raw_data</td>
  1046. <td>FALSE</td>
  1047. <td>No</td>
  1048. <td>Whether the cipher-text should be raw.
  1049. Boolean. If set to TRUE, then Base64 encoding and
  1050. decoding will not be performed and HMAC will not
  1051. be a hexadecimal string.</td>
  1052. </tr>
  1053. </tbody>
  1054. </table>
  1055. <div class="admonition important">
  1056. <p class="first admonition-title">Important</p>
  1057. <p class="last"><code class="docutils literal"><span class="pre">encrypt()</span></code> and <code class="docutils literal"><span class="pre">decrypt()</span></code> will return FALSE if
  1058. a mandatory parameter is not provided or if a provided
  1059. value is incorrect. This includes <em>hmac_key</em>, unless <em>hmac</em>
  1060. is set to FALSE.</p>
  1061. </div>
  1062. </div>
  1063. <div class="section" id="supported-hmac-authentication-algorithms">
  1064. <span id="digests"></span><h4><a class="toc-backref" href="#id15">Supported HMAC authentication algorithms</a><a class="headerlink" href="#supported-hmac-authentication-algorithms" title="Permalink to this headline">¶</a></h4>
  1065. <p>For HMAC message authentication, the Encryption library supports
  1066. usage of the SHA-2 family of algorithms:</p>
  1067. <table border="1" class="docutils">
  1068. <colgroup>
  1069. <col width="19%" />
  1070. <col width="34%" />
  1071. <col width="47%" />
  1072. </colgroup>
  1073. <thead valign="bottom">
  1074. <tr class="row-odd"><th class="head">Algorithm</th>
  1075. <th class="head">Raw length (bytes)</th>
  1076. <th class="head">Hex-encoded length (bytes)</th>
  1077. </tr>
  1078. </thead>
  1079. <tbody valign="top">
  1080. <tr class="row-even"><td>sha512</td>
  1081. <td>64</td>
  1082. <td>128</td>
  1083. </tr>
  1084. <tr class="row-odd"><td>sha384</td>
  1085. <td>48</td>
  1086. <td>96</td>
  1087. </tr>
  1088. <tr class="row-even"><td>sha256</td>
  1089. <td>32</td>
  1090. <td>64</td>
  1091. </tr>
  1092. <tr class="row-odd"><td>sha224</td>
  1093. <td>28</td>
  1094. <td>56</td>
  1095. </tr>
  1096. </tbody>
  1097. </table>
  1098. <p>The reason for not including other popular algorithms, such as
  1099. MD5 or SHA1 is that they are no longer considered secure enough
  1100. and as such, we don’t want to encourage their usage.
  1101. If you absolutely need to use them, it is easy to do so via PHP’s
  1102. native <a class="reference external" href="http://php.net/manual/en/function.hash-hmac.php">hash_hmac()</a> function.</p>
  1103. <p>Stronger algorithms of course will be added in the future as they
  1104. appear and become widely available.</p>
  1105. </div>
  1106. </div>
  1107. </div>
  1108. <div class="section" id="class-reference">
  1109. <h2><a class="toc-backref" href="#id16">Class Reference</a><a class="headerlink" href="#class-reference" title="Permalink to this headline">¶</a></h2>
  1110. <dl class="class">
  1111. <dt id="CI_Encryption">
  1112. <em class="property">class </em><code class="descname">CI_Encryption</code><a class="headerlink" href="#CI_Encryption" title="Permalink to this definition">¶</a></dt>
  1113. <dd><dl class="method">
  1114. <dt id="CI_Encryption::initialize">
  1115. <code class="descname">initialize</code><span class="sig-paren">(</span><em>$params</em><span class="sig-paren">)</span><a class="headerlink" href="#CI_Encryption::initialize" title="Permalink to this definition">¶</a></dt>
  1116. <dd><table class="docutils field-list" frame="void" rules="none">
  1117. <col class="field-name" />
  1118. <col class="field-body" />
  1119. <tbody valign="top">
  1120. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
  1121. <li><strong>$params</strong> (<em>array</em>) – Configuration parameters</li>
  1122. </ul>
  1123. </td>
  1124. </tr>
  1125. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">CI_Encryption instance (method chaining)</p>
  1126. </td>
  1127. </tr>
  1128. <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CI_Encryption</p>
  1129. </td>
  1130. </tr>
  1131. </tbody>
  1132. </table>
  1133. <p>Initializes (configures) the library to use a different
  1134. driver, cipher, mode or key.</p>
  1135. <p>Example:</p>
  1136. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">initialize</span><span class="p">(</span>
  1137. <span class="k">array</span><span class="p">(</span><span class="s1">&#39;mode&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;ctr&#39;</span><span class="p">)</span>
  1138. <span class="p">);</span>
  1139. </pre></div>
  1140. </div>
  1141. <p>Please refer to the <a class="reference internal" href="#configuration"><span class="std std-ref">Configuring the library</span></a> section for detailed info.</p>
  1142. </dd></dl>
  1143. <dl class="method">
  1144. <dt id="CI_Encryption::encrypt">
  1145. <code class="descname">encrypt</code><span class="sig-paren">(</span><em>$data</em><span class="optional">[</span>, <em>$params = NULL</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Encryption::encrypt" title="Permalink to this definition">¶</a></dt>
  1146. <dd><table class="docutils field-list" frame="void" rules="none">
  1147. <col class="field-name" />
  1148. <col class="field-body" />
  1149. <tbody valign="top">
  1150. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
  1151. <li><strong>$data</strong> (<em>string</em>) – Data to encrypt</li>
  1152. <li><strong>$params</strong> (<em>array</em>) – Optional parameters</li>
  1153. </ul>
  1154. </td>
  1155. </tr>
  1156. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">Encrypted data or FALSE on failure</p>
  1157. </td>
  1158. </tr>
  1159. <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p>
  1160. </td>
  1161. </tr>
  1162. </tbody>
  1163. </table>
  1164. <p>Encrypts the input data and returns its ciphertext.</p>
  1165. <p>Example:</p>
  1166. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$ciphertext</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">encrypt</span><span class="p">(</span><span class="s1">&#39;My secret message&#39;</span><span class="p">);</span>
  1167. </pre></div>
  1168. </div>
  1169. <p>Please refer to the <a class="reference internal" href="#custom-parameters"><span class="std std-ref">Using custom parameters</span></a> section for information
  1170. on the optional parameters.</p>
  1171. </dd></dl>
  1172. <dl class="method">
  1173. <dt id="CI_Encryption::decrypt">
  1174. <code class="descname">decrypt</code><span class="sig-paren">(</span><em>$data</em><span class="optional">[</span>, <em>$params = NULL</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Encryption::decrypt" title="Permalink to this definition">¶</a></dt>
  1175. <dd><table class="docutils field-list" frame="void" rules="none">
  1176. <col class="field-name" />
  1177. <col class="field-body" />
  1178. <tbody valign="top">
  1179. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
  1180. <li><strong>$data</strong> (<em>string</em>) – Data to decrypt</li>
  1181. <li><strong>$params</strong> (<em>array</em>) – Optional parameters</li>
  1182. </ul>
  1183. </td>
  1184. </tr>
  1185. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">Decrypted data or FALSE on failure</p>
  1186. </td>
  1187. </tr>
  1188. <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p>
  1189. </td>
  1190. </tr>
  1191. </tbody>
  1192. </table>
  1193. <p>Decrypts the input data and returns it in plain-text.</p>
  1194. <p>Example:</p>
  1195. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">decrypt</span><span class="p">(</span><span class="nv">$ciphertext</span><span class="p">);</span>
  1196. </pre></div>
  1197. </div>
  1198. <p>Please refer to the <a class="reference internal" href="#custom-parameters"><span class="std std-ref">Using custom parameters</span></a> secrion for information
  1199. on the optional parameters.</p>
  1200. </dd></dl>
  1201. <dl class="method">
  1202. <dt id="CI_Encryption::create_key">
  1203. <code class="descname">create_key</code><span class="sig-paren">(</span><em>$length</em><span class="sig-paren">)</span><a class="headerlink" href="#CI_Encryption::create_key" title="Permalink to this definition">¶</a></dt>
  1204. <dd><table class="docutils field-list" frame="void" rules="none">
  1205. <col class="field-name" />
  1206. <col class="field-body" />
  1207. <tbody valign="top">
  1208. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
  1209. <li><strong>$length</strong> (<em>int</em>) – Output length</li>
  1210. </ul>
  1211. </td>
  1212. </tr>
  1213. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">A pseudo-random cryptographic key with the specified length, or FALSE on failure</p>
  1214. </td>
  1215. </tr>
  1216. <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p>
  1217. </td>
  1218. </tr>
  1219. </tbody>
  1220. </table>
  1221. <p>Creates a cryptographic key by fetching random data from
  1222. the operating system’s sources (i.e. /dev/urandom).</p>
  1223. </dd></dl>
  1224. <dl class="method">
  1225. <dt id="CI_Encryption::hkdf">
  1226. <code class="descname">hkdf</code><span class="sig-paren">(</span><em>$key</em><span class="optional">[</span>, <em>$digest = 'sha512'</em><span class="optional">[</span>, <em>$salt = NULL</em><span class="optional">[</span>, <em>$length = NULL</em><span class="optional">[</span>, <em>$info = ''</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Encryption::hkdf" title="Permalink to this definition">¶</a></dt>
  1227. <dd><table class="docutils field-list" frame="void" rules="none">
  1228. <col class="field-name" />
  1229. <col class="field-body" />
  1230. <tbody valign="top">
  1231. <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
  1232. <li><strong>$key</strong> (<em>string</em>) – Input key material</li>
  1233. <li><strong>$digest</strong> (<em>string</em>) – A SHA-2 family digest algorithm</li>
  1234. <li><strong>$salt</strong> (<em>string</em>) – Optional salt</li>
  1235. <li><strong>$length</strong> (<em>int</em>) – Optional output length</li>
  1236. <li><strong>$info</strong> (<em>string</em>) – Optional context/application-specific info</li>
  1237. </ul>
  1238. </td>
  1239. </tr>
  1240. <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">A pseudo-random key or FALSE on failure</p>
  1241. </td>
  1242. </tr>
  1243. <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p>
  1244. </td>
  1245. </tr>
  1246. </tbody>
  1247. </table>
  1248. <p>Derives a key from another, presumably weaker key.</p>
  1249. <p>This method is used internally to derive an encryption and HMAC key
  1250. from your configured <em>encryption_key</em>.</p>
  1251. <p>It is publicly available due to its otherwise general purpose. It is
  1252. described in <a class="reference external" href="https://tools.ietf.org/rfc/rfc5869.txt">RFC 5869</a>.</p>
  1253. <p>However, as opposed to the description in RFC 5869, this implementation
  1254. doesn’t support SHA1.</p>
  1255. <p>Example:</p>
  1256. <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$hmac_key</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">encryption</span><span class="o">-&gt;</span><span class="na">hkdf</span><span class="p">(</span>
  1257. <span class="nv">$key</span><span class="p">,</span>
  1258. <span class="s1">&#39;sha512&#39;</span><span class="p">,</span>
  1259. <span class="k">NULL</span><span class="p">,</span>
  1260. <span class="k">NULL</span><span class="p">,</span>
  1261. <span class="s1">&#39;authentication&#39;</span>
  1262. <span class="p">);</span>
  1263. <span class="c1">// $hmac_key is a pseudo-random key with a length of 64 bytes</span>
  1264. </pre></div>
  1265. </div>
  1266. </dd></dl>
  1267. </dd></dl>
  1268. </div>
  1269. </div>
  1270. </div>
  1271. <footer>
  1272. <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
  1273. <a href="file_uploading.html" class="btn btn-neutral float-right" title="File Uploading Class">Next <span class="fa fa-arrow-circle-right"></span></a>
  1274. <a href="encrypt.html" class="btn btn-neutral" title="Encrypt Class"><span class="fa fa-arrow-circle-left"></span> Previous</a>
  1275. </div>
  1276. <hr/>
  1277. <div role="contentinfo">
  1278. <p>
  1279. &copy; Copyright 2014 - 2019, British Columbia Institute of Technology.
  1280. Last updated on Sep 19, 2019.
  1281. </p>
  1282. </div>
  1283. Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
  1284. </footer>
  1285. </div>
  1286. </div>
  1287. </section>
  1288. </div>
  1289. <script type="text/javascript">
  1290. var DOCUMENTATION_OPTIONS = {
  1291. URL_ROOT:'../',
  1292. VERSION:'3.1.11',
  1293. COLLAPSE_INDEX:false,
  1294. FILE_SUFFIX:'.html',
  1295. HAS_SOURCE: false
  1296. };
  1297. </script>
  1298. <script type="text/javascript" src="../_static/jquery.js"></script>
  1299. <script type="text/javascript" src="../_static/underscore.js"></script>
  1300. <script type="text/javascript" src="../_static/doctools.js"></script>
  1301. <script type="text/javascript" src="../_static/js/theme.js"></script>
  1302. <script type="text/javascript">
  1303. jQuery(function () {
  1304. SphinxRtdTheme.StickyNav.enable();
  1305. });
  1306. </script>
  1307. </body>
  1308. </html>