使加密字段的值唯一 [英] Making a Value of an Encrypted Field Unique

查看:162
本文介绍了使加密字段的值唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaEE应用程序使用Jasypt简单加密器使用JPA加密存储在MySQL数据库中的用户名.

A JavaEE application uses Jasypt simple encryptor to encrypt usernames stored in a MySQL database using JPA.

由于用户名已加密,因此不能使用户名列唯一. (请参阅下面的用例.)

As the usernames are encrypted, can't make the username column unique. (Refer to use cases below.)

可以在保存新用户名之前执行搜索以避免重复,但是理论上可以同时创建多个用户名.

A search can be performed before saving a new username to avoid duplicates, but there is a theoretical possibility of creating multiple users names simultaneously.

Encrypt('username','key') = 'EncryptedUsername'

Decrypt('EncryptedUsername','key') = 'username'

案例1-已经在数据库中

Case 1 - Already in the database

  • 用户名1 ='a'

  • Username1 = 'a'

Key1 ='1'

EncryptedValue1 ='XXXX'(假设)

EncryptedValue1 = 'XXXX' (suppose)

情况2-要添加新的用户名

Case 2 - Going to add a new username

  • 用户名2 ='b'

  • Username2 = 'b'

Key2 ='2'

EncryptedValue2 ='XXXX'(假设)

EncryptedValue2 = 'XXXX' (suppose)

情况3-要添加另一个新的用户名

Case 3 - Going to add another new username

  • 用户名3 ='a'

  • Username3 = 'a'

Key3 ='3'

EncryptedValue3 ='YYYY'(假设)

EncryptedValue3 = 'YYYY' (suppose)

必须允许第2种情况.

不允许出现第3种情况

我无法通过使字段存储加密值唯一来实现这一点.

I can not achieve that by making the field I store the encrypted value unique.

如果我使它唯一,则不允许在情况2中添加新的用户名'b'.这是错误的,因为现有的用户名'a'与新的用户名'b'不同.

If I make it unique, it will not allow adding new username 'b' in case 2. That is wrong as existing username 'a' is different from the new username 'b'.

在第3种情况下,由于加密值不同,它也将允许添加"a"作为用户名.

In case 3, it will also allow adding the 'a' as a username as the encrypted values are different.

我们如何确保用户名在加密后仍保持唯一?

How can we make sure the user name remains unique despite encryption?

推荐答案

使用应用程序范围Bean搜索提供的用户名.如果找不到新的用户名,请使用新的用户名创建一个用户.如果找到了用户名,请从用户那里请求另一个.由于只有一个地方可以执行此功能,因此不会重复.

Use an application scope bean to search for the provided username. If the new username is not found, create a user with the new username. If the username found, ask for another one from the user. As there is only one place to carry out this function, there will be no duplicates.

这篇关于使加密字段的值唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