RSA encryption from java and decryption using php -


i trying rsa encrypt of user email id using java , trying decrypt using php. not successful below details

source in java:

package ia; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.security.keypair; import java.security.keypairgenerator; import java.security.nosuchalgorithmexception; import java.security.privatekey; import java.security.publickey; import java.util.date; import javax.crypto.cipher; import org.apache.commons.codec.binary.base64;    public class encryptionuntil { /** * string hold name of encryption algorithm. */ public static final string algorithm = "rsa";  /** * string hold name of private key file. */  public static final string private_key_file = "c:/keys/private/private.key";   /**  * string hold name of public key file.  */  public static final string public_key_file = "c:/keys/public/public.key";   /**   * generate key contains pair of private , public key using 1024   * bytes. store set of keys in prvate.key , public.key files.  *   * @throws nosuchalgorithmexception  * @throws ioexception  * @throws filenotfoundexception  */  public static void generatekey() { try {     final keypairgenerator keygen = keypairgenerator             .getinstance(algorithm);     keygen.initialize(512);     final keypair key = keygen.generatekeypair();      file privatekeyfile = new file(private_key_file);     file publickeyfile = new file(public_key_file);      // create files store public , private key     if (privatekeyfile.getparentfile() != null) {         privatekeyfile.getparentfile().mkdirs();     }     privatekeyfile.createnewfile();      if (publickeyfile.getparentfile() != null) {         publickeyfile.getparentfile().mkdirs();     }     publickeyfile.createnewfile();      // saving public key in file     objectoutputstream publickeyos = new objectoutputstream(             new fileoutputstream(publickeyfile));     publickeyos.writeobject(key.getpublic());     publickeyos.close();      // saving private key in file     objectoutputstream privatekeyos = new objectoutputstream(             new fileoutputstream(privatekeyfile));     privatekeyos.writeobject(key.getprivate());     privatekeyos.close(); } catch (exception e) {     e.printstacktrace(); }  }  /**  * method checks if pair of public , private key has been  * generated.  *   * @return flag indicating if pair of keys generated.  */ public static boolean arekeyspresent() {  file privatekey = new file(private_key_file); file publickey = new file(public_key_file);  if (privatekey.exists() && publickey.exists()) {     return true; } return false; }  /** * encrypt plain text using public key. *  * @param text *            : original plain text * @param key *            :the public key * @return encrypted text * @throws java.lang.exception */ public static byte[] encrypt(string text, publickey key) { byte[] ciphertext = null; try {     // rsa cipher object , print provider     final cipher cipher = cipher.getinstance(algorithm);     // encrypt plain text using public key     cipher.init(cipher.encrypt_mode, key);     ciphertext = cipher.dofinal(text.getbytes());  } catch (exception e) {     e.printstacktrace(); } return ciphertext; }  /** * decrypt text using private key. *  * @param text *            :encrypted text * @param key *            :the private key * @return plain text * @throws java.lang.exception */ public static string decrypt(byte[] text, privatekey key) { byte[] dectyptedtext = null; try {     // rsa cipher object , print provider     final cipher cipher = cipher.getinstance(algorithm);      // decrypt text using private key     cipher.init(cipher.decrypt_mode, key);     dectyptedtext = cipher.dofinal(text);  } catch (exception ex) {     ex.printstacktrace(); }  return new string(dectyptedtext); }  public static void encdecrypt (string originaltext) throws filenotfoundexception,     ioexception, classnotfoundexception{     objectinputstream inputstream = null;      // encrypt string using public key     inputstream = new objectinputstream(new fileinputstream(             public_key_file));     final publickey publickey = (publickey) inputstream.readobject();     final byte[] ciphertext = encrypt(originaltext, publickey);     final string ciphertext64 = base64.encodebase64urlsafestring(ciphertext);       // decrypt cipher text using private key.     inputstream = new objectinputstream(new fileinputstream(             private_key_file));     final privatekey privatekey = (privatekey) inputstream.readobject();     final string plaintext = decrypt(base64.decodebase64(ciphertext64), privatekey);      // printing original, encrypted , decrypted text     system.out.println("original text: " + originaltext);     //system.out.println("encrypted text: " + new string(ciphertext));     system.out.println("base 64 text: " +ciphertext64);     system.out.println("decrypted text: " + plaintext);   }  /**   * test encryptionuntil  */  public static void main(string[] args) {  try {      // check if pair of keys present else generate those.     if (!arekeyspresent()) {         // method generates pair of keys using rsa algorithm ,         // stores         // in respective files         generatekey();     }      final string originaltext = "rohit.gupta@infoaxon.com";     date dd = new date();     system.out.println(dd);     final string currenttime = string.valueof(dd.gettime());      encdecrypt(originaltext);     encdecrypt(currenttime);  } catch (exception e) {     e.printstacktrace(); } } } 

private key:

aced 0005 7372 0014 6a61 7661 2e73 6563 7572 6974 792e 4b65 7952 6570 bdf9 4fb3 889a a543 0200 044c 0009 616c 676f 7269 7468 6d74 0012 4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b 5b00 0765 6e63 6f64 6564 7400 025b 424c 0006 666f 726d 6174 7100 7e00 014c 0004 7479 7065 7400 1b4c 6a61 7661 2f73 6563 7572 6974 792f 4b65 7952 6570 2454 7970 653b 7870 7400 0352 5341 7572 0002 5b42 acf3 17f8 0608 54e0 0200 0078 7000 0001 5830 8201 5402 0100 300d 0609 2a86 4886 f70d 0101 0105 0004 8201 3e30 8201 3a02 0100 0241 008d 442f 2df6 7e5d 6e48 16a9 70a1 9006 c932 1c47 71de 6cb7 81eb 8483 e5e4 73ee b06f 5e73 ed1e 851d 54f1 2d86 6491 479a d314 8897 f7e6 85dc 65ca f1f9 318e cc41 4702 0301 0001 0240 2306 f713 d47c bcb9 ed92 00ed 7681 f9cc c56a 11a5 005b c09c ac43 2d59 416e 258e a6a3 c4bb cc6d bcf1 7b5b 24d6 ff95 a146 2040 4d27 a92d cb9e ccaa 3519 fc85 50d1 0221 00db b9b2 a4c4 3ef6 4780 303c 6798 819f 1a9a 04ca dced 0f9e 0cfd b4a5 75f5 bdf0 f502 2100 a496 8e0e d531 5e0b b427 6966 2b55 546b 2a8a a5d0 dcf4 bbfd 7ce9 1c56 d79c 13cb 0220 2736 8cdb 3aea c1a9 2107 7ac0 4247 5fcd af8f 0b65 4229 775b 7a2b b31b ca2f 8bc1 0220 28e3 e6a3 34c0 3117 4348 cf5c bcc3 5457 d397 e29e 4366 e215 9624 ec0f 7f3d 9d85 0221 00b4 fdab 7ff3 9804 f6f5 00bf 32a1 3c5c 7517 b0ab 90e1 f20a c9df 8d30 f778 c729 e074 0006 504b 4353 2338 7e72 0019 6a61 7661 2e73 6563 7572 6974 792e 4b65 7952 6570 2454 7970 6500 0000 0000 0000 0012 0000 7872 000e 6a61 7661 2e6c 616e 672e 456e 756d 0000 0000 0000 0000 1200 0078 7074 0007 5052 4956 4154 45 

rsa encrypt output:

    [b@1dd8823 

and base64 encode output:

    cslqf1wj2moxrzdtfjua7idy6cty_3o2whuhfww8zwsqapot-vscv_kqw_gm2cdw5w1ssa3izbdrpnij1-wmuq 

php code decryption

 <?php  function der2pem($der_data, $kind) {  $pem = chunk_split(base64_encode($der_data), 64, "\n");  $pem = "-----begin ".$kind."-----\n".$pem."-----end ".$kind."-----\n";  return $pem;  }   function to_pem($filename, $kind) {  // todo: handle errors  $f = fopen($filename, "r");  $der = fread($f, filesize($filename));  $pem = der2pem($der, $kind);  fclose($f);   return $pem;  }   function load_private_key($filename) {  $pem = to_pem($filename, "private key");  return openssl_pkey_get_private($pem);  }   function load_public_key($filename){  $pem = to_pem($filename, "public key");  return openssl_pkey_get_public($pem);  }    $private_key = load_private_key("private.key");     $public_key = load_public_key("public.key");   $ciphertext = base64_decode("[b@1dd8823");   #$ciphertext = "cslqf1wj2moxrzdtfjua7idy6cty_3o2whuhfww8zwsqapot-  vscv_kqw_gm2cdw5w1ssa3izbdrpnij1-wmuq";  echo "cipher = "; echo $ciphertext; echo '</br>';echo '</br>';  if (openssl_private_decrypt($ciphertext, $recovered_plaintext, $private_key)) {  echo  "recover text = "; echo $recovered_plaintext; echo '</br>'; } #} ?> 

the php output returns none...

try key.getpublic().getencoded() instead of key.getpublic(). same thing key.getprivate(). key isn't in format openssl supports.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -