计算机二级辅导:javaRSA加解密计算机等级考试
文章作者 100test 发表时间 2010:01:01 12:28:31
来源 100Test.Com百考试题网
import java.security.Key.
import java.security.KeyFactory.
import java.security.KeyPair.
import java.security.KeyPairGenerator.
import java.security.PrivateKey.
import java.security.PublicKey.
import java.security.interfaces.RSAPrivateKey.
import java.security.interfaces.RSAPublicKey.
import java.security.spec.PKCS8EncodedKeySpec.
import java.security.spec.X509EncodedKeySpec.
import javax.crypto.Cipher.
import sun.misc.BASE64Decoder.
import sun.misc.BASE64Encoder.
public class RSACoder {
/**
* 得到公钥
* @param key 密钥字符串(经过base64编码)
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes.
keyBytes = (new BASE64Decoder()).decodeBuffer(key).
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes).
KeyFactory keyFactory = KeyFactory.getInstance("RSA").
PublicKey publicKey = keyFactory.generatePublic(keySpec).
return publicKey.
}