【问题标题】:I'm encrypt to large string(8KByte) in Java我在 Java 中加密到大字符串(8KByte)
【发布时间】:2014-04-07 04:41:59
【问题描述】:

我想测量在 java 中加密数据所需的时间。

我已成功加密小数据。

但是,我不知道如何加密大数据。

以下是我的来源。

package com.exam.encrypttest;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView tv1;
Button but1;
EditText edit1;
String type = "AES";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edit1 = (EditText) findViewById(R.id.edit01);
    edit1.setText("ABCDEFGH");
    tv1 = (TextView) findViewById(R.id.text01);
    but1 = (Button) findViewById(R.id.button01);
    but1.setOnClickListener(new ButtonClick());
}

class ButtonClick implements View.OnClickListener {
    public void onClick(View v) {
        int sel = v.getId();
        switch (sel) {
        case R.id.button01:
            Encryption(edit1.getText().toString(), type);
            break;
        }
    }

    private void Encryption(String text, String key) {
        long time1 = System.currentTimeMillis();
        Cipher cipher;
        SecretKeySpec skeySpec;
        KeyGenerator kgen;

        try {
            kgen = KeyGenerator.getInstance(key);
            SecretKey skey = kgen.generateKey();
            kgen.init(128);
            byte[] raw = skey.getEncoded();

            skeySpec = new SecretKeySpec(raw, key);
            cipher = Cipher.getInstance(key);

            byte[] encrypted = Encrypt(text, skeySpec, cipher);
            String sendtext = Base64.encode(encrypted);

            long time2 = System.currentTimeMillis();


            byte[] abc = Base64.decode(sendtext);
            byte[] decrypted = Decrypt(abc, skeySpec, cipher);
            long time3 = System.currentTimeMillis();            
            tv1.setText("Encrypt Time(ms) : "+(time2-time1));



        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ArrayIndexOutOfBoundsException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public byte[] Encrypt(String data, SecretKeySpec keySpec,
            Cipher cipher) throws Exception, ArrayIndexOutOfBoundsException {

        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encrypted = cipher.doFinal(data.getBytes());
        return encrypted;
    }

    public byte[] Decrypt(byte[] encrypted_data,
            SecretKeySpec keySpec, Cipher cipher) throws Exception,
            ArrayIndexOutOfBoundsException {
        cipher.init(Cipher.DECRYPT_MODE, keySpec); 
        byte[] decrypted = cipher.doFinal(encrypted_data);
        return decrypted;
    }
}

}

它适用于小数据。 但它不适用于大数据。

请给我一些建议。

【问题讨论】:

  • 不工作是什么意思?
  • 我的源无法加密文本...

标签: java encryption large-data


【解决方案1】:

多次调用file:///C:/java/jdk1.7.0/docs/api/javax/crypto/Cipher.html#update(java.nio.ByteBuffer,%20java.nio.ByteBuffer) 并调整缓冲区大小。最后对doFinal执行一次调用。

您需要在执行任何测试之前定义一组清晰的参数,否则您将比较不同生成的数字。另外不要忘记 Java 依赖于 JIT,因此对于长时间运行的应用程序,您可能需要在测量之前执行多次迭代(因为在您的应用程序运行时会进行 JIT 优化)。

请注意,例如Guava 有一个StopWatch 类,它比System.currentTimeMillis() 更易于使用。

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2018-08-30
    • 2013-09-04
    • 2012-07-29
    • 1970-01-01
    相关资源
    最近更新 更多