【问题标题】:Spring Security - BytesEncryptor - not getting the proper encrypted passwordSpring Security - BytesEncryptor - 没有得到正确的加密密码
【发布时间】:2018-02-19 23:33:43
【问题描述】:
BytesEncryptor enc =  Encryptors.standard("encryptionPassword",saltKey);

byte[] encryptedPwd = enc.encrypt(pwd.getBytes());

byte[] decryptedPwd = enc.decrypt(encryptedPwd );

我使用 BytesEncryptors (Spring security) 来加密/解密密码。我在字节 [] 中有我的加密/解密密码。我想得到加密/解密的密码。

所以我要做的是,

System.out.println(new String(encryptedPwd,"UTF-8"));

System.out.println(new String(decryptedPwd,"UTF-8"));

第二行运行良好。第一行没有给我编码密码的字符串表示形式。它显示了一些无用的东西。

1) 为什么我的第一行不像第二行那样工作?

2) 如果我想对我的 spring bean 配置(XML 文件)使用相同的加密算法,我会怎么做?像 DataSource Info/其他一些登录凭据应该在 XML 文件中加密。(* 我在这里不使用 Hibernate)

【问题讨论】:

  • 尝试使用 Ctrl+'K' 或 ' ` ' 引号格式化您的代码
  • 您希望加密后的密码是什么样的?
  • 我得到这样的东西'–Ú×°ä÷q“ýd。而且我不确定它是否是存储在属性文件中的加密密码

标签: java spring encryption spring-security aes


【解决方案1】:

对于“随机”字节数组的文本安全表示,请使用 base64 编码:

System.out.println(Base64Utils.encodeToString(encryptedPwd));

您可以使用Base64Utils 进行编码和解码。

【讨论】:

  • @user3603360 很高兴听到。如果您认为这回答了您的问题,请不要忘记接受。
【解决方案2】:

您可以简单地将字节数组打印为十六进制字符串。

例如:

  • 字节 0 => 00
  • 字节 -1 => ff
  • 字节 127 => 7f
  • 字节-128 => 80

如果您使用 Spring Security,只需使用返回 char[] 的方法 org.springframework.security.crypto.codec.Hex#encode

HexEncodingTextEncryptor类中你也可以找到这个快捷方式:

    public String encrypt(String text) {
        return new String(Hex.encode(this.encryptor.encrypt(Utf8.encode(text))));
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-07
    • 2021-05-13
    • 2014-05-24
    • 2018-12-10
    • 2015-10-27
    • 2023-03-30
    • 2014-02-14
    • 2022-12-01
    相关资源
    最近更新 更多