Author Topic: Cryptography Implementation Discussion  (Read 16033 times)

Dumb enough?

Pretty sure that people who write cryptographical algorithms are highly intelligent.
dumb enough to try*
i'm smart enough to know i'm not smart enough
... make sense?

Funny thing is I actually wrote my own 256-bit encryption. I have no idea if it's actually that secure or not.

I had a basic implementation of RSA (even had a PEM file writer but not reader (kinda useless)) on the school pc's but forgot to email to myself.

rewriting on pc

exec("./base64.cs");
exec("./prime.cs");
exec("./math.cs");

function rsa_keygen(%p,%q)
{
   %n = %p*%q;
   %m = (%p-1) * (%q-1);
   
   %e = getRandom(10,100);
   while(!coprime(%k,%e))
      %e += 1;

   %d = 0;
   while(Math_Mod(%d * %e,%m) != 1)
      %d += 1;

   return %n SPC %d SPC %e;   
}
function rsa_enc(%c,%n,%e)
{
   return Math_Mod(Math_Pow(%c,%e),%n);
}
function rsa_dec(%c,%n,%d)
{
   return Math_Mod(Math_Pow(%c,%d),%n);
}
« Last Edit: November 15, 2013, 04:55:31 PM by 0xBRIANSMITH »

I'd just like to note that because %e is undefined in rsa_keygen the condition of 1 will never be met and thus this function will crash your Blockland instance.

I'd just like to note that because %e is undefined in rsa_keygen the condition of 1 will never be met and thus this function will crash your Blockland instance.
Woops I'll fix in a bit

what is %m supposed to be

(%q -1)(%p -1) = %m or the totient thingy, %z
« Last Edit: November 15, 2013, 04:17:03 PM by Lugnut »


i can do 19 bit rsa encrypt and decrypt in less than a second on my ti 84 which is like, stuffter than torquestuff

this is like loving awesome guys this is really honestly possible

edit: i know 19 bits is like super loving stuff, and something like 4096 would take loving forever, but without the optimizations, just 9 bits would take like forever

edit: my brain died and said "like" 4 times in one post
stuff
« Last Edit: November 15, 2013, 05:53:26 PM by Lugnut »

Lol, if we're doing RSA we need to use at least 1024 bits, which is 300 decimal digits.

Yes, it's entirely possible. I don't think there's any better way to do modulus than the naive method that will actually save any time, however I could easily be wrong. I've been quite busy lately and still am, so if anyone else is able to continue working on this it would be greatly appreciated.

this fellow suggests a simple shortcut and a binary search method

i don't really understand them lol, do you? http://stackoverflow.com/questions/2773628/better-ways-to-implement-a-modulo-operation-algorithm-question

feel free to ignore the op, i don't think he knows what he's talking about

i can do 19 bit rsa encrypt and decrypt in less than a second on my ti 84 which is like, stuffter than torquestuff

Just to note, TI Basic processes math functions very efficiently (as you'd expect from a calculator...) which is why a lot of people will tell you to use multiplication of booleans by some values instead of using an if statement to check for its value. I get your point, but it's a bit of a stretch to say what you did.

I found some promising looking code on one of the questions linked to the one lugnut gave:
Code: [Select]
int mod(int a, int b)
{
    int x = b;
    while (!(a < b))
        b <<= 1;

    while (!(a < x))
    {
        while(a < b)
            b >>= 1;
     a -= b;
    }
    return a;
}

I'm going to implement fast dividing by single digit numbers soon and then implementing this will be a trivial task, and should outperform the naive division method.

EDIT: optimizations, wrote the comparisons using only less than and not (which are very easy to calculate) and multiplying by 0.5 with third parameter -1 should work just as fast as the method I had in mind for dividing by 2. I'm writing the method now.

EDIT 2: Finished writing the code. Could someone test it out for me?

Code: [Select]
function Math_Mod(%a, %b)
{
%x = %b;
while(!alessthanb(%a, %b))
%b = Math_Multiply(%b, 2);

while(!alessthanb(%a, %x))
{
while(alessthanb(%a, %b))
%b = Math_Multiply(%b, 0.5, -1);
%a = Math_Subtract(%a, %b);
}
return %a;
}

function alessthanb(%a, %b)
{
%c = strLen(%a); %d = strLen(%b);

//Only do character-by-character comparisons if lengths are equal
if(%c < %d)
return true;
else if(%d < %c)
return false;

//Exact equals check
if(%a $= %b)
return false;

for(%x = 0; %x < %c; %x++)
{
%y = getSubStr(%a, %x, 1); %z = getSubStr(%b, %x, 1);
if(%y < %z)
return true;
else if(%y > %z)
return false;
}
}
« Last Edit: November 15, 2013, 07:24:28 PM by Ipquarx »



"The computation is roughly equivalent to breaking a 700 bit RSA key. However, this might be an advance warning that 1024 bit RSA used in secure online commerce should be deprecated, since they may become breakable in the near future. Cryptography professor Arjen Lenstra observed that 'Last time, it took nine years for us to generalize from a special to a nonspecial, hard-to-factor number' and when asked whether 1024-bit RSA keys are dead, said: 'The answer to that question is an unqualified yes.'"
either way, all i was saying is that even my stuffty calculator is fast when it comes to some operations that i had originally thought were extremely taxing
ps: nice stunt with the color
i don't think rsa is a smart thing for us to implement - we should focus on ECC, with it's shorter keylengths and faster algorithm.
i've been looking for a good example using small keysizes that i can work through manually, but i haven't been successful. however, once i figure out and work through generation of ecc keypairs, encryption, and decryption, i'll be ready to describe it to you guys or implement it myself