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

📄 bcciphermidlet.java

📁 J2ME核心类及MIDlet类 MIDP用户界面对象 图形处理及低级事件处理 多线程编程 I/O及网络编程 数据库RMS编程 浮点数编程 多媒体及GAME API编程 安全、加密及
💻 JAVA
字号:
import java.io.*;
import java.util.Random;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.bouncycastle.crypto.*;

public class BCCipherMIDlet extends MIDlet 
{
	private Display mDisplay;
	private Form mForm;
	private TextField mTextField;
	private Random mRandom;

	public void startApp() 
	{
		mDisplay = Display.getDisplay(this);
		mRandom = new Random(System.currentTimeMillis());
		if (mForm == null) 
		{
			mForm = new Form("Login");
			mTextField = new TextField("原文", "TangDashi", 32, 0);
			mForm.append(mTextField);
			mForm.addCommand(new Command("退出", Command.EXIT, 0));
			mForm.addCommand(new Command("加解密", Command.SCREEN, 0));
			mForm.setCommandListener(new CommandListener() {
				public void commandAction(Command c, Displayable s) {
					if (c.getCommandType() == Command.EXIT) 
						notifyDestroyed();
					else 
					{
						try{
							test();
						}catch(Exception e ){e.printStackTrace();}
					}
				}
			});
		}
		mDisplay.setCurrent(mForm);
	}

	private void test() throws CryptoException 
	{
		// 生成Cipher对象
		String password = "1234abcd";
		BCCipher cip = new BCCipher( password );

		// 取得信息
		String txt0 = mTextField.getString();
		byte[] txtBytes0 = txt0.getBytes();
		
		// 测试加密
		byte[] txtBytes1 = cip.encrypt( txtBytes0 );
		String txt1 = new String(
			HexCodec.bytesToHex( txtBytes1 ) );

		// 测试解密
		byte[] tmp = HexCodec.hexToBytes( txt1 );
		byte[] txtBytes2 = cip.decrypt( tmp );
		//txtBytes2 = cip.decrypt( txtBytes1 );

		String txt2 = new String( txtBytes2 );

		// 显示
		Alert alert = new Alert( "加密及解密",
			"原文为:" + txt0 + "\n" +
			"加密为:" + txt1 + "\n" +
			"解密为:" + txt2,
			null, AlertType.INFO );
		alert.setTimeout( Alert.FOREVER );
		mDisplay.setCurrent( alert, mForm );

	}

	private byte[] getBytes(long x) 
	{
		byte[] bytes = new byte[8];
		for (int i = 0; i < 8; i++)
			bytes[i] = (byte)(x >> ((7 - i) * 8));
		return bytes;
	}

	public void pauseApp() {}
	public void destroyApp(boolean unconditional) {}
}


class HexCodec 
{
	private static char[] kDigits = 
		{
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'a', 'b', 'c', 'd', 'e', 'f'
		};
	public static char[] bytesToHex(byte[] raw) 
	{
		int length = raw.length;
		char[] hex = new char[length * 2];
		for (int i = 0; i < length; i++) 
		{
			int value = (raw[i] + 256) % 256;
			int highIndex = value >> 4;
			int lowIndex = value & 0x0f;
			hex[i * 2 + 0] = kDigits[highIndex];
			hex[i * 2 + 1] = kDigits[lowIndex];
		}
		return hex;
	}
	public static byte[] hexToBytes(char[] hex) 
	{
		int length = hex.length / 2;
		byte[] raw = new byte[length];
		for (int i = 0; i < length; i++) 
		{
			int high = Character.digit(hex[i * 2], 16);
			int low = Character.digit(hex[i * 2 + 1], 16);
			int value = (high << 4) | low;
			if (value > 127) value -= 256;
			raw[i] = (byte)value;
		}
		return raw;
	}
	public static byte[] hexToBytes(String hex) 
	{
		return hexToBytes(hex.toCharArray());
	}
}

⌨️ 快捷键说明

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