我必须在我的程序中实施基本加密。我可以使用Base64,它被客户端拒绝。所以我正在使用以下方法。我面临的问题是加密中有特殊字符会导致异常。我可以更改此代码以某种方式加密成纯文本没有特殊字符。如何仅将文本加密为纯文本字符串
protected static byte[] encrypt(String text)
{
try
{
String key = "6589745268754125";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
return encrypted;
}
catch(Exception ex)
{
WriteLog("Encryption Failed");
WriteLog(ex.getMessage());
return null;
}
}
protected static String decrypt(byte[] pass)
{
try
{
String key = "6589745268754125";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(pass));
return decrypted;
}
catch(Exception ex)
{
WriteLog("Encryption Failed");
WriteLog(ex.getMessage());
return null;
}
}
异常消息称“由于最后一个块未正确填充” javax.crypto.BadPaddingException:鉴于最终块未正确填充
什么是问题人物?什么是例外?异常在哪里? – nicomp
Base64是一种编码,而不是加密。如果您想要加密字节的纯文本表示,base64是对其进行编码的好方法。 – shmosel
我知道,我试图通过编码来获得,但现在我要做的加密 – Moon