Tutorial Pemrograman Kriptografi C++ dan Java

Zae
PROBLEM :
I am using rsa key to encrypt a long string. But it throws an exception like
javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:344)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)

CAUSE :
Using RSA to encrypt a large file is not a good idea. You could for example generate a random AES key, encrypt it using RSA and store it in the output file, and then encrypt the file itself with AES, which is much faster and doesn't have any problem with large inputs. The decryption would read the encrypted AES key, decrypt it, and then decrypt the rest of the file with AES.
" The RSA algorithm can only encrypt data that has a maximum byte length of the RSA key length in bits divided with eight minus eleven padding bytes, i.e. number of maximum bytes = key length in bits / 8 - 11 "
SOLVE
if key length 2048 maximum bytes data is 245 bytes
if key length 1024 maximum bytes data is 117 bytes
Zae

For Example :

p value is 47
q value is 71
public key value is 79
Message : "HARI INI"

Question :
How Is Encryption And Decryption Process ?

Answer :

MANUAL RSA ENCRYPTION PROCESS

Prime 1 (p) value :47
Prima 2 (q) value :71
public (e) key :79
N (p x q) value :3337
phi (N) --> (p-1) x (q-1) :3220
Private Key (d) : (1+k*3220)/79; k=1,2,3,....
find d with rounded results by trying k values
obtained private key value (d) :1019
So :
Public Key : (79,3220)
Private Key: (1019,3220)

Message (M) = HARI INI

===============
ENCRYPTION PROCESS
===============

Convert Message to Decimal Format
H = 72
A = 65
R = 82
I = 73
  = 32
I = 73
N = 78
I = 73

For this encryption process, I break m into smaller blocks, for example m is broken into six blocks that require 3 digits

M0=726
M1=582
M2=733
M3=273
M4=787
M5=003

CipherText (C) = Plaintext (M) ^ e mod N

C0 = 726 ^ 79 mod 3337 = 215
C1 = 582 ^ 79 mod 3337 = 776
C2 = 733 ^ 79 mod 3337 = 1743
C3 = 273 ^ 79 mod 3337 = 933
C4 = 787 ^ 79 mod 3337 = 1731
C5 = 3 ^ 79 mod 3337 = 158

CipherText: 215.776.1743.933.1731.158

Plaintext (M) =Ciphertext (C) ^ d mod N

P0 = 215 ^ 1019 mod 3337 = 726
P1 = 776 ^ 1019 mod 3337 = 582
P2 = 1743 ^ 1019 mod 3337 = 733
P3 = 933 ^ 1019 mod 3337 = 273
P4 = 1731 ^ 1019 mod 3337 = 787
P5 = 158 ^ 1019 mod 3337 = 3

Convert Desimal to Ascii
Return Decryption
72=H
65=A
82=R
73=I
32=
73=I
78=N
73=I

---- FINISH----