PDA

View Full Version : perchè non dovrebbe funzionare il mio programma?


atelzut2
19-09-2014, 10:43
salve a tutti,
faccio un programmino stupido in java e tutto va bene, funziona alla perfezione.
la trsferisco su android e... crash tutto va male.
qualcuno mi sa dire il motivo?
il programma è un cifrario con chiave simmetrica AES. nonn fa altro che creare due cifrari uno per criptare una frase e l'altro per decriptarla. il problema si presenta quando uso il metodo k2string() che mi ritorna la chiave sotto forma di stringa (il metodo nasce per avere una stringa da poter usare nel costruttore cifrario(String k, String alg)).
metto un po di codice.

cifrario (non ho riportato tutti i metodi ma solo qulli che mi davano problemi):package com.example.cifrarioandroid;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.*;
import static org.apache.commons.codec.binary.Hex.*;
import static org.apache.commons.io.FileUtils.*;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import org.apache.commons.codec.DecoderException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Cifrario {

KeyGenerator keygenerator;
static SecretKey myDesKey;
Cipher eCipher=null;
Cipher dCipher=null;


public Cifrario(){
try
{
keygenerator = KeyGenerator.getInstance("AES");
keygenerator.init(128);
}

catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
myDesKey = keygenerator.generateKey();


// Create the cipher
try
{
eCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
dCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
}
catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}//costruttore



public Cifrario(String k, String alg){
try
{
eCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
dCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
}
catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


myDesKey = new SecretKeySpec(Base64.decodeBase64(k),alg);

}





public String getKAlg(){
return myDesKey.getAlgorithm();
}

public SecretKey getK()
{
return myDesKey;}

public String k2String(){

String stringKey = Base64.encodeBase64String(myDesKey.getEncoded());

return stringKey;
}

}//cifrario






mainActivity:package com.example.cifrarioandroid;





import android.support.v7.app.ActionBarActivity;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
private TextView testo, chiavi;
private String frase= "nel mezzo del cammin di nostra vita mi ritrovai per una selva oscura che la ritta via era smarrita";
Cifrario cript, decript;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testo = (TextView)findViewById(R.id.testo);
chiavi= (TextView)findViewById(R.id.chiavi);
cript= new Cifrario();
//////////////////////////////////////////////////
//il problema si presenta alla chiamata di k2String()
decript=new Cifrario(cript.k2String(),"AES");

///////////////////////////////////////////////////

}




public void onResume(){
super.onResume();
byte [] b=cript.encrypt(frase);

testo.setText(new String(decript.decrypt(b)));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}






@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}



grazie a chiunque mi possa darae una mano

<Gabrik>
19-09-2014, 10:58
Per prima cosa l'AES è simmetrico, quindi cifri e decifri con la stessa chiave,
se vuoi usare 2 chiavi diverse (tipicamente pubblica e privata) potresti usare l'RSA.

Il problema potrebbe essere la classe che si occupa del base64, su Android non sono presenti tutte le classi e i package che Java mette a disposizione,
prova ad usare l'implementazione del base64 fornita da bouncycastle

https://www.bouncycastle.org/java.html

fornisce anche classi adatte a cifrare e decifrare array di byte

Freaxxx
19-09-2014, 11:19
il "Java" su Android non è Java SE, EE o ME, quelli sono prodotti Sun / Oracle, Java su Android viene usato come linguaggio, il framework di riferimento è ben diverso da quello a cui sei probabilmente abituato .

Ti consiglio di documentarti bene sulle librerie Java disponibili su Android o di fare un porting di quelle che ti servono, comunque sia non ti aspettare di portare la tua applicazione Java da desktop ad Android così facilmente .

atelzut2
08-10-2014, 12:41
grazie mille.ho risolto usando android.util.base64 di android.