gpt4 book ai didi

java - 加密字符串到 byte[]

转载 作者:行者123 更新时间:2023-11-29 04:46:41 27 4
gpt4 key购买 nike

我需要加密我正在使用的密码 this教程

我需要将加密的密码作为字符串存储在 sql 数据库中,当我尝试读取字符串并将其转换为 byte[] 时遇到问题,byte[] 与原始加密的 byte[] 不同

byte[] encrypted = encryper.encrypte("pavel");

Log.i("info","encrypted : "+encrypted);

String pass = String.valueOf(encrypted);

byte[] passBytes = pass.getBytes();

Log.i("info","passBytes : "+passBytes);

Log.i("info","decrypted : "+encryper.decrypte(passBytes));

日志

I/info: encrypted : [B@3832a394
I/info: passBytes : [B@33deee3d
I/info: decrypted :

解密方法

public String decrypte(byte[] text)
{

byte[] textDecrypted = new byte[0];

try {

cipher.init(Cipher.DECRYPT_MODE, myDesKey);
textDecrypted = cipher.doFinal(text);
Log.i("info","do final : "+textDecrypted);

}catch (InvalidKeyException e) {e.printStackTrace();
}catch (IllegalBlockSizeException e){e.printStackTrace();}
catch (BadPaddingException e){e.printStackTrace();}

return new String(textDecrypted);
}

但是如果我写 public String decrypte(byte[] text) {

    byte[] textDecrypted = new byte[0];

try {

cipher.init(Cipher.DECRYPT_MODE, myDesKey);
textDecrypted = cipher.doFinal(text);
Log.i("info","do final : "+textDecrypted);

}catch (InvalidKeyException e) {e.printStackTrace();
}catch (IllegalBlockSizeException e){e.printStackTrace();}
catch (BadPaddingException e){e.printStackTrace();}

return new String(textDecrypted);
}

但是如果我把原来的字节[]全部工作

encryper.decrypte(encrypted)

最佳答案

我会使用 Base64.Encoder(java.util,自 Java 8 起)将字节数组编码为字符串,然后将其保存到数据库,然后将其从数据库读取为字符串并使用 Base64.decoder 将其解码为字节数组

关于java - 加密字符串到 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36866807/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com