0

我想创建用 java 编写的消息签名和验证软件。

因此,我决定使用互联网上的一些代码。

虽然代码没有语法错误,但它显示 java 堆空间错误。

但是,错误消息如下所示

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.io.ByteArrayOutputStream.write(Unknown Source)
at myPackage.BASE64Decoder.decodeAtom(BASE64Decoder.java:87)
at myPackage.CharacterDecoder.decodeBuffer(CharacterDecoder.java:61)
at myPackage.CharacterDecoder.decodeBuffer(CharacterDecoder.java:87)
at myPackage.UserSMSVerifier.messageGenarator(UserSMSVerifier.java:91)
at 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 a SecretKey for Symmetric Encryption
        SymmetricEncrypt encryptUtil = new SymmetricEncrypt();
        SecretKey senderSecretKey = SymmetricEncrypt.getSecret();

        //encrypt the data using a 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 the SecretKey with the 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();
        for (int i=0; i < bytedataTosend.length;i++){
            stringDataTosend = stringDataTosend + Integer.toHexString((int)bytedataTosend[i] & 0xFF);       }


        //Message to be Signed = Encrypted Secret Key + data
        String strMsgToSign = strSenbyteEncryptWithPublicKey + "|" + stringDataTosend;
        //sign the 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 to get the symmetric key
        byte[] bytestrEncryptWithPublicKey = new BASE64Decoder().decodeBuffer(strEncryptWithPublicKey);
        byte[] byteDecryptWithPrivateKey = encryptUtil.decryptData(byteEncryptWithPublicKey , userPrivate.getPrivate(), "RSA/ECB/PKCS1Padding");

        //decrypt the data using the 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 is : " + 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 {
        for (int i = 0; i < 255; i++) {
            pem_convert_array[i] = -1;
        }
        for (int i = 0; i < 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    a = -1, b = -1, c = -1, d = -1;
       if (rem < 2) {
          //throw new Exception("BASE64Decoder: Not enough bytes for an atom.");
       }
       do {
           i = inStream.read();
           if (i == -1) {
              //throw new Exception();
           }
      } while (i == '\n' || i == '\r');
       decode_buffer[0] = (byte) i;
       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];
           a = 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;
   }
}
4

1 回答 1

2

把它放在你的运行时配置中:

Xss -1024m Xmx-1024m

更改您的 java 堆参数。

于 2013-05-19T15:55:11.947 回答