Commit 73bda7bf authored by Corentin Mors's avatar Corentin Mors
Browse files

Handle first run errors and add random iv web side

parent 27bb3169
......@@ -37,6 +37,7 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import android.util.Base64;
import android.util.Log;
public class CryptLib {
......@@ -92,7 +93,6 @@ public class CryptLib {
System.arraycopy(encryptionKey.getBytes("UTF-8"), 0, _key, 0, len);
System.arraycopy(initVector.getBytes("UTF-8"), 0, _iv, 0, ivlength);
SecretKeySpec keySpec = new SecretKeySpec(_key, "AES"); // Create a new SecretKeySpec for the specified key data and algorithm name.
IvParameterSpec ivSpec = new IvParameterSpec(_iv); // Create a new IvParameterSpec instance with the bytes from the specified buffer iv used as initialization vector.
......
......@@ -2,13 +2,11 @@ package fr.pixelswap.onmap.onmap;
public class CryptoFunc {
public static String sencrypt(String password, String data) throws Exception {
CryptLib cryptLib = new CryptLib();
return cryptLib.encryptPlainTextWithRandomIV(data, password);
}
public static String sdecrypt(String password, String data) throws Exception {
CryptLib cryptLib = new CryptLib();
return cryptLib.decryptCipherTextWithRandomIV(data, password);
}
......
......@@ -48,13 +48,14 @@ public class MainActivity extends AppCompatActivity {
private Socket mSocket;
private static final int REQUEST_ACCESS_FINE_LOCATION = 0;
private String mUsername;
private String mPassword;
private int[] mUsernameColors;
private LocationManager mLocManager;
private LocationListener mLocListener;
MapView map = null;
GeoPoint currentLocation;
......@@ -72,36 +73,10 @@ public class MainActivity extends AppCompatActivity {
}
@Override public void onCreate(Bundle savedInstanceState) {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_ACCESS_FINE_LOCATION);
}
} else {
// Permission has already been granted
}
//Permissions handled above
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final String mRoom = settings.getString("mRoom", null);
......@@ -115,18 +90,21 @@ public class MainActivity extends AppCompatActivity {
// Set up location getters
LocationManager mLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final LocationListener mLocListener = new MyLocationListener();
LocationProvider low = mLocManager.getProvider(mLocManager.getBestProvider(createCoarseCriteria(), false));
Log.e("OM_PROVIDER", low.getName());
mLocManager.requestLocationUpdates(low.getName(), 0, 0, mLocListener);
Location location = mLocManager.getLastKnownLocation(low.getName());
if( location != null ) {
currentLocation = new GeoPoint(location.getLatitude(), location.getLongitude());
mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocListener = new MyLocationListener();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(getBaseContext(), SettingsActivity.class);
startActivity(intent);
finish();
}
else{
LocationProvider low = mLocManager.getProvider(mLocManager.getBestProvider(createCoarseCriteria(), false));
mLocManager.requestLocationUpdates(low.getName(), 0, 0, mLocListener);
Location location = mLocManager.getLastKnownLocation(low.getName());
if( location != null ) {
currentLocation = new GeoPoint(location.getLatitude(), location.getLongitude());
}
}
//load/initialize the osmdroid configuration, this can be done
Context ctx = getApplicationContext();
......@@ -164,7 +142,7 @@ public class MainActivity extends AppCompatActivity {
settingsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//LocationManager.removeUpdates(mLocListener);
mLocManager.removeUpdates(mLocListener);
Intent intent = new Intent(getBaseContext(), SettingsActivity.class);
startActivity(intent);
finish();
......@@ -172,6 +150,18 @@ public class MainActivity extends AppCompatActivity {
});
}
@Override
public void onDestroy() {
super.onDestroy();
mSocket.disconnect();
mSocket.off("geoloc", onGeoloc);
mSocket.off("user left", onUserLeft);
mLocManager.removeUpdates(mLocListener);
}
private void displayMyCurrentLocationOverlay() {
if( currentLocation != null) {
Marker locationMarker = new Marker(map);
......
package fr.pixelswap.onmap.onmap;
import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
......@@ -11,13 +15,45 @@ import android.widget.EditText;
public class SettingsActivity extends AppCompatActivity {
private static final int REQUEST_ACCESS_FINE_LOCATION = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_ACCESS_FINE_LOCATION);
}
} else {
// Permission has already been granted
}
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final EditText roomInput = findViewById(R.id.editRoom);
final EditText usernameInput = findViewById(R.id.editUsername);
final EditText passwordInput = findViewById(R.id.editPassword);
roomInput.setText(settings.getString("mRoom", "myroom"));
usernameInput.setText(settings.getString("mUsername", "John"));
passwordInput.setText(settings.getString("mPassword", "azerty"));
Button settingsButton = findViewById(R.id.buttonSettings);
settingsButton.setOnClickListener(new View.OnClickListener() {
......@@ -25,9 +61,6 @@ public class SettingsActivity extends AppCompatActivity {
public void onClick(View view) {
SharedPreferences.Editor edit = settings.edit();
EditText roomInput = findViewById(R.id.editRoom);
EditText usernameInput = findViewById(R.id.editUsername);
EditText passwordInput = findViewById(R.id.editPassword);
edit.putString("mRoom", roomInput.getText().toString());
edit.putString("mUsername", usernameInput.getText().toString());
......
......@@ -2378,8 +2378,7 @@ var cryptoLib = require('@skavinvarnan/cryptlib');
 
window.encrypt = function (data, password){
try {
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.encryptPlainTextWithRandomIV(data, shaKey);
return cryptoLib.encryptPlainTextWithRandomIV(data, password);
} catch (exception) {
throw new Error(exception.message);
}
......@@ -2387,8 +2386,7 @@ window.encrypt = function (data, password){
 
window.decrypt = function (data, password){
try {
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.decryptCipherTextWithRandomIV(data, shaKey);
return cryptoLib.decryptCipherTextWithRandomIV(data, password);
} catch (exception) {
throw new Error(exception.message);
}
......@@ -2396,8 +2394,7 @@ window.decrypt = function (data, password){
 
window.oencrypt = function (data, password){
try {
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.encryptPlainTextWithRandomIV(JSON.stringify(data), shaKey);
return cryptoLib.encryptPlainTextWithRandomIV(JSON.stringify(data), password);
} catch (exception) {
throw new Error(exception.message);
}
......@@ -2405,8 +2402,7 @@ window.oencrypt = function (data, password){
 
window.odecrypt = function (data, password){
try {
shaKey = cryptoLib.getHashSha256(password, 32);
var ostring = cryptoLib.decryptCipherTextWithRandomIV(data, shaKey);
var ostring = cryptoLib.decryptCipherTextWithRandomIV(data, password);
return JSON.parse(ostring);
} catch (exception) {
throw new Error(exception.message);
......
......@@ -4,8 +4,7 @@ var cryptoLib = require('@skavinvarnan/cryptlib');
window.encrypt = function (data, password){
try {
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.encryptPlainTextWithRandomIV(data, shaKey);
return cryptoLib.encryptPlainTextWithRandomIV(data, password);
} catch (exception) {
throw new Error(exception.message);
}
......@@ -13,8 +12,7 @@ window.encrypt = function (data, password){
window.decrypt = function (data, password){
try {
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.decryptCipherTextWithRandomIV(data, shaKey);
return cryptoLib.decryptCipherTextWithRandomIV(data, password);
} catch (exception) {
throw new Error(exception.message);
}
......@@ -22,8 +20,7 @@ window.decrypt = function (data, password){
window.oencrypt = function (data, password){
try {
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.encryptPlainTextWithRandomIV(JSON.stringify(data), shaKey);
return cryptoLib.encryptPlainTextWithRandomIV(JSON.stringify(data), password);
} catch (exception) {
throw new Error(exception.message);
}
......@@ -31,8 +28,7 @@ window.oencrypt = function (data, password){
window.odecrypt = function (data, password){
try {
shaKey = cryptoLib.getHashSha256(password, 32);
var ostring = cryptoLib.decryptCipherTextWithRandomIV(data, shaKey);
var ostring = cryptoLib.decryptCipherTextWithRandomIV(data, password);
return JSON.parse(ostring);
} catch (exception) {
throw new Error(exception.message);
......
......@@ -32,7 +32,6 @@
{% block scripts %}
<script src="/socket.io/socket.io.js"></script>
<script src="assets/openlayer.min.js"></script>
<script src="bower_components/crypto-js/crypto-js.js"></script>
<script>var username = "{{ username | escape }}"; var room = "{{ room | escape }}"; var password = "{{ password | escape }}";</script>
<script src="assets/crypto.bundle.js"></script>
<script src="assets/main.js"></script>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment