# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
--- D:\delphi7\utils\web\lib\libc\rsa\rsa-all.js.orig
+++ D:\delphi7\utils\web\lib\libc\rsa\rsa-all.js
@@ -1,3 +1,4 @@
+define(['lib'],function(){
 
 // *** base64.js ***
 
@@ -89,7 +90,7 @@
  * the server-side, but the defaults work in most cases.
  */
 var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
-var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
+var b64pad  = "="; /* base-64 pad character. "=" for strict RFC compliance   */
 
 /*
  * These are the functions you'll usually want to call
@@ -1782,7 +1783,7 @@
 }
 
 // PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
-function pkcs1pad2(s,n) {
+function pkcs1pad2(s,n,type) {
   if(n < s.length + 11) { // TODO: fix for utf-8
     alert("Message too long for RSA");
     return null;
@@ -1805,6 +1806,7 @@
     }
   }
   ba[--n] = 0;
+  if (type==2){
   var rng = new SecureRandom();
   var x = new Array();
   while(n > 2) { // random non-zero pad
@@ -1812,7 +1814,13 @@
     while(x[0] == 0) rng.nextBytes(x);
     ba[--n] = x[0];
   }
-  ba[--n] = 2;
+  }
+  else {
+    while(n > 2) { // FF pad
+      ba[--n] = 255;
+    }
+  }
+  ba[--n] = type;
   ba[--n] = 0;
   return new BigInteger(ba);
 }
@@ -1846,7 +1854,7 @@
 
 // Return the PKCS#1 RSA encryption of "text" as an even-length hex string
 function RSAEncrypt(text) {
-  var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
+  var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3,2);
   if(m == null) return null;
   var c = this.doPublic(m);
   if(c == null) return null;
@@ -1875,11 +1883,11 @@
 // Version 1.1: support utf-8 decoding in pkcs1unpad2
 
 // Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext
-function pkcs1unpad2(d,n) {
+function pkcs1unpad2(d,n,type) {
   var b = d.toByteArray();
   var i = 0;
   while(i < b.length && b[i] == 0) ++i;
-  if(b.length-i != n-1 || b[i] != 2)
+  if(b.length-i != n-1 || b[i] != type)
     return null;
   ++i;
   while(b[i] != 0)
@@ -1983,7 +1991,7 @@
   var c = parseBigInt(ctext, 16);
   var m = this.doPrivate(c);
   if(m == null) return null;
-  return pkcs1unpad2(m, (this.n.bitLength()+7)>>3);
+  return pkcs1unpad2(m, (this.n.bitLength()+7)>>3,2);
 }
 
 // Return the PKCS#1 RSA decryption of "ctext".
@@ -2002,3 +2010,59 @@
 RSAKey.prototype.generate = RSAGenerate;
 RSAKey.prototype.decrypt = RSADecrypt;
 //RSAKey.prototype.b64_decrypt = RSAB64Decrypt;
+
+//mk---------------------------------------
+
+/*
+ * I had to modify the code in order to support reverse-operations :
+ * encrypt with private, and decrypt with public
+ * this needed two new routines, found below,
+ * and also to patch the pkcs1pad and unpad routines to take a 'type' parameter and
+ * do the proper work
+ *
+ * if we need to update the version,
+ * run build.sh, which will generate the concatenated file,
+ * and then patch it using the diff file
+ *
+ */
+
+// Return the PKCS#1 RSA encryption of "text" as an even-length hex string - use private key
+function RSAEncryptPrivate(text) {
+  var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3,1);
+  if(m == null) return null;
+  var c = this.doPrivate(m);
+  if(c == null) return null;
+  var h = c.toString(16);
+  if((h.length & 1) == 0) return h; else return "0" + h;
+}
+
+// Return the PKCS#1 RSA decryption of "ctext".
+// "ctext" is an even-length hex string and the output is a plain string.
+function RSADecryptPublic(ctext) {
+  var c = parseBigInt(ctext, 16);
+  var m = this.doPublic(c);
+  if(m == null) return null;
+  return pkcs1unpad2(m, (this.n.bitLength()+7)>>3,1);
+}
+
+RSAKey.prototype.encryptPublic = RSAEncrypt;
+RSAKey.prototype.encryptPrivate = RSAEncryptPrivate;
+RSAKey.prototype.decryptPublic = RSADecryptPublic;
+RSAKey.prototype.decryptPrivate = RSADecrypt;
+
+_.extend(lib,{
+  BigInteger:BigInteger,
+  RSAKey:RSAKey,
+  base64:{
+   hex2b64:hex2b64,
+   b64tohex:b64tohex,
+   b64toBA:b64toBA
+  },
+  sha:{
+    hex_sha1:hex_sha1,
+    b64_sha1:b64_sha1,
+    sha1_test:sha1_vm_test
+  }
+});
+
+});
