Archivi categoria: Programming

My articles

Come cifrare le comunicazioni di Whatsapp

Ultimo aggiornamento: 22-02-2016

Nelle ultime settimane, anche grazie alle chiacchiere con diverse persone, mi è balenata per la testa l’idea di fare un programma di messaggistica per cellulari con comunicazioni criptate che sfruttasse la crifratura a-simmetrica.

La cifratura a-simmetrica utilizza due chiavi:

  • Una chiave che si usa per cifrare i messaggi: la do alla persona con cui sto comunicando (chiave pubblica in gergo)
  • Una chiave per decifrare i messaggi che arrivano e che la tengo io da parte al sicuro (chiave privata)

Però  di app per comunicare sui cellulari con comunicazione cifrata già c’è ne sono: Telegram per esempio (che usa Diffie-Hellman per creare la key, da usare poi in AES per cifrare i messagi) e poi il monopolio è in mano a Whatsapp, che  trasmette i messaggi in chiaro… quindi perchè non cifrare le comunicazioni di  whatsapp? =)

Continue reading Come cifrare le comunicazioni di Whatsapp

Algoritmi Randomizzati e Paralleli per risolvere il Sudoku

Ultimo aggiornamento: 15-05-2014

Durante l’ultima sessione di corsi, ho seguito Agoritmi 2, in cui il prof per spiegarci il BackTrack c’ha fatto implementare in python un sudoku solver, basandosi sugli appunti di Norving, che usa backtrack con constraint propagation.

Base

Io ho scritto il programma con una euristica diversa dalla sua: non uso la propagazione dei vincoli (e che lo facevo uguale :P) e non risolvo solo per sottogriglie, ma vado a vedere la colonna o la riga o la sottogriglia che ha il maggior numero di numeri già inseriti e lì vado a tentare l’inserimento di un nuovo numero, ovviamente il tutto ottimizzato usando le hash table (usando la funzione set in python) in modo da non contare ogni volta il numero di elementi presenti.

Con questi meccanismi rapportato all’algoritmo di Norving “mi difendo”, non ho performance  così elevate, però riesco a risolvere i suoi hardest sudoku anche in 0.03 secondi, ma a volte anche di più (parlo di secondi e molti a volte):

Solution computed
 .  .  .  9  2  .  .  .  .      3  8  7  9  2  6  4  1  5 
 .  .  6  8  .  3  .  .  .      5  4  6  8  1  3  9  7  2 
 1  9  .  .  7  .  .  .  6      1  9  2  4  7  5  8  3  6 
 2  3  .  .  4  .  1  .  .      2  3  5  7  4  9  1  6  8 
 .  .  1  .  .  .  7  .  .      9  6  1  2  5  8  7  4  3 
 .  .  8  .  3  .  .  2  9      4  7  8  6  3  1  5  2  9 
 7  .  .  .  8  .  .  9  1      7  5  4  3  8  2  6  9  1 
 .  .  .  5  .  7  2  .  .      6  1  3  5  9  7  2  8  4 
 .  .  .  .  6  4  .  .  .      8  2  9  1  6  4  3  5  7 

Number of recursive calls: 735
In  0.0366179943085 seconds

..

Solution computed
 7  .  .  .  .  .  4  .  .      7  9  8  6  3  5  4  2  1 
 .  2  .  .  7  .  .  8  .      1  2  6  9  7  4  5  8  3 
 .  .  3  .  .  8  .  7  9      4  5  3  2  1  8  6  7  9 
 9  .  .  5  .  .  3  .  .      9  7  2  5  8  6  3  1  4 
 .  6  .  .  2  .  .  9  .      5  6  4  1  2  3  8  9  7 
 .  .  1  .  9  7  .  .  6      3  8  1  4  9  7  2  5  6 
 .  .  .  3  .  .  9  .  .      6  1  7  3  5  2  9  4  8 
 .  3  .  .  4  .  .  6  .      8  3  5  7  4  9  1  6  2 
 .  .  9  .  .  1  .  3  5      2  4  9  8  6  1  7  3  5 

Number of recursive calls: 3595
In  0.172569036484 seconds

Continue reading Algoritmi Randomizzati e Paralleli per risolvere il Sudoku

SetStyle: Set CSS Properties Without Surprises

Ultimo aggiornamento: 31-01-2014

Introduction

Using JavaScript to make HTML element, often is used elem.setAttribute("style", "property1:val; property2:val;") to set CSS properties in easy way; but there’s a problem if you had just set a CSS property with elem.style.prop="val" : it will be reset to default browser value!! (Run the test code showed below.)

So I made a simple function to solve this problem and continue to use a setAttribute-like approach.

Using the Code

I wrote the setStyle method in HTMLElement interface, that represents any HTML element, so to use it easily.

