gpt4 book ai didi

java - RSA 加密 - 字节数组和字符串之间的转换

转载 作者:行者123 更新时间:2023-11-29 08:37:02 25 4
gpt4 key购买 nike

<分区>

我正在尝试实现能够执行以下操作的 RSA 加密:

  • 接受一个字符串值作为使用公钥加密的输入
  • 将加密的密码作为字符串返回
  • 接受加密的密码作为使用私钥解密的输入
  • 返回原始值,解密

如果我直接解密 byte,我就能使加密/解密工作加密返回的数组,但如果我解析 byte 似乎无法让它工作数组到 String然后回到byte又是 .

以下代码确实有效:

cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherBytes = cipher.doFinal(input);
System.out.println("cipher: " + new String(cipherBytes));
returnValue += new String(cipherBytes);

cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] plainText = cipher.doFinal(cipherBytes);
System.out.println("plain : " + new String(plainText));

以下代码有效:

byte[] cipherBytes = cipher.doFinal(input);
System.out.println("cipher: " + new String(cipherBytes));
returnValue += new String(cipherBytes);

String cipherText = new String(cipherBytes);
byte[] reCipherBytes = cipherText.getBytes();

cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] plainText = cipher.doFinal(reCipherBytes);
System.out.println("plain : " + new String(plainText));

任何人都可以建议我需要做什么才能使第二个版本成功运行吗?

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