我尝试使用以下代码加密纯文本。代码似乎加密了文本但它没有解密到明文。我做错了什么?
代码:
Entity entity = new Entity("password");
byte[] ciphertext = crypto.encrypt(("data to encrypt").getBytes(),entity);
plaintext = crypto.decrypt(ciphertext,entity)
输出:
Ecrypted text:[B@417a110
Decrypted text:[B@417df20
答案 0 :(得分:2)
以下代码可以加密/解密字符串
KeyChain keyChain = new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256);
crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
public static String encrypt(String key, String value) throws KeyChainException, CryptoInitializationException, IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
OutputStream cryptoStream = crypto.getCipherOutputStream(bout, Entity.create(key));
cryptoStream.write(value.getBytes("UTF-8"));
cryptoStream.close();
String result = Base64.encodeToString(bout.toByteArray(), Base64.DEFAULT);
bout.close();
return result;
}
public static String decrypt(String key, String value) throws KeyChainException, CryptoInitializationException, IOException {
ByteArrayInputStream bin = new ByteArrayInputStream(Base64.decode(value, Base64.DEFAULT));
InputStream cryptoStream = crypto.getCipherInputStream(bin, Entity.create(key));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int read = 0;
byte[] buffer = new byte[1024];
while ((read = cryptoStream.read(buffer)) != -1) {
bout.write(buffer, 0, read);
}
cryptoStream.close();
String result = new String(bout.toByteArray(), "UTF-8");
bin.close();
bout.close();
return result;
}
答案 1 :(得分:0)
我找到了答案。
原因是我们打印的是字节数组而不是字符串。
数组将包含一组字节,这就是我们在logcat中打印出来时所看到的。
要查看实际的String,我们只需要将byte []放入一个新的String(byte [])中 - 这取自我的修改后的官方facebook示例:
Imports OpenHardwareMonitor
Imports OpenHardwareMonitor.Hardware
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cp As New Computer()
cp.Open()
cp.HDDEnabled = True
cp.FanControllerEnabled = True
cp.RAMEnabled = True
cp.GPUEnabled = True
cp.MainboardEnabled = True
cp.CPUEnabled = True
Dim Info As String = ""
For i As Integer = 0 To cp.Hardware.Count() - 1
If cp.Hardware(i).HardwareType = HardwareType.Mainboard Then
Info += " Motherboard: " & Trim(cp.Hardware(i).Name) & vbCrLf
End If
If cp.Hardware(i).HardwareType = HardwareType.CPU Then
Info += " Processor: " & Trim(cp.Hardware(i).Name) & vbCrLf
End If
If cp.Hardware(i).HardwareType = HardwareType.GpuNvidia Then
Info += " Video Card: " & Trim(cp.Hardware(i).Name) & vbCrLf
End If
If cp.Hardware(i).HardwareType = HardwareType.RAM Then
Info += " RAM: " & Trim(cp.Hardware(i).Name) & vbCrLf
End If
If cp.Hardware(i).HardwareType = HardwareType.HDD Then
Info += " HDD: " & Trim(cp.Hardware(i).Name) & vbCrLf
End If
If cp.Hardware(i).HardwareType = HardwareType.SuperIO Then
Info += " SuperIO: " & Trim(cp.Hardware(i).Name) & vbCrLf
End If
Next
TextBox1.Text = Info
End Sub
End Class
结果:
08-02 19:31:11.237 29364-29364 /? E /加密加密: 0 & ?B 6 H ` “ 1 xx 08-0219:31:11.237 29364-29364 /? E / Crypto Decrypted:密码
答案 2 :(得分:0)
Base64.encodeToString(cipherText, Base64.DEFAULT); then store it.
答案 3 :(得分:0)
可能为时已晚,但是我遇到了同样的问题,并在解密后设法获得了纯文本。
您真正需要做的是像下面的代码一样使用 nameTextView=findViewById(R.id.nameDashboardTextView)
ageTextView=findViewById(R.id.ageDashboardTextView)
emailIDTextView=findViewById(R.id.emailIDTextView)
genderTextView=findViewById(R.id.genderTextView)
weightTextView=findViewById(R.id.weightTextView)
heightTextView=findViewById(R.id.heightTextView)
progressBar3=findViewById(R.id.progressBar3)
:
ByteArrayOutputStream
希望这会有所帮助。