问题
I use This Algorithm for Encrypt and Decrypt data in android. But when use utf-8 charater ..this error is displayed : [encrypt] data not block size aligned.
I use this Algorithm for Encrypt and Decrypt : https://snipt.net/raw/ee573b6957b7416f28aa560ead71c3a2/?nice
my code:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(ServerIP.frooshgah_URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
JSONObject json = new JSONObject();
try {
json.put("UserId", "0s");
json.put("N_frooshgah", N_frooshgah);
json.put("N_masol", N_masol);
json.put("N_makan", N_makan);
json.put("address", address);
json.put("tel", tel);
json.put("time_baz", time_baz);
json.put("time_baste", time_baste);
json.put("tavzihat", tavzihat);
json.put("tag", tag);
json.put("categori", "پوشاک");
json.put("city", city);
json.put("lat", lat);
json.put("long", Long);
} catch (JSONException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
MCrypt mcrypt = new MCrypt();
String encrypted = "";
try {
encrypted = MCrypt.bytesToHex(mcrypt.encrypt(json.toString()));
//encrypted = encryption.hexToString(json.toString(), 2);
//key = UUID.randomUUID().toString().replaceAll("-", "");
//encrypted=Crypto.encrypt(json.toString(),key);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
How to Resolve this Problem?
thanks
回答1:
First of all, I see the MCrypt
class your using provides source code. Download the source code and add it to your project and modify the padString(string)
method to this:
private static String padString(String source){
char paddingChar = ' ';
int size = 16;
int x = source.getBytes(Charset.forName("UTF-8")).length % size;
int padLength = size - x;
for (int i = 0; i < padLength; i++)
{
source += paddingChar;
}
return source;
}
This will allow the code to execute while using UTF-8
as a charset
. If you want to "improve" the library to support mutliple charsets, consider adding a charset
parameter into the encrypt
/decrypt
methods of the class.
来源:https://stackoverflow.com/questions/18279818/how-to-encrypt-and-decrypt-utf-8-in-java-or-android