import java.awt.AWTException;
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、大邑縣網(wǎng)站維護(hù)、網(wǎng)站推廣。
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CharTest extends JFrame implements KeyListener {
JPanel pnlMain;
JTextField txtfile;
/**
* @param args
*/
public CharTest(){
pnlMain = new JPanel();
this.getContentPane().add(pnlMain);
txtfile = new JTextField(10);
pnlMain.add(txtfile);
txtfile.requestFocus();
txtfile.addKeyListener(this);
}
public void keyPressed(KeyEvent e) {
System.out.println(""+e.getKeyCode());
}
public void keyTyped(KeyEvent e) {
System.out.println(""+e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
System.out.println(""+e.getKeyCode());
}
public static void main(String[] args) {
try {
Robot robot = new Robot();
JFrame f = new CharTest();
f.setSize(200, 300);
f.setVisible(true);
robot.keyPress(KeyEvent.VK_SHIFT );
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_SHIFT );
} catch (AWTException e) {
// TODO Auto-generated catch block
System.out.println("here");
e.printStackTrace();
}
}
}
這段代碼好像就是一段讀取數(shù)據(jù)的代碼。沒有看出有加密的過程啊?
這段代碼做的事情就是
1.讀包長 passLength = in.read() ^ Server.SEED
2.從流in中讀passLength長度的數(shù)據(jù)內(nèi)容。
唯一一個(gè)特別的地方就是
passLength = in.read() ^ Server.SEED
也僅僅只能把長度讀出來進(jìn)行了一下異或。
寫包的時(shí)候也同樣進(jìn)行一樣異或就可以了。
沒看明白你指的是什么key?加密的密鑰向量嗎?
如果是想用DES加密解密的話,可以使用下面的(加上注釋后不讓發(fā),所以把注釋去掉了,擦,還不讓寫“加密解密”這樣的字)
import?javax.crypto.Cipher;
import?javax.crypto.SecretKey;
import?javax.crypto.SecretKeyFactory;
import?javax.crypto.spec.DESKeySpec;
import?javax.crypto.spec.IvParameterSpec;
public?class?DES?{
private?static?final?String?ENCODING?=?"utf-8";
private?byte[]?key,iv;
private?void?setKeyIv(String?key){
try?{
this.key?=?key.getBytes(ENCODING);
//偏移量,可以與密鑰相同
this.iv?=?new?String("bing1987").getBytes(ENCODING);
}
catch?(Exception?e)?{
e.printStackTrace();
}
}
//初始構(gòu)造方法,設(shè)置默認(rèn)密鑰
public?DES()?{
setKeyIv("5a8f9e3l");
}
public?DES(String?key)?{
setKeyIv(key);
}
public?void?main(String?content)?throws?Exception?{
System.out.println("加前:"?+?content);
String?encrypt?=?encrypt(content);
System.out.println("加后:"?+?encrypt);
String?decrypt?=?decrypt(encrypt);
System.out.println("解后:"?+?decrypt);
}
//加mi方法:instr?待加字串?return?加后的密文
public?String?encrypt(String?instr)?throws?Exception{
byte[]?bytes?=?desPublic(Cipher.ENCRYPT_MODE,?instr.getBytes(ENCODING));
return?Tools.encryptBASE64(bytes);
}
//解mi方法:encryptStr?待解字串??return?解后的字串
public?String?decrypt(String?encryptStr)?throws?Exception?{
byte[]?bytes?=?desPublic(Cipher.DECRYPT_MODE,?Tools.decryptBASE64(encryptStr));
return?new?String(bytes);
}
//mode????加或解表示值
//context?待加或解的字節(jié)數(shù)組
//return?加或解后的字節(jié)數(shù)組
private?byte[]?desPublic(int?mode,?byte[]?context)?throws?Exception?
Cipher?cipher?=?Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec?desKeySpec?=?new?DESKeySpec(this.key);
SecretKeyFactory?keyFactory?=?SecretKeyFactory.getInstance("DES");
SecretKey?secretKey?=?keyFactory.generateSecret(desKeySpec);
IvParameterSpec?iv?=?new?IvParameterSpec(this.iv);
cipher.init(mode,?secretKey,?iv);
return?cipher.doFinal(context);
}
}
上面用到的tools工具
import?sun.misc.BASE64Decoder;
import?sun.misc.BASE64Encoder;
public?class?Tools?{
public?static?byte[]?decryptBASE64(String?key)?throws?Exception?{
return?(new?BASE64Decoder()).decodeBuffer(key);
}
public?static?String?encryptBASE64(byte[]?key)?throws?Exception?{
return?(new?BASE64Encoder()).encodeBuffer(key);
}
}
你也可以把decryptBASE64和encryptBASE64方法合并到DES類里
容器中的key嗎就是,key-value這兩個(gè)東東在容器中,key就是你對象的一個(gè)命名,value就是對象本身。相當(dāng)與字典一樣從key找value。