⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testaes.java

📁 j2me 和j2se两上不同平台的加解密
💻 JAVA
字号:
package test;

import java.util.Date;

import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;

import crypto.Crypt;

public class TestAES {

    /**
     * @param args
     */
    public static void main(String[] args) {
        byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
                0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
      
        byte[] out = null;
        byte[] outs = null;
        boolean mode;
        String content ="TEST12345673321422342413243489324324320ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        System.out.println("Original content:");
        System.out.println(content);
        byte[] in = content.getBytes();
        Crypt c= new Crypt();
        out = c.crypt(true,in, keybytes);
        System.out.println(new String(Hex.encode(out)));
       
        outs = c.crypt(false, out, keybytes);
        System.out.println(new String(outs));
        
        try {
          
        	long startTime = (new Date()).getTime();      	
        	BufferedBlockCipher engine = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
            engine.init(true, new KeyParameter(keybytes));
            byte[] enc = new byte[engine.getOutputSize(content.getBytes().length)];
            int size1 = engine.processBytes(content.getBytes(), 0, content.getBytes().length, enc, 0);
            int size2 = engine.doFinal(enc, size1);
            
            
            
            
            System.out.println("size2 ="+size2);
            byte[] encryptedContent =new byte[size1+size2];
            System.arraycopy(enc, 0, encryptedContent, 0, encryptedContent.length);
            long endTime = (new Date().getTime());
			System.out.println(startTime - endTime);

            System.out.println("Encrypted Content:");
            System.out.println(new String(Hex.encode(encryptedContent)));
            
            
           

            engine.init(false, new KeyParameter(keybytes));
            
            byte[] dec = new byte[engine.getOutputSize(encryptedContent.length)];
            size1 = engine.processBytes(encryptedContent, 0, encryptedContent.length, dec, 0);
            size2 = engine.doFinal(dec, size1);
            System.out.println("size2 ="+size2);
            byte[] decryptedContent =new byte[size1+size2];
            System.arraycopy(dec, 0, decryptedContent, 0, decryptedContent.length);
            System.out.println("Decrypted Content:");
            System.out.println(new String(decryptedContent));

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -