Let's do this:
As R.M suggested in the comments, you should learn the way Mathematica defines functions.
Here's how I've defined your functions:
keygen[keybitlength_] :=
Module[{kbl, kbl1, pPrime, qPrime, nKey, phiOFn, eKey, dKey},
kbl = 2^(keybitlength/2);
kbl1 = 2^(keybitlength/2 - 1);
pPrime = RandomPrime[{kbl1, kbl}];
qPrime = RandomPrime[{kbl1, kbl}];
nKey = pPrime*qPrime;
phiOFn = (pPrime - 1) (qPrime - 1);
While[eKey = RandomPrime[{2, phiOFn}]; ! CoprimeQ[eKey, phiOFn]];
dKey = PowerMod[eKey, -1, phiOFn];
{{eKey, nKey}, {dKey, nKey}}]
encryption[message_, list : {eKey_, nKey_}] := PowerMod[message, eKey, nKey]
decryption[encmessage_, list : {dKey_, nKey_}] := PowerMod[encmessage, dKey, nKey]
Now it's simple to use this for numeric values:
keys = keygen[32]
(*{{1680015751, 2207995403}, {383738359, 2207995403}}*)
encryption[123456, keys[[1]]]
(*1731973844*)
decryption[1731973844,keys[[2]]]
(*123456*)
The best way I can figure out to do strings is a ToCharacterCode
method.
Let's redefine the encryption
and decryption
functions to handle these.
encryption[message_, list : {eKey_, nKey_}] :=
Module[{temp},
If[StringQ[message], (temp = ToCharacterCode[message];PowerMod[#, eKey, nKey] & /@temp),
PowerMod[message, eKey, nKey]]]
decryption[encmessage_, list : {dKey_, nKey_}] := Module[{temp},
If[ListQ[encmessage], (temp = PowerMod[#, dKey, nKey] & /@ encmessage;
FromCharacterCode[temp]), PowerMod[encmessage, dKey, nKey]]]
Testing:
keys = keygen[16]
enc = encryption["Hello World!", keys[[1]]]
decryption[enc, keys[[2]]]
(*{{35437, 41693}, {39973, 41693}}*)
(*{2592, 40831, 8438, 8438, 12888, 12500, 32151, 12888, 21729, 8438, 39307, 198}*)
(*"Hello World!"*)
myFunc[par1_,par2_,... ]:=Module[{localVar1, localVar2,...}, statement1; statement2; ... ]
where the ellipsis indicate that you may use as many of the relevant construction as you wish. $\endgroup$