如何在JAVA或Android中加密和解密UTF

如何在JAVA或Android中加密和解密UTF

本文介绍了如何在JAVA或Android中加密和解密UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个算法在Android中加密和解密数据。但是当使用utf-8 charater ..这个错误显示:[encrypt]数据不是块大小对齐。



我使用这个加密和解密算法:



我的代码:

  HttpClient client = new DefaultHttpClient(); 

HttpPost post = new HttpPost(ServerIP.frooshgah_URL);
列表< 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自动生成的catch块
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(异常e1){
// TODO自动生成的catch块
e1.printStackTrace();
}

如何解决这个问题?



谢谢

解决方案

首先,我看到 MCrypt class你的使用提供源代码。下载源代码并将其添加到您的项目中,并将 padString(string)方法修改为:

  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; (int i = 0; i< padLength; i ++)


{
source + = paddingChar;
}

返回源;
}

这将允许代码执行时使用 UTF -8 作为字符集。如果要改进库以支持多个字符集,请考虑将 charset 参数添加到 encrypt / decrypt 类的方法。


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

解决方案

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.

这篇关于如何在JAVA或Android中加密和解密UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:17