This is the function:

HTMLElement.prototype.setStyle = function(str) {    
    var props = str.split(";"); //get properties
    for(var i = 0; i < props.length; i++) {
        var t = props[i].split(":"); //t[0] = property - t[1] = value
        this.style[fixPropName(t[0].trim())] = t[1].trim(); //trim removes white space(s)
    }
}

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

//trasform text, for example, from background-color to backgroundColor
function fixPropName(str) {
    var tArray = str.split("-");
    var prop = tArray[0];
    for(var i = 1; i < tArray.length; i++) {
        prop += tArray[i].capitalize();
    }
    return prop
}

Now you can use this function, in this way:

var div = document.getElementById("IdElement");
div.setStyle("background:blue; color: white");

This function is just a millisecond (on my PC, and sometimes) slower than setAttribute method, but now you are sure to haven’t unexpected CSS properties change.

To make a test, run this code:

<script>
HTMLElement.prototype.setStyle = function(str) {    
    var props = str.split(";"); //get properties
    for(var i = 0; i < props.length; i++) {
        var t = props[i].split(":"); //t[0] = property - t[1] = value
        this.style[fixPropName(t[0].trim())] = t[1].trim(); //trim removes white space(s)
    }
}

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

//trasform text, for example, from background-color to backgroundColor
function fixPropName(str) {
    var tArray = str.split("-");
    var prop = tArray[0];
    for(var i = 1; i < tArray.length; i++) {
        prop += tArray[i].capitalize();
    }
    return prop
}
window.addEventListener("DOMContentLoaded", 
function(e) {

var div = document.getElementById("cc");
div.style.opacity = "1";
str = "Opacity: " + div.style.opacity + "<br />";
var endTime, startTime = Date.now(); 
div.setAttribute("style","background:green;color:black");
endTime = Date.now(); 
str += "TimeExec: " +(startTime-endTime) + "<br />";
str += "Opacity: " + div.style.opacity + "<br /><br />";

div.style.opacity = "1";
str += "Opacity: " + div.style.opacity + "<br />";
startTime = Date.now(); 
div.setStyle("background:blue; color: white");
endTime = Date.now(); 
str += "TimeExec: " +(endTime-startTime) + "<br />";
str += "Opacity: " + div.style.opacity + "<br />";

div.innerHTML = str;
}, false);
</script>

<div id="cc">To to to</div>

Article on CodeProject

How linux distros protect you (parte 1)

Ultimo aggiornamento: 22-02-2016

Questo è il primo di una serie d’ articoli, dove voglio fare un resoconto delle difese ai buffer overflow, format string e pericoli vari presenti su linux.
Su linux (come su altri s.o.) ci sono protezioni sia a livello di compilazione che d’ esecuzione, potrei finire l’articolo postando il link al Hardening sul sito di Debian, però voglio approfondire con altri articoli trovati in giro per la rete e facendo esempi.
In questo primo articolo faccio una breve introduzione al formato ELF e al caricamento in memoria da parte del kernel dell’eseguibile, mostrando anche una prima protezione.
Per comprendere l’articolo ed i suoi link, si dovrebbero avere perlomeno le basi di programmazione C, della compilazione e linking con GCC, assembly INTEL ed anche una base di Sistemi Operativi non farebbe male :P. Continue reading How linux distros protect you (parte 1)

Javascript specific knowdlege

This week-end I found on Twitter an Javascript escape contest: http://escape.alf.nu. At level 9 there’s need of specific js knowledge:

1) As you know, you can call an object method with: obj.method() or with obj[‘method’](), so using the object like an array, ad accessing to it with a string, so you should also do this: obj[‘meth’+’od’]() and you will have the same result. Continue reading Javascript specific knowdlege

Come cifrare un oggetto java tramite JCA

Ultimo aggiornamento: 28-10-2013

In Java per il salvataggio di oggetti cifrati si può utilizzare la JCA: Java Cryptography Architecture, presente di default nell’ambiente Java.

Il JCA è stato progettato su “engine” criptografici e definendo delle classi che forniscono le funzionalità a questi engine.
Per i nostri scopi useremo l’engine Cipher insieme agli oggetti Sealed, utilizzati proprio per memorizzare oggetti cifrati.
Il cifrario usato è un AES con mode:CBC e padding:PKCS5Padding.

Continue reading Come cifrare un oggetto java tramite JCA

Use RequestAnimationFrame with setTimeout-like function

Introduction

In this article I’ll show how to use jsTimers-rAF, a library that makes use of RequestAnimationFrame simply as the use of setTimeout or setInterval function.

In github there’re some examples of use, but now I’ll show how to use the library to change the background on the page, as a div, or what else you can imagine to do with requestAnimationFrame. Continue reading Use RequestAnimationFrame with setTimeout-like function