2013-04-25 109 views
0

如何加密/解密public/private。加密/解密public/private android

我假設這意味着密鑰是動態的,對於一個字符串來說永遠不會相同。

我想知道是否有任何圖書館這樣做或一步一步的教程,讓初學者理解和實施的應用程序。

我想在HTTP例如,以確保密碼:

http://www.example.com/username="ENCRYPTED1"+Password="ENCRYPTED2" 

加密1和2是動態的,永遠不會相同。

通過上面的方法和密鑰應該總是改變,因此即使您在瀏覽器中鍵入加密密鑰它不應該允許,因爲密鑰會改變。

我希望這是正確的道路。

我看着Spongy城堡,我不明白如何實現它。

請幫我指導一下。

在此先感謝。

代碼:

public class CustomizedListView extends Activity { 
    // All static variables 
    static final String URL = "http://example.com/getmsgs/userno=123"; 
    // XML node keys 
    static final String KEY_SONG = "song"; // parent node 
    static final String KEY_ID = "id"; 
    static final String KEY_TITLE = "title"; 
    static final String KEY_ARTIST = "artist"; 
    static final String KEY_DURATION = "duration"; 
    static final String KEY_THUMB_URL = "thumb_url"; 

    ListView list; 
    LazyAdapter adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 

     JSONObject json = JSONfunctions.getJSONfromURL(URL); 


     try { 
      JSONObject arr2 = json.getJSONObject("feed"); 
      JSONArray arr = arr2.getJSONArray("entry"); 

      for (int i = 0; i < arr.length(); i++) { 
       JSONObject e1 = arr.getJSONObject(i); 

       JSONArray arr3 = e1.getJSONArray("im:image"); 

       JSONObject arr8 = e1.getJSONObject("im:name"); 

       JSONObject arr10 = e1.getJSONObject("im:artist"); 

        JSONObject e12 = arr3.getJSONObject(0); 

      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      map.put(KEY_THUMB_URL, e12.getString("label")); 

      map.put(KEY_ARTIST, arr8.getString("label")); 
      map.put(KEY_TITLE, arr10.getString("label")); 
      // adding HashList to ArrayList 
      songsList.add(map); 
      } 

     } catch (JSONException e) { 
      // Log.e("log_tag", "Error parsing data "+e.toString()); 
      Toast.makeText(getBaseContext(), 
        "Network communication error!", 5).show(); 
     } 


     list=(ListView)findViewById(R.id.list); 

     // Getting adapter by passing xml data ArrayList 
     adapter=new LazyAdapter(this, songsList);   
     list.setAdapter(adapter); 

     // Click event for single list row 
     list.setOnItemClickListener(new OnItemClickListener() { 

      @SuppressWarnings("unchecked") 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 


       HashMap<String, String> o = (HashMap<String, String>) list.getItemAtPosition(position); 
       Toast.makeText(CustomizedListView.this, "ID '" + o.get("KEY_TITLE") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

      } 
     });  
    } 
} 

PHP代碼:

<?php 

$strno=$_GET['strno']; 

if (isset($strno)) 
{ 
     $connect=mysql_connect("localhost","test","test") or die ('Connection error!!!'); 
     mysql_select_db("test") or die ('Database error!!!'); 

    $query=mysql_query("select sno FROM users where strno='$strno';"); 
    while($row = mysql_fetch_assoc($query)) 

    { 
     $jsonoutput='{"json":{ 
      "msg_sub":"'.$row['msg_sub'].'", 
      }}'; 
    } 

} 

echo trim($jsonoutput); 
mysql_close($connect) or die ('Unable to close connection-error!!!'); 
} 

?> 

JSONfunctions.java

public class JSONfunctions { 

    public static JSONObject getJSONfromURL(String url){ 
     InputStream is = null; 
     String result = ""; 
     JSONObject jArray = null; 

     //http post 
     try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(url); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 

     }catch(Exception e){ 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 

     //convert response to string 
     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
      } 
      is.close(); 
      result=sb.toString(); 
    }catch(Exception e){ 
      Log.w("log_tag", "Error converting result "+e.toString()); 
    } 

    try{ 

     jArray = new JSONObject(result);    
    }catch(JSONException e){ 
      Log.w("log_tag", "Error parsing data "+e.toString()); 
    } 

    return jArray; 
} 

}

回答

3

我希望這是正確的道路。

您正在偏離軌道。

而不是創建自己的協議,使用SSL/HTTPS,然後客戶端可以照常通過POST請求發送其用戶名和密碼(除了通過HTTPS完成)。

或者,你可以做「相互認證」。這意味着客戶端和服務器都使用其公鑰(使用HTTPS,只有服務器使用其證書/公鑰進行身份驗證)進行身份驗證。

+0

謝謝您的回覆。我正在使用jsonFunctions。Java解析數據和代碼看起來像** JsonObject obj = jsonFunction.getjsonfromurl(「http://www.example.com/username=」ENCRYPTED1「+密碼=」ENCRYPTED2「); **目前這是我的代碼在Android中。我想動態加密它,也沒有會話在我的PHP(我希望這是正常的)。我想知道在哪裏繼續或我需要閱讀什麼來實現所需的。目前我很難過,不知道從哪裏開始。任何正確的道路上的指導將幫助我很多。再次感謝。 – 2013-04-25 05:32:19

+0

@JamesPatrick嘿,你在這裏指出,你正在使用'JSON'.so嘗試發送你的數據在'JSON對象'''HTTP POST''中,這樣就可以對你的數據進行編碼以及'POST'將會隱藏你的參數,所以不用擔心這一點 – 2013-04-25 05:37:49

+0

我已經添加了上面的代碼,在這裏我想加密userno即:** 123 ** – 2013-04-25 05:42:46

0

不要發明新的安全協議。使用HTTPS,然後您不需要自己加密密碼。使用HTTP,除非您的操作與HTTPS基本相同,否則您將以任何方式加密和交換密鑰可能都不太有效。它只會通過默默無聞的安全(谷歌爲此)。

編輯: 並且不要發送密碼作爲GET參數,但始終作爲POST數據,即使使用HTTPS。即使GET參數無法在網絡上捕獲,如果使用https,它們可能會被瀏覽器緩存或未加密地轉到服務器日誌,更多信息請參閱此處:http://www.w3schools.com/tags/ref_httpmethods.asp

+0

所以在上面的例子中static final String URL =「https://example.com/getmsgs/userno=123」;這會工作嗎?並且如果我在瀏覽器中粘貼它,它不應該讓我通過?會發生? – 2013-04-25 06:21:21

+0

查看我的編輯... – Oliv 2013-04-29 08:26:17