我目前正在将 Jasypt 用于我的网络应用程序。它工作正常,但加密方式有所不同,具体取决于托管的服务器。
因此,我不能只获取实时数据库中的数据并将其用于在我的开发环境中进行调试。这会很有帮助,但没有它我也能生活。
令我担心的是,目前我正在使用托管提供商。到目前为止一切都很好,但我担心如果在某个时候他们更换服务器,或者将我的应用程序转移到另一台服务器,加密数据(例如用于登录的电子邮件和密码等)将不会被加密同样的方式,所有数据将变得无法使用。
有人知道 Jasypt 的独立平台替代品吗?
或者有没有办法让Jasypt本身平台独立?
谢谢,丹
PS:我需要一种具有以下基本功能的方法:字符串加密(可逆)、密码加密(不可逆但可比较)和“SHA-1”加密。如果最后一段中的术语不是最正确,我深表歉意,但我根本不是加密专家。谢谢!
编辑添加代码、结果和异常:
public class Test
{
public static void main ( String [] args )
{
System.out.println ( "String encryption = " + new EncryptionUtil ( ).encryptString ( "test string" ) );
System.out.println ( "Password encryption = " + new EncryptionUtil ( ).encryptPassword ( "test password" ) );
}
}
在开发环境中生成此内容:
String encryption = ybXukKBN57QSY8ITPgu9RmJQrZP4Py6g
Password encryption = nNX82PuKx5TrqBFSCy6yzNpco7Asov2S
每次输出都不同,但可以解密字符串,并通过执行以下操作来比较密码:
public class Decryption
{
public static void main ( String [] args )
{
System.out.println ( new EncryptionUtil ( ).decryptString ( "ybXukKBN57QSY8ITPgu9RmJQrZP4Py6g" ) );
System.out.println ( new EncryptionUtil ( ).passwordsMatch ( "test password", "nNX82PuKx5TrqBFSCy6yzNpco7Asov2S" ) );
}
}
这给出了这个输出:
test string
true
这是我创建的加密实用程序类:
public class EncryptionUtil
{
private String password = "<<=Encryption-Password=>>";
// ============================================================ Encrypt password string
public String encryptPassword ( String pwd )
{
if ( null != pwd && ! "".equals ( pwd ) )
{
return new BasicPasswordEncryptor ().encryptPassword ( pwd );
}
else
{
return "";
}
}
// ====================================== Check if password entered matches that stored
public boolean passwordsMatch ( String enteredPassword, String storedPassword )
{
return new BasicPasswordEncryptor().checkPassword ( enteredPassword, storedPassword );
}
//===================================================================== Encrypt string
public String encryptString ( String text )
{
if ( null != password && ! "".equals ( password ) )
{
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword ( password );
return textEncryptor.encrypt ( text );
}
else
{
return "";
}
}
// ===================================================================== Decrypt string
public String decryptString ( String text )
{
try
{
if ( null != text && ! "".equals ( text ) )
{
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword ( password );
return textEncryptor.decrypt ( text );
}
else
{
return "";
}
}
catch ( Exception e )
{
return text;
}
}
// =============== Encrypt email. Used for login and registration only, not decryptable
public String encryptEmail ( String email )
{
if ( null != email && ! "".equals ( email ) )
{
return new String ( new Digester("SHA-1").digest ( email.getBytes () ) );
}
else
{
return "";
}
}
}
在 LIVE 环境中同样的情况给了我这个:
String encryption = L/UlkJjYhLnYiov7XeDjb9W7+k8Gduvz
Password encryption = P+LJM7VJHu/hudSQOrmvcvV/DrzCv+pj
当我尝试解密字符串并检查密码时,我得到:
public class Decryption
{
public static void main ( String [] args )
{
System.out.println ( new EncryptionUtil ( ).decryptString ( "L/UlkJjYhLnYiov7XeDjb9W7+k8Gduvz" ) );
System.out.println ( new EncryptionUtil ( ).passwordsMatch ( "test password", "P+LJM7VJHu/hudSQOrmvcvV/DrzCv+pj" ) );
}
}
从 LIVE 获得的字符串(上面的字符串)的结果如下:
测试字符串 错误
这次字符串的加密起作用了(我过去没有加密过,说实话我有点惊讶和困惑),但是密码失败了。
<小时/>新编辑 - 某些字符串在加密后以 == 结尾。末尾没有“==”的字符串可以跨系统解密。那些不起作用的。也许这可以是一个线索?
最佳答案
我不确定问题是什么 - 不管怎样,我设法通过随机尝试解决了它,但不知何故有效......
如果有人发现自己处于同样的情况,请尝试(在我的例子中是幸运的随机尝试)更改用于文本加密器的密码。
我无法解释它为何有效。
关于Java平台独立加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18896045/