现在的位置: 首页 > 综合 > 正文

在加密和签名中使用数字证书(下)

2013年10月22日 ⁄ 综合 ⁄ 共 1891字 ⁄ 字号 评论关闭

Message.java文件
package com.robin.Signature;
public class Message {
protected byte[] body;
private byte[] signature;
Message(byte data[]) {
body = data;
}
byte[] getBody() {
return body;
}
byte[] getSignature() {
return signature;
}
void setSignature(byte data[]) {
signature = data;
}
}
SecretMessage文件
package com.robin.Signature;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class SecretMessage extends Message {
SecretMessage(byte[] data) {
super(data);
}
public void crypt(Key key) {
byte data[] = body;
Cipher cipher=null;
try {
cipher = Cipher.getInstance(key.getAlgorithm());
catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
data = cipher.doFinal(data);
catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
body = data;
}
public void decrypt(Key key) {
byte data[] = body;
Cipher cipher=null;
try {
cipher = Cipher.getInstance(key.getAlgorithm());
catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
try {
cipher.init(Cipher.DECRYPT_MODE, key);
catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
data = cipher.doFinal(data);
catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
body = data;
}
}

抱歉!评论已关闭.