Ich habe folgenden Code geschrieben. Die jeweilige funktion soll entweder das übergebene byte[] verschlüsseln oder entschlüsseln. Wenn ich jedoch nun versuche eine Nachricht mit eine länge von 157 byte zu verschlüsseln resultiert dies in ein 256byte langes Array. Soll dieses nun abschließend wieder entschlüsselt werden, wird eine "javax.crypto.BadPaddingException: Message is larger than modulus" geworfen mit der ich nichts anfangen kann. Ich habe selbst nach mehrmaligem suchen keinen Fehler entdecken können. Darum möchte ich hier fragen, ob ich etwas übersehen habe.
Java:
public byte[] encryptRSA(Key publickey, byte[] data) {
try {
final int blocksize = Setup.ENCRYPTION_RSA_KEY_SIZE / 8 - 11; //Setup.ENCRYPTION_RSA_KEY_SIZE = 2048
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int i = 0; i < data.length; i = i + blocksize) {
byte[] tmp = new byte[blocksize];
if (tmp.length < data.length - i) {
System.arraycopy(data, i, tmp, 0, tmp.length);
} else {
for (int j = 0; j < data.length - i; j++) {
tmp[j] = data[i + j];
}
}
Cipher cipher = Cipher
.getInstance(Setup.ENCRYPTION_KEY_ALGORITHEM); //Setup.ENCRYPTION_KEY_ALGORITHEM = "RSA"
cipher.init(Cipher.ENCRYPT_MODE, publickey);
tmp = cipher.doFinal(tmp);
bos.write(tmp);
}
return bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
[/Java]
[Java]
public byte[] decryptRSA(Key privateKey, byte[] encrypted) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
final int blocksize = Setup.ENCRYPTION_RSA_KEY_SIZE / 8; //Setup.ENCRYPTION_RSA_KEY_SIZE = 2048
for (int i = 0; i < encrypted.length; i = i + blocksize) {
byte[] tmp = new byte[blocksize];
System.arraycopy(encrypted, i, tmp, 0, blocksize);
Cipher cipher = Cipher
.getInstance(Setup.ENCRYPTION_KEY_ALGORITHEM); //Setup.ENCRYPTION_KEY_ALGORITHEM = "RSA"
cipher.init(Cipher.DECRYPT_MODE, privateKey);
tmp = cipher.doFinal(tmp);
bos.write(tmp);
}
return bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}