String content = "";
for(int i=1;i<100;i++)content += i + ") Easy to say, hard to do.\n";
/*1、利用公钥加密至文件*/
{
/*1)根据公钥反向构造PublickKey对象,调用cipher的init方法时使用*/
String KEY_PUBLIC = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIv38xXk06As11OCWq0IPQoQAmz20ZiE8T5KeaMTUbhzUkGNTAQygApua71R/INeEDNsyyQS4PT6EaWTjJop2rcCAwEAAQ==";
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(new BASE64Decoder().decodeBuffer(KEY_PUBLIC));
PublicKey key = KeyFactory.getInstance("RSA").generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA");//RSA/ECB/PKCS1PADDING
cipher.init(Cipher.ENCRYPT_MODE, key);
/*
* 直接加密,因为内容太长将有异常,需要分块加密
* javax.crypto.IllegalBlockSizeException: Data must not be longer than 53 bytes
* cipher.doFinal(content.getBytes())