import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
public class RsaTest {
public static void main(String args[]) throws Exception {
SecureRandom sr = new SecureRandom();
KeyPairGenerator kg = KeyPairGenerator.getInstance("DSA");
kg.initialize(512, sr);
//产生新密钥对
KeyPair kp = kg.generateKeyPair();
//获得私匙
PrivateKey prk = kp.getPrivate();
//获得公钥
PublicKey puk = kp.getPublic();
//返回一个指定算法的Signature对象
Signature signature = Signature.getInstance("DSA");
signature.initSign(prk);
String str = "你好吗?我在测试RSA测试^_^你好吗?我在测试RSA测试^_^你好吗?我在测试RSA测试^_^你好吗?我在测试RSA测试^_^";
signature.update(str.getBytes());
byte[] dest = signature.sign();
System.out.println("Enc:"+new String(dest));
Signature signature2 = Signature.getInstance("DSA");
signature2.initVerify(puk);
signature2.update(str.getBytes());
System.out.println(signature2.verify(dest));
}
}
</script>