java - Encrypting plain text using own RSA implementation -


im trying encrypt , decrypt string, , file containing plaintext, using own rsa implementation in java. ive tried following countless examples here on , web, people using either built in java rsa function or own implementation of algorithm, cant of them work, in decrypted text never matches original string.

i dont know if doing wrong encoding/decoding of text, have tried using default utf-8 , base64 encoders in java.xml.* no luck there either. im not using padding, dont know if thats necessary work @ all, , im using arbitrary key length, ive tried changing size of, , doesnt make things work either. exercise myself, no ones information going protected or anything. im not sure issue is, here code, tries encrypt/decrypt simple string:

   biginteger 1 = new biginteger("1");    securerandom rand = new securerandom();     biginteger d;    biginteger e;    biginteger n;    biginteger p = biginteger.probableprime(10, rand); // 10 arbitrary, have tried different numbers    biginteger q = biginteger.probableprime(10, rand);    biginteger phi = (p.subtract(one)).multiply(q.subtract(one));     n = p.multiply(q); //10 bits * 10 bits = ??? bits key length    e = new biginteger("65537"); //public key exponent     d = e.modinverse(phi); //private key exponent     string string = "this test";    byte[] bytes = string.getbytes();    biginteger plaintext = new biginteger(bytes);    biginteger ciphertext = plaintext.modpow(e, n);    biginteger originalmessage = ciphertext.modpow(d, e);     system.out.println(string.getbytes());    system.out.println(ciphertext);    system.out.println(originalmessage); 

the output has different values 3 things everytime run program: in same relative form:

[b@52d85409 157529 24312 

several notes:

  1. your modulus (the n in example) needs least long amount of information want encrypt. 1 of caveats of rsa, why things aes better suited longer streams files. below have chosen 128 range of p , q instead of 10, other values work well.
  2. it's ciphertext.modpow(d, n)
  3. you have convert biginteger string print properly, so: new string(originalmessage.tobytearray())

putting together:

biginteger 1 = new biginteger("1"); securerandom rand = new securerandom();  biginteger d, e, n; biginteger p = biginteger.probableprime(128, rand); biginteger q = biginteger.probableprime(128, rand); biginteger phi = (p.subtract(one)).multiply(q.subtract(one));  n = p.multiply(q); e = new biginteger("65537"); d = e.modinverse(phi);  string string = "this test"; biginteger plaintext = new biginteger(string.getbytes()); biginteger ciphertext = plaintext.modpow(e, n); biginteger originalmessage = ciphertext.modpow(d, n); string decrypted = new string(originalmessage.tobytearray());  system.out.println("original: " + string); system.out.println("decrypted: " + decrypted); 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -