.


:




:

































 

 

 

 


Crypto chat with VK API Technologies

4 -351

VK API

Crypto chat with VK API Technologies

. , Java Android, VK API

: , , , , RSA, XOR, DES, AES

Description. The article describes a software system developed in Java for the Android mobile platform, which encrypts the messages using asymmetrical or symmetrical encryption with VK API technologies

Keywords: Information security, crypto chat, cryptography, encryption, RSA, XOR, DES, AES

VK , Android.

API (application programming interface) - , . API , , .

1.

API , vk.com http- . , , , API- . .

, 210700286 :

 

https://api.vk.com/method/users.get?user_id=210700286&v=5.52

 

.

https:// .

api.vk.com/methods API-.

users.get API . , , . , users.get , video.add , likes.delete .

 

. , groups, photos, . .

?user_id=210700286&v=5.52 . ( ) GET- http-. , id=210700286 API 5.52 ( ). .

 

JSON- ( , - ). JSON : .

 

:

{"response":[{"id":210700286,"first_name":"Lindsey","last_name":"Stirling"}]}

, API , id , first_name . , . , users.get , user .

 

. , , , , , users.get, users.search, groups.getMembers .

 

, , . , : - , , - .

 

API . -, API - . .

API , access_token. , . . , , , , .

Implicit flow.

:

  1. public class auth extends AppCompatActivity {
  2. WebView wb = (WebView) findViewById(R.id.web);
  3. WebSettings webSettings = wb.getSettings();
  4. webSettings.setJavaScriptEnabled(true);
  5. SimpleWebViewClient webViewClient = new SimpleWebViewClient();
  6. wb.setWebViewClient(webViewClient);
  7. wb.loadUrl("https://oauth.vk.com/authorize?client_id=5213947&redirect_uri=oauth.vk.com/blank.html&display=mobile&scope=4098&&response_type=token&v=5.58");
  8. }

, , ..

GetToken.

  1. public void GetToken (View view) {
  2. WebView wb = (WebView) findViewById(R.id.web);
  3. token = wb.getUrl().toString().split("=")[1].toString().split("&")[0];
  4. Snackbar.make(view, "Получаю токен...", Snackbar.LENGTH_LONG).setAction("Action", null).show();

5. wb.setVisibility(View.INVISIBLE); }

. , , . . token.

 

expires_in . 86400 . , . scope offline.

user_id id , .

 

AssyncTask GetFriends.

  1. class GetFriend extends AsyncTask<String, Void, String> {
  2. @Override
  3. protected void onPreExecute() {
  4. super.onPreExecute();
  5. }
  6. @Override
  7. protected String doInBackground(String... params) {
  8. StringBuilder sb = new StringBuilder();
  9. try {
  10. HttpURLConnection connection = (HttpURLConnection) newURL(params[0]).openConnection();
  11. connection.connect();
  12. InputStream inputStream =connection.getInputStream();
  13. BufferedReader reader = newBufferedReader(new InputStreamReader(inputStream));
  14. String line = null;
  15. while ((line = reader.readLine())!=null) {
  16. sb.append(line);
  17. }
  18. TimeUnit.SECONDS.sleep(2);
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. } catch (MalformedURLException e) {
  22. e.printStackTrace();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. return sb.toString();
  27. }
  28. @Override
  29. protected void onPostExecute(String result) {
  30. String friendsCount = null;
  31. String posts = "";
  32. super.onPostExecute(result);
  33. try {
  34. int i;
  35. JSONObject obj = new JSONObject(result);
  36. friendsCount = obj.getJSONObject("response").getString("count");
  37. JSONArray arr = obj.getJSONObject("response").getJSONArray("items");
  38. friends = new String[Integer.parseInt(friendsCount)];
  39. for (i = 0; i < arr.length(); i++) {
  40. friends[i] =
  41. "id" +arr.getJSONObject(i).getString("id")
  42. + ":" + ' \n ' +arr.getJSONObject(i).getString("first_name")
  43. + " " +arr.getJSONObject(i).getString("last_name")
  44. + " <<" +arr.getJSONObject(i).getString("online") + ">>";
  45. friends[i] =friends[i].replace("<<1>>", " online");
  46. friends[i] =friends[i].replace("<<0>>", " offline");
  47. }
  48. showDialog(55);
  49. } catch (JSONException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. }

friends.

AssyncTask getHistory.

1.

  1. class GetHistory extends AsyncTask<String, Void, String> {
  2. @Override
  3. protected void onPreExecute() {
  4. super.onPreExecute();
  5. }
  6. @Override
  7. protected String doInBackground(String... params) {
  8. StringBuilder sb = new StringBuilder();
  9. try {
  10. HttpURLConnection connection = (HttpURLConnection) newURL(params[0]).openConnection();
  11. connection.connect();
  12. InputStream inputStream =connection.getInputStream();
  13. BufferedReader reader = newBufferedReader(newInputStreamReader(inputStream));
  14. String line = null;
  15. while ((line =reader.readLine())!= null) {
  16. sb.append(line);
  17. }
  18. TimeUnit.SECONDS.sleep(2);
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. } catch (MalformedURLException e) {
  22. e.printStackTrace();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. return sb.toString();
  27. }
  28. @Override
  29. protected void onPostExecute(String result) {
  30. String messegesCount = null;
  31. String posts = "";
  32. super.onPostExecute(result);
  33. ////&#1087;&#1072;&#1088;&#1089;&#1080;&#1085;&#1075;
  34. try {
  35. JSONObject obj = new JSONObject(result);
  36. messegesCount = obj.getJSONObject("response").getString("count");
  37. JSONArray arr = obj.getJSONObject("response").getJSONArray("items");
  38. for (int i = 0; i <arr.length(); i++) {
  39. String msg =arr.getJSONObject(i).getString("body");
  40. if(msg.contains(":CRMSGWLDMR:")) {
  41. msg =decrypt(msg.replace(":CRMSGWLDMR:", ""), key).replace("+"," ");
  42. }
  43. if(arr.getJSONObject(i).getString("from_id").equals(user_id))
  44. posts +=" :" + msg + " \n ";
  45. else
  46. posts += "" + msg + " \n ";
  47. }
  48. } catch (JSONException e) {
  49. e.printStackTrace();
  50. }
  51. if (result.contains("count")) {
  52. final AlertDialog.Builder builder = new AlertDialog.Builder(auth.this);
  53. if (!user_id_name.equals(null))
  54. builder.setTitle("" + user_id_name);

50 :CRMSGWLDMR: (52- )

AssyncTask sendMessage.

  1. class SendMessage extends AsyncTask<String, Void, String> {
  2. @Override
  3. protected void onPreExecute() {
  4. super.onPreExecute();
  5. }
  6. @Override
  7. protected String doInBackground(String... params) {
  8. StringBuilder sb = new StringBuilder();
  9. try {
  10. HttpURLConnection connection = (HttpURLConnection) newURL(params[0]).openConnection();
  11. connection.connect();
  12. InputStream inputStream =connection.getInputStream();
  13. BufferedReader reader = newBufferedReader(newInputStreamReader(inputStream));
  14. String line = null;
  15. while ((line =reader.readLine())!= null) {
  16. sb.append(line);
  17. }
  18. TimeUnit.SECONDS.sleep(2);
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. } catch (MalformedURLException e){
  22. e.printStackTrace();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. return sb.toString();
  27. }
  28. @Override
  29. protected void onPostExecute(String result) {
  30. GetHistory("https://api.vk.com/method/messages.getHistory?offset=0&count=5&user_id="+ user_id + "&access_token=" + token + "&v=5.59");
  31. }
  32. }

XOR. .

  1. public static String xor_encrypt(String message, String key){
  2. try {
  3. if (message==null || key==null) return null;
  4. char[] keys=key.toCharArray();
  5. char[] mesg=message.toCharArray();
  6. BASE64Encoder encoder = new BASE64Encoder();
  7. int ml=mesg.length;
  8. int kl=keys.length;
  9. char[] newmsg=new char[ml];
  10. for (int i=0; i<ml; i++){
  11. newmsg[i]=(char)(mesg[i]^keys[i%kl]);
  12. }
  13. mesg=null;
  14. keys=null;
  15. String temp = newString(newmsg);
  16. return new String(newBASE64Encoder().encodeBuffer(temp.getBytes()));
  17. }
  18. catch (Exception e) {
  19. return null;
  20. }
  21. }
  22. public static String xor_decrypt(String message, String key){
  23. try {
  24. if (message==null || key==null) return null;
  25. BASE64Decoder decoder = new BASE64Decoder();
  26. char[] keys=key.toCharArray();
  27. message = new String(decoder.decodeBuffer(message));
  28. char[] mesg=message.toCharArray();
  29. int ml=mesg.length;
  30. int kl=keys.length;
  31. char[] newmsg=new char[ml];
  32. for (int i=0; i<ml; i++){
  33. newmsg[i]=(char)(mesg[i]^keys[i%kl]);
  34. }
  35. mesg=null; keys=null;
  36. return new String(newmsg);
  37. }
  38. catch (Exception e) {
  39. return null;
  40. }
  41. }

 

 



<== | ==>
- | .
:


: 2016-12-05; !; : 715 |


:

:

.
==> ...

1263 - | 1244 -


© 2015-2024 lektsii.org - -

: 0.022 .