Coldfusion ENCRYPT 和 MySQL AES_DECRYPT 一起工作吗?
我正在使用 ColdFusion 9 和 MySQL 5.1。我正在尝试调整 ColdFusion 加密/解密函数和 mySQL AES_ENCRYPT/AES_DECRYPT,以便我可以根据情况互换使用它们。对此运气不太好。
首先,我使用 ColdFusion 创建了一个 AES 字符串:
<cfset theKey = generateSecretKey("AES") />
<cfoutput>#theKey#</cfoutput>
示例密钥:4OFWUiuqFEkGrSRFm8sLlg==
我使用此密钥通过 MySQL 进行加密。请注意,encrypt_test 是现有表,fld 是 varchar 列。
INSERT INTO encrypt_test
SET fld = aes_encrypt('the text to encrypt', '4OFWUiuqFEkGrSRFm8sLlg==')
接下来我尝试使用 ColdFusion 进行解密:
<cfset theKey = "4OFWUiuqFEkGrSRFm8sLlg=="
<cfset theAlgorithm = "AES" />
然后运行 cfquery 来获取数据(表中只有 1 条记录),
<cfquery name="testDecrypt">
SELECT fld FROM encrypt_test
</cfquery`
最后解密
<cfoutput>#Decrypt(testDecrypt.fld, theKey, theAlgorithm)#</cfoutput>
这会导致 Null
。我怀疑这是一个填充问题或其他一些不匹配的问题,有人知道我做错了什么,或者如何使这项工作有效?
I am using ColdFusion 9, and MySQL 5.1. I am trying to align the ColdFusion encrypt/decrypt functions and mySQL AES_ENCRYPT/AES_DECRYPT so I can use them interchangeably depending on the situation. Not having much luck with that.
First I created an AES string with ColdFusion:
<cfset theKey = generateSecretKey("AES") />
<cfoutput>#theKey#</cfoutput>
Example key: 4OFWUiuqFEkGrSRFm8sLlg==
I use this key to encrypt with MySQL. Note, encrypt_test is an existing table, and fld is a varchar column.
INSERT INTO encrypt_test
SET fld = aes_encrypt('the text to encrypt', '4OFWUiuqFEkGrSRFm8sLlg==')
Next I try to decrypt with ColdFusion:
<cfset theKey = "4OFWUiuqFEkGrSRFm8sLlg=="
<cfset theAlgorithm = "AES" />
Then run a cfquery to get the data (Only 1 record in the table),
<cfquery name="testDecrypt">
SELECT fld FROM encrypt_test
</cfquery`
And finally decrypt
<cfoutput>#Decrypt(testDecrypt.fld, theKey, theAlgorithm)#</cfoutput>
This results in a Null
. I suspect its a padding issue or some other mismatch, anyone have an idea what I am doing wrong, or how to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道这个帖子已经很旧了,但答案出现在最近的帖子上。所以我将其发布给后代。正如本博客中所述条目,差异的原因是:
因此,在将键值传递到
加密/解密
之前,您需要对键值执行相同的操作。I know this thread is old, but the answer came up on a recent thread. So I am posting it for posterity. As explained in this blog entry, the reason for the difference is:
So you need to perform the same manipulations on the key value, before passing it into
encrypt/decrypt
.我会坚持只使用 CF 的功能。这样,您就可以添加各种安全流程层,包括迭代和多个密钥等内容,从而轻松构建自定义解决方案。它所增加的开销也并不多。
I would stick with just using CF's functions. That way you can add all kinds of layers of security processes, to include things like iterations and multiple keys, to build a custom solution with ease. THe amount of overhead it adds is not much at all for that as well.
为什么不使用 ColdFusion 的加密功能而不是 MySQL 的呢?
事实上,这是测试问题所在的一种方法:尝试输出数据库中的加密值以及 CF 的加密函数将生成的值,看看它们是否相同。
或者,只需在查询中使用 aes_decrypt 函数,而不是使用 ColdFusion 的解密。
嗯,来自文档:
因此,假设 CFML 不进行填充,您就必须自己找出相反的方法或其他方法。
Why don't you use ColdFusion's encrypt function instead of MySQL's?
In fact that would be one way to test where the problem might lie : try outputting both the encrypted value from your database and what CF's encrypt function would produce and see if they're identical.
Alternatively just use the aes_decrypt function in your query instead of using ColdFusion's decrypt.
Hmmm, from the docs:
So assuming CFML doesn't do that padding, you'd have to figure out the reverse of this yourself or something.
我知道这是一篇很旧的帖子,但这是你应该做的:
在存储到数据库之前:
然后:
它对我有用
I know it's quite an old post but here is what you should do:
Before storing into the DB:
Then:
It worked for me
使用 jBCrypt :: bCrypt 是可用的最强加密......在 Mark Mandel 的 Fantastic 的帮助下JavaLoader
在 ColdFusion 中实现 jBCrypt 是小菜一碟...
就密码字段而言,您使用的数据库类型并不重要...如果您是,该字段可以是 varchar(60) 或 nvarchar(60)也处理区域设置支持......
密码哈希.cfc ...
... yada yada ... 更多代码 ...
Use jBCrypt :: bCrypt is the strongest encryption available ... with the assistance of Mark Mandel's Fantastic JavaLoader
implementing jBCrypt is a snap in ColdFusion ...
As far as the password field it really doesn't matter what kind of database you're using ... the field could be varchar(60) or nvarchar(60) if you're dealing with locale support too...
The PasswordHash.cfc ...
... yada yada ... more code ...