SugarCRM comes with a variety of modules that store values in the database. Some of those values are encrypted. For example, mailbox passwords for inbound and outbound email configurations.
When you create this configurations through the web interface or the API, you don’t need to worry about encryption, as SugarCRM handles that all by itself. But sometimes, you need to access those values from third-party code. The easiest way would be of course to use the same API functionality, but this is not always possible (different machines, different technology stack, etc).
It is still possible decrypt the values in the database, if you know where to look.
First of all, here is a little side note for InboundEmail and OutboundEmail modules. InboundEmail is a full-featured module, which you can find in modules/InboundEmail folder. OutboundEmail is however not – it lives in include/OutboundEmail . This might seem surprising, but the reason for this (probably, as I don’t know for sure) is that outbound email configuration is much simpler. Inbound emails are linked with folders, which are then used to subscribe users, etc. Outbound emails are just SMTP configurations to use, directly linked to users.
Anyways. Let’s get back on track.
Most of the encryption and decryption magic happens in include/utils/encryption_utils.php. If you look through the code, you’ll notice that it deals with mostly two things:
- Generating or reading an existing encryption key.
- Encrypting or decrypting text with Blowfish, using the encryptionkey.
Encryption keys are stored in custom/blowfish/ folder. The files that you’ll find there have weird names and a .php extension. The name of the file comes from the module, for which the key will be used. ROT13 algorithm is used to convert the name of the module into the file name. (Note, that for outbound email, the name of the module is OutBoundEmail, not OutboundEmail).
If the encryption key file does not exist, a new one will be generated. The file will contain a PHP snippet like this:
<?php // created: 2016-04-18 10:00:00 $key = array ( 0 => 'a0a0a0a0-b1b1-c3c3-d4d4-e5e5e5e5e5e5', );
If you accidentally remove the file, then you won’t be able to decrypt any of the values, encrypted with this key, so make sure you backup this up. Especially considering that this folder might be in your .gitignore, as a sub-folder of custom/ which stored lots of auto-generated stuff.
Note that the file actually defines a $key variable, which, if you will include it in your code, can overwrite your $key variable. So, be warned.
A little note for the above as well. The Blowfish.php file which contains the Crypt_Blowfish class, requires the Blowfish/DefaultKey.php file (from the setKey() method). That requirement uses relative path, but not based on the current file. Yeah, I know. So, if you just copy over the library somewhere else, you might need to adjust either path variables, or the setKey() method.
Armed with this knowledge, you can now work with encrypted values stored by the SugarCRM in the database. Good luck!