Newsgroup: it.comp.java
Titolo: Alterare array di byte

Salve a tutti, ho un piccolo problema con il seguente programma. Il
programma si compone di 4 classi, mi scuso inizialmente se sono un pò
lunghe ma forse è meglio che vediate tutto per capire dove stà il
problema.
/**
* Sender is a partecipant which want send a message to a verifier.
* In order to authenticate the message, sender send an encrypted
digest
* to the verifier.
*
* @author Simone Di Cola
* @version 26 Nov 2007
*/
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import java.security.Security;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Sender
{
// instance variables
private String text;
private Key prvKey;
public Key pblKey;
public byte[] encriptDigest;
/**
* Constructor for objects of class Sender
*/
public Sender( String originalText)
{
// initialise instance variables
text = new String(originalText);
}
/**
* MessageDigest : estimated the message digest using SHA-1 algoritm
*
* @param void
* @return digest
*/
private byte[] msgDigest() throws NoSuchAlgorithmException
{
MessageDigest hash = MessageDigest.getInstance("SHA1");
hash.update(Utils.toByteArray(text));
byte[] digest=hash.digest();
return digest;
}
/**
* keyCreator: generate a pair of key
*
* @param void
* @return void
*/
private void keyCreator() throws NoSuchAlgorithmException
{
KeyPairGenerator keyGen= KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(512, random);
KeyPair pair = keyGen.generateKeyPair();
pblKey = pair.getPublic();
prvKey = pair.getPrivate();
}
/**
* encryptDigest: encrit the digest using the private key
*
* @param void
* @return void
*/
public void encryptDigest () throws Exception
{
byte [] mydigest =msgDigest(); //Create the digest
keyCreator(); //Generate the pair of keys
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, prvKey); //Encrypt
encriptDigest = cipher.doFinal(mydigest); //Store the result
}
}
/**
* Verifier is the second partecipant and provide to verify the
authenticity
* of the sender. For do this decrypt the message with the public key,
recalculates
* a new digest and compare the two digits.
*
* @author Simone Di Cola
* @version 26 Nov 2007
*/
import java.security.Key;
import javax.crypto.Cipher;
import java.security.Security;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Verifier
{
// instance variables
private byte [] old_digest;
private Key publicKey;
private String text;
/**
* Constructor for objects of class Verifier
*/
public Verifier(String message,byte[] digest, Key pubKey)
{
// initialise instance variables
text = new String(message);
old_digest= digest;
publicKey= pubKey;
}
/**
* verifyAuth decrypt the message with the public key,
* recalculates a new digest and compare the two digits.
*
* @param void
* @return true if authentication is verify, false otherwise.
*/
public Boolean verifyAuth() throws Exception
{
/**
* decrypt the digest he has received from the Sender
*/
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE,publicKey);
byte[] digestDecripted = cipher.doFinal(old_digest);
/**
* recalculate a new digest from the text received
*/
MessageDigest hash = MessageDigest.getInstance("SHA1");
hash.update(Utils.toByteArray(text));
byte [] new_digest = hash.digest();
/**
* compare these two digests
*/
return (hash.isEqual(digestDecripted,new_digest));
}
}
/**
* Adversary is a third partecipant, wich is placed between Sender and
Verifier
* and may change either the original message, or encrypted digest, or
both.
*
* @author Simone Di Cola
* @version 28 Nov 2007
*/
import java.lang.System;
public class Adversary
{
/**
* Alteration of original message
*
* @param msg is the original message
* @return the message changed
*/
public String alteration(String msg)
{
return msg.replace('a','o'); // Returns a new string resulting from
replacing all occurrences of 'a' with 'o';
}
/**
* Overload of procedure above, this time with the encrypted digest
*
* @param encrDig is the encripted digest send by Sender
* @return the digest alterated
*/
public byte[] alteration(byte[] encrDig)
{
byte [] altDig = Utils.toByteArray("alteration");
System.out.println("ORIGIN:"+Utils.toHex(encrDig));
System.arraycopy(altDig,0,encrDig,encrDig.length-
altDig.length,altDig.length); //change the last ten values
System.out.println("MODIFI:"+Utils.toHex(encrDig));
return encrDig;
}
}
E qui il main
/**
* Main class of message authentication using SHA-1 hash alghoritm
*
* @author Simone Di Cola
* @version 26 Nov 2007
*/
import java.util.Scanner;
public class main
{
public static void main () throws Exception
{
String message = new String("This is the original message");
Sender sender = new Sender (message);
sender.encryptDigest();
Adversary advers = new Adversary();
System.out.println("\n Enter 1 alterate the message \n 2 for alterate
the encript digest \n any other number for no alteration \n ");
Scanner sc = new Scanner(System.in);
byte [] digest= sender.encriptDigest;
int choise = sc.nextInt();
switch (choise)
{
case 1: // 1 ALTERATE MESSAGE
{
message = advers.alteration(message); //message.replace('a','o');
break;
}
case 2: // 2 ALTERATE ENCRIPT DIGEST
{
digest = advers.alteration(sender.encriptDigest);
break;
}
}
Verifier receiver = new Verifier(message,digest,sender.pblKey);
if (receiver.verifyAuth())
System.out.println("Authentication success\n");
else
System.out.println("Authentication failed\n");
}
}
In pratica ho un problema al case 2, esattamente il seguente:
javax.crypto.BadPaddingException: Data must start with zero
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:308)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:255)
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at Verifier.verifyAuth(Verifier.java:48)
at main.main(main.java:40)
Dove caspita è che sbaglio nell'alterare l'array di byte ? Grazie a
chiunque mi può dare una mano.

 chiunque mi può dare una mano.
Scusa, ma se non ho capito male, cambi gli ultimi 10 byte del digest,
non del messaggio.
Il risultato è una stringa che non è una digest RSA valido, infatti c'è
un errore nel padding.
Ciao.

 - Show quoted text -
Infatti devo modificare il digest criptato, non il messaggio

 Infatti devo modificare il digest criptato, non il messaggio
Allora l'errore č corretto, il digest č stato alterato e non č piů valido.
Cosa ti saresti aspettato?