memory - Exception in thread "main" java.lang.OutOfMemoryError: Java heap space with BASE64Decoder -
i want create message signing , verifying software written in java.
thus, decided use code internet.
while codes have no syntax error, shows java heap space error.
however, error message shown below
exception in thread "main" java.lang.outofmemoryerror: java heap space @ java.util.arrays.copyof(unknown source) @ java.io.bytearrayoutputstream.write(unknown source) @ mypackage.base64decoder.decodeatom(base64decoder.java:87) @ mypackage.characterdecoder.decodebuffer(characterdecoder.java:61) @ mypackage.characterdecoder.decodebuffer(characterdecoder.java:87) @ mypackage.usersmsverifier.messagegenarator(usersmsverifier.java:91) @ mypackage.test1.main(test1.java:8)
--test1.java
package mypackage; public class test1{ public static void main(string[] args) throws exception { string testmessage = "kkkkkkkkkkkkkkk"; string contentmessage; usersmsverifier.messagegenarator(testmessage); } }
--usersmsverifier.java
package mypackage; import java.io.*; import java.security.*; import java.security.cert.*; import javax.crypto.*; import org.bouncycastle.openssl.*; import org.bouncycastle.util.encoders.*; //import android.os.*; public class usersmsverifier { static string signedmail; static { security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider()); } public static string messagegenarator(string origninalmessage) throws exception{ //load privatekey, certificate pemreader userprivatekey = new pemreader( new inputstreamreader( new fileinputstream("c://users//lara//workspace_ee//testca_server//webcontent//"+"/pkcs10priv.key"))); pemreader usercerti = new pemreader( new inputstreamreader( new fileinputstream("c://users//lara//workspace_ee//testca_server//webcontent//"+"/usercert.cer"))); keypair userprivate = (keypair)userprivatekey.readobject(); x509certificate usercert = (x509certificate)usercerti.readobject(); java.security.messagedigest md = java.security.messagedigest.getinstance("md5"); //messagedigest. //java.security.messagedigest byte[] datatosend = origninalmessage.getbytes(); //generate secretkey symmetric encryption symmetricencrypt encryptutil = new symmetricencrypt(); secretkey sendersecretkey = symmetricencrypt.getsecret(); //encrypt data using symmetric key byte[] byteciphertext = encryptutil.encryptdata(datatosend, sendersecretkey, "aes"); string strciphertext = new base64encoder().encode(byteciphertext); //get reciever's public key publickey pubkeyreceiver = usercert.getpublickey(); //encrypt secretkey receivers public key byte[] byteencryptwithpublickey = encryptutil.encryptdata(sendersecretkey.getencoded(), pubkeyreceiver,"rsa/ecb/pkcs1padding"); string strsenbyteencryptwithpublickey = new base64encoder().encode(byteencryptwithpublickey); md.update(datatosend); byte bytedatatosend[] = md.digest(); string stringdatatosend = new string(); (int i=0; < bytedatatosend.length;i++){ stringdatatosend = stringdatatosend + integer.tohexstring((int)bytedatatosend[i] & 0xff); } //message signed = encrypted secret key + data string strmsgtosign = strsenbyteencryptwithpublickey + "|" + stringdatatosend; //sign messsage //char[] password = "password".tochararray(); signature yoursign = signature.getinstance("md5withrsa"); yoursign.initsign(userprivate.getprivate()); yoursign.update(stringdatatosend.getbytes()); byte[] bytesigneddata = yoursign.sign(); //yoursign. //heoolo //return new string(hex.encode(bytesigneddata)); //values transmitted through unsecure channels ==> bytesigneddata, strmsgtosign string strrecvsigneddata = new string (bytesigneddata); string[] strrecvsigneddataarray = strmsgtosign.split("|"); int intindexofsep = strmsgtosign.indexof("|"); string strencryptwithpublickey=strmsgtosign.substring(0, intindexofsep); string strhashofdata = strmsgtosign.substring(intindexofsep+1); //decrypt symmetric key byte[] bytestrencryptwithpublickey = new base64decoder().decodebuffer(strencryptwithpublickey); byte[] bytedecryptwithprivatekey = encryptutil.decryptdata(byteencryptwithpublickey , userprivate.getprivate(), "rsa/ecb/pkcs1padding"); //decrypt data using symmetric key javax.crypto.spec.secretkeyspec secretkeyspecdecrypted = new javax.crypto.spec.secretkeyspec(bytedecryptwithprivatekey, "aes"); byte[] bytedecrypttext = encryptutil.decryptdata(byteciphertext, secretkeyspecdecrypted, "aes"); string strdecryptedtext = new string(bytedecrypttext); system.out.println("decrypted data : " + strdecryptedtext); return new string(hex.encode(bytesigneddata)); } }
--base64decoder.java
package mypackage; import java.io.outputstream; import java.io.pushbackinputstream; import java.io.printstream; public class base64decoder extends characterdecoder { protected int bytesperatom() { return (4); } protected int bytesperline() { return (72); } private final static char pem_array[] = { // 0 1 2 3 4 5 6 7 'a','b','c','d','e','f','g','h', // 0 'i','j','k','l','m','n','o','p', // 1 'q','r','s','t','u','v','w','x', // 2 'y','z','a','b','c','d','e','f', // 3 'g','h','i','j','k','l','m','n', // 4 'o','p','q','r','s','t','u','v', // 5 'w','x','y','z','0','1','2','3', // 6 '4','5','6','7','8','9','+','/' // 7 }; private final static byte pem_convert_array[] = new byte[256]; static { (int = 0; < 255; i++) { pem_convert_array[i] = -1; } (int = 0; < pem_array.length; i++) { pem_convert_array[pem_array[i]] = (byte) i; } } byte decode_buffer[] = new byte[4]; protected void decodeatom(pushbackinputstream instream, outputstream outstream, int rem) throws java.io.ioexception { int i; byte = -1, b = -1, c = -1, d = -1; if (rem < 2) { //throw new exception("base64decoder: not enough bytes atom."); } { = instream.read(); if (i == -1) { //throw new exception(); } } while (i == '\n' || == '\r'); decode_buffer[0] = (byte) i; = readfully(instream, decode_buffer, 1, rem-1); if (i == -1) { //throw new exception(); } if (rem > 3 && decode_buffer[3] == '=') { rem = 3; } if (rem > 2 && decode_buffer[2] == '=') { rem = 2; } switch (rem) { case 4: d = pem_convert_array[decode_buffer[3] & 0xff]; // nobreak case 3: c = pem_convert_array[decode_buffer[2] & 0xff]; // nobreak case 2: b = pem_convert_array[decode_buffer[1] & 0xff]; = pem_convert_array[decode_buffer[0] & 0xff]; break; } switch (rem) { case 2: outstream.write( (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)) ); break; case 3: outstream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) ); outstream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) ); break; case 4: outstream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) ); outstream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) ); outstream.write( (byte) (((c << 6) & 0xc0) | (d & 0x3f)) ); break; } return; } }
put on runtime configuration:
xss -1024m xmx-1024m
to change java heap parameters.
Comments
Post a Comment