Wie immer ohne Gewähr. Beispiel klappt ;-).
[code=Java]
public class Test
{
public static void main(String[] args)
{
byte[] in_60 = new byte[60];
System.arraycopy(new byte[]{'B','A','5','9','4','E','0','F','B','9','E','B','B','D','3','0'}, 0, in_60, 0, 16);
byte[] test = pad10star1(in_60, 8);
System.out.println(new String(test));
return;
}
public static byte[] pad10star1(byte[] M, int n) {
//Padded die Nachricht M, so dass eine vielfache Länge von r erreicht wird
//M: message pair (length in bits, string of hex characters ('9AFC...')
//n: length in bits (must be a multiple of 8)
//Example: pad10star1([60, 'BA594E0FB9EBBD30'],8) returns 'BA594E0FB9EBBD93'
byte[] my_string=M;
int my_string_length=M.length;
if (n % 8 !=0)
throw new RuntimeException("n must be a multiple of 8");
// Check the length of the provided string
if (my_string.length % 2 !=0) {
//Pad with one '0' to reach correct length (don't know test
//vectors coding)
my_string=new byte[my_string_length+1];
System.arraycopy(M, 0, my_string, 0, M.length);
my_string[my_string_length]='0';
}
if (my_string_length > my_string.length/2*8) {
throw new RuntimeException("the string is too short to contain the number of bits announced");}
int nr_bytes_filled = my_string_length/8;
int nbr_bits_filled = my_string_length % 8;
int l = my_string_length % n;
int my_byte;
if ((n-8) <= l && l <= (n-2))
{
if (nbr_bits_filled == 0)
{
my_byte = 0;
}
else
{
byte[] bytes = new byte[2];
System.arraycopy(my_string, nr_bytes_filled*2, bytes, 0, bytes.length);
my_byte=Integer.parseInt(new String(bytes),16);
}
my_byte=(my_byte>>(8-nbr_bits_filled));
my_byte=my_byte+(1<<(nbr_bits_filled))+(1<<7);
byte[] bytes = Integer.toString(my_byte, 16).getBytes();
byte[] temp = new byte[nr_bytes_filled*2+2];
System.arraycopy(my_string, 0, temp, 0, nr_bytes_filled*2);
System.arraycopy(bytes, 0, temp, nr_bytes_filled*2, 2);
my_string=temp.clone();
}
else
{
if (nbr_bits_filled == 0)
{
my_byte = 0;
}
else
{
byte[] bytes = new byte[2];
System.arraycopy(my_string, nr_bytes_filled*2, bytes, 0, bytes.length);
my_byte=Integer.parseInt(new String(bytes),16);
}
my_byte=(my_byte>>(8-nbr_bits_filled));
my_byte=my_byte+(1<<(nbr_bits_filled));
byte[] bytes = Integer.toString(my_byte, 16).getBytes();
byte[] temp = new byte[nr_bytes_filled*2+2];
System.arraycopy(my_string, 0, temp, 0, nr_bytes_filled*2);
System.arraycopy(bytes, 0, temp, nr_bytes_filled*2, 2);
int newSize = temp.length;
while((8*newSize/2)%n < (n-8))
{
newSize+=2;
}
newSize+=2;
my_string=new byte[newSize];
System.arraycopy(temp, 0, my_string, 0, temp.length);
my_string[newSize-2]='8';
}
return my_string;
}
}
[/code]