RSA加密组件,DSA数字签名组件,应用代码,以下代码将RSA和DSA融合在了一起,有注释,可以方便大家在做一些安全程序时的加密及验证组件。JAVA提供了大数类可以方便我们自己实现RSA密码算法,不过做应用的话就用JDK里面提供的,方便。话说RSA算法应用就比较广泛了,SSH,网银,数字证书那些咯。我抽屉里貌似就两个那方面玩意,U盾和中行的公钥生成器(它上面是那么写的)。

 
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Map;
import java.util.HashMap;
import javax.crypto.Cipher;
public class RSADriver{
/*
*相应的DSA数字签名算法只需修改算法名:DSA,SHA1withDSA(Java6支持),SHA224,SHA256,SHA348,SHA512withDSA(BC支持)
*DSA只用于签名,不能用于加密数据!
*/
private static final String KEY_ALGORITHM="RSA";
/*
*RSA数字签名算法:
*Java6 支持MD2withRSA,MD5withRSA,SHA1withRSA,密钥长度默认1024,新的Java还没体验。
*/
private static final String SIGNATURE_ALGORITHM="MD5withRSA";
private static final String PUB_KEY="RSAPublicKey";
private static final String PRI_KEY="RSAPrivateKey";
/*
*密钥:长度必须为64的倍数,范围在512~65536之间。
*/
private static final int KEY_SIZE=512;
public static Map<String,Object> initKey()throws Exception{
KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(KEY_SIZE);
KeyPair keypair=keyPairGen.generateKeyPair();
RSAPublicKey publickey=(RSAPublicKey)keypair.getPublic();
RSAPrivateKey privatekey=(RSAPrivateKey)keypair.getPrivate();
Map<String,Object> keymap=new HashMap<String,Object>(2);
keymap.put(PUB_KEY,publickey);
keymap.put(PRI_KEY,privatekey);
return keymap;
}
public static byte[] getPublicKeyEncoded(Map<String,Object> map)throws Exception{
Key key=(Key)map.get(PUB_KEY);
return key.getEncoded();
}
public static byte[] getPrivateKeyEncoded(Map<String,Object> map)throws Exception{
Key key=(Key)map.get(PRI_KEY);
return key.getEncoded();
}
public static PublicKey PubKey(byte[] k)throws Exception{
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(k);
KeyFactory keyfactory=KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey publickey=keyfactory.generatePublic(x509KeySpec);
return publickey;
}
public static PrivateKey PriKey(byte[] k)throws Exception{
PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(k);
KeyFactory keyfactory=KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privatekey=keyfactory.generatePrivate(pkcs8KeySpec);
return privatekey;
}
public static byte[] decryptByPublicKey(byte[] data,byte[] key)throws Exception{
Cipher cipher=Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE,PubKey(key));
return cipher.doFinal(data);
}
public static byte[] decryptByPrivateKey(byte[] data,byte[] key)throws Exception{
Cipher cipher=Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE,PriKey(key));
return cipher.doFinal(data);
}
public static byte[] encryptByPublicKey(byte[] data,byte[] key)throws Exception{
Cipher cipher=Cipher.getInstance(KEY_ALGORITHM);
                cipher.init(Cipher.ENCRYPT_MODE,PubKey(key));
                return cipher.doFinal(data);
}
public static byte[] encryptByPrivateKey(byte[] data,byte[] key)throws Exception{
                Cipher cipher=Cipher.getInstance(KEY_ALGORITHM);
                cipher.init(Cipher.ENCRYPT_MODE,PriKey(key));
                return cipher.doFinal(data);
        }
public static byte[] sign(byte[] data,byte[] key)throws Exception{
Signature sig=Signature.getInstance(SIGNATURE_ALGORITHM);
sig.initSign(PriKey(key));//使用私钥对象进行签名
sig.update(data);
return sig.sign();
}
public static boolean verify(byte[] data,byte[] key,byte[] sign)throws Exception{
Signature sig=Signature.getInstance(SIGNATURE_ALGORITHM);
sig.initVerify(PubKey(key));//使用公钥对象进行验证
sig.update(data);
return sig.verify(sign);
}
}