Jasypt 问题的浮点加密
我需要加密浮点值,我正在使用 Jasypt 来实现这一点。
据我所知,Jasypt不支持float加密,只支持BigDecimal。因此,我将浮点值转换为 BigDecimal。
转换成功完成。还使用Jasypt进行加密和解密。
但是,当我将加密值保存到 Oracle DB 中时,该值在数据库中发生了更改。
我需要保留的值的示例:
-6542850164453273769179743775075308980128742113.12 -4139490689573544701682206282760323584523816140.64 9936653106931456268018508106437020093773774849.6 -69457501008740608752977363196163239676824308939.2 -512974351190591202428175056439128604458367.320048
我在oracle中使用数字数据类型。
问题是,如何保存上述值而不允许数据库更改它们?
DB 始终从值中删除 (.) 并添加零。
该值 689612971966376606053641908553771273056281.427984 保存为: -2173339361221855962557234522978985207570000000000000000000000 00000000000000000000000000000000000000000000000000000000000000
为什么会出现这种情况?
I need to encrypt float values and I am using Jasypt to achive this.
As far as I know, Jasypt does not support float encryption and only supports BigDecimal. Therefore, I am converting float values to BigDecimal.
The conversion is done successfully. Also the encryption and decryption using Jasypt.
However, when I persist the encrypted value into the Oracle DB, the value is changed in the DB.
Example of values that I need to persist as it is:
-6542850164453273769179743775075308980128742113.12
-4139490689573544701682206282760323584523816140.64
9936653106931456268018508106437020093773774849.6
-69457501008740608752977363196163239676824308939.2
-512974351190591202428175056439128604458367.320048
I am using Number data type in the oracle.
The questionis, how to save the above values without allowing the DB to change them?
The DB always remove the ( . ) from the value and adds zeros
This value 689612971966376606053641908553771273056281.427984 is saved as:
-217333936122185596255723452297898520757000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Why this happens?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您依赖于从数据库中出来的数字与您输入的数字完全相同 - 虽然对您来说获得准确的结果似乎很奇怪(就术语而言)否定)我并不完全惊讶它通常不起作用。您必须非常仔细地选择数据库类型,才能再次获得准确的值 - 特别是,它必须具有与加密结果一样高的精度。
还不清楚加密是否依赖于 BigDecimal 的规模(即 1.0 和 1.00 的表示方式不同)。我不知道你是否会说服数据库保留比例信息。
我强烈建议您使用更普通的加密机制 - 基本上加密为二进制数据,将其作为某种 blob 存储在数据库中,然后从 blob 解密为原始数据。与执行浮点到定点转换相比,在数据库中存储 blob 时遇到微妙的表示问题的可能性要小得多。
You're relying on the number coming out of the database being exactly the same as the number you put in - and while it seems very odd for you to get exactly the results you're getting (in terms of negation) I'm not wholly surprised that it doesn't work in general. You would have to pick your database type really carefully to get the exact value out again - in particular, it would have to have as much precision as the results of the encryption.
It's also not clear whether the encryption relies on the scale of the
BigDecimal
(i.e. where 1.0 and 1.00 are represented differently). I don't know whether you'll persuade the database to preserve the scale information.I would strongly advise you to use more normal encryption mechanisms - basically encrypt to binary data, store it as a blob of some kind in the database, and the decrypt from the blob to the original data. You're much less likely to run into subtle representation problems storing a blob in a database than performing floating point to fixed point conversions.
Java
float
是使用 32 位存储的 IEEE-754 浮点数。我们可以将这些位转储到一个方便的 32 位宽的 Java
int
中,并且我们还可以将int
转换回 float:Float.floatToIntBits(), Float.intBitsToFloat()。请注意,
int
可以是正值,也可以是负值;它的范围为 [−(231), (231) − 1]。Jasypt 提供了一种加密
BigInteger< /code>s
,因此我们可以将
int
转换为BigInteger
,对其进行加密,并将其存储在数据库中。这是加密代码的草图:
以及解密代码的草图:
A Java
float
is an IEEE-754 floating-point number that uses 32 bits of storage.We can dump these bits into a Java
int
which is conveniently 32 bits wide, and we can also convert theint
back to a float: Float.floatToIntBits(), Float.intBitsToFloat().Note that the
int
may be positive or negative; it has the range [−(231), (231) − 1].Jasypt provides a way to encrypt
BigInteger
s, so we can convert theint
to aBigInteger
, encrypt that, and store it in the database.Here's a sketch of the encryption code:
And a sketch of the decryption code: