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.
:
- public class auth extends AppCompatActivity {
- WebView wb = (WebView) findViewById(R.id.web);
- WebSettings webSettings = wb.getSettings();
- webSettings.setJavaScriptEnabled(true);
- SimpleWebViewClient webViewClient = new SimpleWebViewClient();
- wb.setWebViewClient(webViewClient);
- 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");
- }
, , ..
GetToken.
- public void GetToken (View view) {
- WebView wb = (WebView) findViewById(R.id.web);
- token = wb.getUrl().toString().split("=")[1].toString().split("&")[0];
- 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.
- class GetFriend extends AsyncTask<String, Void, String> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected String doInBackground(String... params) {
- StringBuilder sb = new StringBuilder();
- try {
- HttpURLConnection connection = (HttpURLConnection) newURL(params[0]).openConnection();
- connection.connect();
- InputStream inputStream =connection.getInputStream();
- BufferedReader reader = newBufferedReader(new InputStreamReader(inputStream));
- String line = null;
- while ((line = reader.readLine())!=null) {
- sb.append(line);
- }
- TimeUnit.SECONDS.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- @Override
- protected void onPostExecute(String result) {
- String friendsCount = null;
- String posts = "";
- super.onPostExecute(result);
- try {
- int i;
- JSONObject obj = new JSONObject(result);
- friendsCount = obj.getJSONObject("response").getString("count");
- JSONArray arr = obj.getJSONObject("response").getJSONArray("items");
- friends = new String[Integer.parseInt(friendsCount)];
- for (i = 0; i < arr.length(); i++) {
- friends[i] =
- "id" +arr.getJSONObject(i).getString("id")
- + ":" + ' \n ' +arr.getJSONObject(i).getString("first_name")
- + " " +arr.getJSONObject(i).getString("last_name")
- + " <<" +arr.getJSONObject(i).getString("online") + ">>";
- friends[i] =friends[i].replace("<<1>>", " online");
- friends[i] =friends[i].replace("<<0>>", " offline");
- }
- showDialog(55);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
friends.
AssyncTask getHistory.
1.
- class GetHistory extends AsyncTask<String, Void, String> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected String doInBackground(String... params) {
- StringBuilder sb = new StringBuilder();
- try {
- HttpURLConnection connection = (HttpURLConnection) newURL(params[0]).openConnection();
- connection.connect();
- InputStream inputStream =connection.getInputStream();
- BufferedReader reader = newBufferedReader(newInputStreamReader(inputStream));
- String line = null;
- while ((line =reader.readLine())!= null) {
- sb.append(line);
- }
- TimeUnit.SECONDS.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- @Override
- protected void onPostExecute(String result) {
- String messegesCount = null;
- String posts = "";
- super.onPostExecute(result);
- ////парсинг
- try {
- JSONObject obj = new JSONObject(result);
- messegesCount = obj.getJSONObject("response").getString("count");
- JSONArray arr = obj.getJSONObject("response").getJSONArray("items");
- for (int i = 0; i <arr.length(); i++) {
- String msg =arr.getJSONObject(i).getString("body");
- if(msg.contains(":CRMSGWLDMR:")) {
- msg =decrypt(msg.replace(":CRMSGWLDMR:", ""), key).replace("+"," ");
- }
- if(arr.getJSONObject(i).getString("from_id").equals(user_id))
- posts +=" :" + msg + " \n ";
- else
- posts += "" + msg + " \n ";
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- if (result.contains("count")) {
- final AlertDialog.Builder builder = new AlertDialog.Builder(auth.this);
- if (!user_id_name.equals(null))
- builder.setTitle("" + user_id_name);
50 :CRMSGWLDMR: (52- )
|
|
AssyncTask sendMessage.
- class SendMessage extends AsyncTask<String, Void, String> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected String doInBackground(String... params) {
- StringBuilder sb = new StringBuilder();
- try {
- HttpURLConnection connection = (HttpURLConnection) newURL(params[0]).openConnection();
- connection.connect();
- InputStream inputStream =connection.getInputStream();
- BufferedReader reader = newBufferedReader(newInputStreamReader(inputStream));
- String line = null;
- while ((line =reader.readLine())!= null) {
- sb.append(line);
- }
- TimeUnit.SECONDS.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (MalformedURLException e){
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- @Override
- protected void onPostExecute(String result) {
- GetHistory("https://api.vk.com/method/messages.getHistory?offset=0&count=5&user_id="+ user_id + "&access_token=" + token + "&v=5.59");
- }
- }
XOR. .
- public static String xor_encrypt(String message, String key){
- try {
- if (message==null || key==null) return null;
- char[] keys=key.toCharArray();
- char[] mesg=message.toCharArray();
- BASE64Encoder encoder = new BASE64Encoder();
- int ml=mesg.length;
- int kl=keys.length;
- char[] newmsg=new char[ml];
- for (int i=0; i<ml; i++){
- newmsg[i]=(char)(mesg[i]^keys[i%kl]);
- }
- mesg=null;
- keys=null;
- String temp = newString(newmsg);
- return new String(newBASE64Encoder().encodeBuffer(temp.getBytes()));
- }
- catch (Exception e) {
- return null;
- }
- }
- public static String xor_decrypt(String message, String key){
- try {
- if (message==null || key==null) return null;
- BASE64Decoder decoder = new BASE64Decoder();
- char[] keys=key.toCharArray();
- message = new String(decoder.decodeBuffer(message));
- char[] mesg=message.toCharArray();
- int ml=mesg.length;
- int kl=keys.length;
- char[] newmsg=new char[ml];
- for (int i=0; i<ml; i++){
- newmsg[i]=(char)(mesg[i]^keys[i%kl]);
- }
- mesg=null; keys=null;
- return new String(newmsg);
- }
- catch (Exception e) {
- return null;
- }
- }