انا مش عارفه ايش الخطأ في الكود التالي
import javax.crypto.provider.key.RawSecretKey;
import javax.security.Cipher;
import javax.security.Provider;
import javax.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
public class ProvaDESCryptix {
public ProvaDESCryptix() {
String message = "Fabrizio";
String chiave = "Fabrizio";
byte[] stringCryptix, stringJCE;
byte[] encryptedCryptix, encryptedJCE;
long before;
System.out.println("*** Cryptix ***");
try {
// Inizializzazione Cryptix
javax.security.Cipher ipherCryptix = javax.security.Cipher.getInstance("DES", "Cryptix");
RawSecretKey sKey = new RawSecretKey("DES", chiave.getBytes());
cipherCryptix.initEncrypt(sKey);
cipherCryptix.initDecrypt(sKey);
stringCryptix = message.getBytes();
before = System.currentTimeMillis();
encryptedCryptix = cipherCryptix.crypt(stringCryptix);
System.out.println("Cryptix: Tempo impiegato: " +
(System.currentTimeMillis() - before) + " ms.");
System.out.println(toCharArray(message.getBytes()));
// System.out.println(toCharArray(encryptedCryptix));
System.out.println(byteArrayToHexString(encryptedCryptix));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println();
System.out.println();
System.out.println("*** JCE ***");
// Inizializzazione JCE
Provider sunJce = new com.sun.crypto.provider.SunJCE();
Security.addProvider(sunJce);
javax.crypto.Cipher cipherJCE;
try {
KeyGenerator kgen = KeyGenerator.getInstance("DES");
SecretKeySpec keySpec = new SecretKeySpec(chiave.getBytes(),"DES");
cipherJCE = javax.crypto.Cipher.getInstance("DES");
cipherJCE.init(javax.crypto.Cipher.ENCRYPT_MODE, skeySpec);
stringJCE = message.getBytes();
before = System.currentTimeMillis();
encryptedJCE = cipherJCE.doFinal(stringJCE);
System.out.println("JCE: Tempo impiegato: " +
(System.currentTimeMillis() - before) + " ms.");
System.out.println(toCharArray(message.getBytes()));
System.out.println(toCharArray(encryptedJCE));
System.out.println(byteArrayToHexString(encryptedJCE));
} catch (Exception e) {
e.printStackTrace();
}
}
public static char[] toCharArray(byte[] B) {
char[] c = new char[b.length];
for (int i = 0; i < b.length; i++)
c = (char)b;
return c;
}
public String byteArrayToHexString(byte[] B) {
StringBuffer sb = new StringBuffer();
StringBuffer singleByte = new StringBuffer();
int k;
for (int i = 0; i < b.length; i++) {
singleByte.append(Integer.toHexString((int)b & 0x000000FF).toUpperCase());
if (singleByte.length() == 1)
sb.append("0" + singleByte);
else
sb.append(singleByte);
singleByte.setLength(0);
}
return sb.toString().trim();
}
public byte[] hexStringToByteArray(String s) {
int length = s.length() / 2;
byte[] b = new byte[length];
String singleByte;
for (int i = 0; i < length; i++) {
singleByte = s.substring(i * 2, i * 2 + 2);
try {
b = Byte.parseByte(singleByte, 16);
} catch (NumberFormatException e) {
System.err.println("Errore di conversione String->byte allaposizione " + i);
b = (byte)0;
}
}
return b;
}
public static void main(String[] args) {
new ProvaDESCryptix();
}
}