01/09/06 13:23
jimi
Sono alle prime armi con la crittografia (per dirla in altri termini sono piuttosto ignorante... ), stò cercando di fare un esercizio in cui devo creare una coppia di chiavi con RSA, generare una chiave TripleDES.
Cifrare una stringa usando TripleDES, cifrare la chiave TripleDES con RSA, decifrare la chiave TripleDES (con RSA) decifrare la stringa.
Ho provato a cifrare e decifrare la stringa con RSA e funziona, ma quando cifro la chiave TripleDES qualcosa non va...
Il codice per cifrare e decifrare la chiave è questo...
public byte[] crittaKeyRSA(PublicKey RSAPublicKey, Cipher RSACipher, Key key){
byte[] dataEncoded=null;
byte[] keyToBytes=null;
try{
RSACipher.init(Cipher.ENCRYPT_MODE, RSAPublicKey);
}catch(InvalidKeyException inv){
System.out.println("ERRORE!!! Chiave non valida!!!"+inv);
}
try{
keyToBytes = key.getEncoded();
}catch(Exception un){
System.out.println("ERRORE!!!"+un);
}
try{
dataEncoded = RSACipher.doFinal(keyToBytes);
}catch(IllegalBlockSizeException err){
System.out.println("ERRORE!!!"+err);
}catch(BadPaddingException bpe){
System.out.println("ERRORE!!!"+bpe);
}
return dataEncoded;
}
public Key deCrittaKeyRSA(PrivateKey RSAPrivateKey, Cipher RSACipher, byte[] dataEncoded){
byte[] dataDecoded=null;
Key output=null;
try{
RSACipher=cifrarioRSA();
RSACipher.init(Cipher.DECRYPT_MODE, RSAPrivateKey);
}catch(InvalidKeyException inv){
System.out.println("ERRORE!!! La chiave non è valida"+inv);
}
try{
dataDecoded=RSACipher.doFinal(dataEncoded);
}catch(Exception e){
System.out.println("ERRORE!!"+e);
}
try{
SecretKey k = new SecretKeySpec(dataDecoded,"TripleDES"
output=k;
}catch(Exception e){
System.out.println("ERRORE!!"+e);
}
return output;
}
Cifrare una stringa usando TripleDES, cifrare la chiave TripleDES con RSA, decifrare la chiave TripleDES (con RSA) decifrare la stringa.
Ho provato a cifrare e decifrare la stringa con RSA e funziona, ma quando cifro la chiave TripleDES qualcosa non va...
Il codice per cifrare e decifrare la chiave è questo...
public byte[] crittaKeyRSA(PublicKey RSAPublicKey, Cipher RSACipher, Key key){
byte[] dataEncoded=null;
byte[] keyToBytes=null;
try{
RSACipher.init(Cipher.ENCRYPT_MODE, RSAPublicKey);
}catch(InvalidKeyException inv){
System.out.println("ERRORE!!! Chiave non valida!!!"+inv);
}
try{
keyToBytes = key.getEncoded();
}catch(Exception un){
System.out.println("ERRORE!!!"+un);
}
try{
dataEncoded = RSACipher.doFinal(keyToBytes);
}catch(IllegalBlockSizeException err){
System.out.println("ERRORE!!!"+err);
}catch(BadPaddingException bpe){
System.out.println("ERRORE!!!"+bpe);
}
return dataEncoded;
}
public Key deCrittaKeyRSA(PrivateKey RSAPrivateKey, Cipher RSACipher, byte[] dataEncoded){
byte[] dataDecoded=null;
Key output=null;
try{
RSACipher=cifrarioRSA();
RSACipher.init(Cipher.DECRYPT_MODE, RSAPrivateKey);
}catch(InvalidKeyException inv){
System.out.println("ERRORE!!! La chiave non è valida"+inv);
}
try{
dataDecoded=RSACipher.doFinal(dataEncoded);
}catch(Exception e){
System.out.println("ERRORE!!"+e);
}
try{
SecretKey k = new SecretKeySpec(dataDecoded,"TripleDES"
output=k;
}catch(Exception e){
System.out.println("ERRORE!!"+e);
}
return output;
}
aaa