非对称加密RAS
2016-03-30 23:35
295 查看
非对称加密主要特点是一次性生成有2把钥匙,公钥加密私钥解密,反过来一样私钥加密公钥解密.
public static final String RSA = "RSA";
public static void main(String[] args) throws Exception{
String input = "非对称加密";
encrypt(input,RSA);
}
private static void encrypt(String input, String rsa) throws Exception {
//生成密匙对
KeyPair pair = KeyPairGenerator.getInstance(rsa).generateKeyPair();
//公钥
PublicKey publicKey = pair.getPublic();
//私钥
PrivateKey privateKey = pair.getPrivate();
Cipher cipher = Cipher.getInstance(rsa);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//不能处理大数据,会报错
//byte[] doFinal = cipher.doFinal(input.getBytes());
//加密一次性不能超过117
byte[] doFinal = doFinalWrite(cipher,input.getBytes(),117);
//测试下加密结果
System.out.println(new String(doFinal));
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bs = doFinalWrite(cipher, doFinal, 128);
//测试下解密结果
System.out.println(new String(bs));
}
/**
* 把字符串分批添加到数组中
* 需要调用cipher.doFinal(input, inputOffset, inputLen)
* @param cipher
* @param bytes
* @param i
* @return
* @throws Exception
*/
private static byte[] doFinalWrite(Cipher cipher, byte[] bytes, int i) throws Exception {
int offset=0;
byte[] temp;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while(bytes.length-offset>0){
if(bytes.length-offset>=i){
temp = cipher.doFinal(bytes,offset,i);
offset=offset+i;
}else{
temp = cipher.doFinal(bytes, offset,bytes.length-offset);
offset = offset+(bytes.length-offset);
}
bos.write(temp);
}
bos.close();
return bos.toByteArray();
}
public static final String RSA = "RSA";
public static void main(String[] args) throws Exception{
String input = "非对称加密";
encrypt(input,RSA);
}
private static void encrypt(String input, String rsa) throws Exception {
//生成密匙对
KeyPair pair = KeyPairGenerator.getInstance(rsa).generateKeyPair();
//公钥
PublicKey publicKey = pair.getPublic();
//私钥
PrivateKey privateKey = pair.getPrivate();
Cipher cipher = Cipher.getInstance(rsa);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//不能处理大数据,会报错
//byte[] doFinal = cipher.doFinal(input.getBytes());
//加密一次性不能超过117
byte[] doFinal = doFinalWrite(cipher,input.getBytes(),117);
//测试下加密结果
System.out.println(new String(doFinal));
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bs = doFinalWrite(cipher, doFinal, 128);
//测试下解密结果
System.out.println(new String(bs));
}
/**
* 把字符串分批添加到数组中
* 需要调用cipher.doFinal(input, inputOffset, inputLen)
* @param cipher
* @param bytes
* @param i
* @return
* @throws Exception
*/
private static byte[] doFinalWrite(Cipher cipher, byte[] bytes, int i) throws Exception {
int offset=0;
byte[] temp;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while(bytes.length-offset>0){
if(bytes.length-offset>=i){
temp = cipher.doFinal(bytes,offset,i);
offset=offset+i;
}else{
temp = cipher.doFinal(bytes, offset,bytes.length-offset);
offset = offset+(bytes.length-offset);
}
bos.write(temp);
}
bos.close();
return bos.toByteArray();
}
相关文章推荐
- OpenSSL编程之RSA
- 每 172 个活动 RSA 证书中就有一个容易受到攻击
- 用ASP编写的加密和解密类
- VBS脚本加密/解密VBS脚本(简易免杀版1.1)
- BAT加密工具 EncryBat 非编译型bat批处理加密方案与代码
- C#对称加密(AES加密)每次生成的结果都不同的实现思路和代码实例
- SQLServer 2008中的代码安全(一) 存储过程加密与安全上下文
- 实例讲解SQL Server加密功能
- C#实现对文件进行加密解密的方法
- C#实现数据包加密与解密实例详解
- C#最简单的字符串加密解密方法
- C#加密app.config中连接字符串的方法
- C#使用伪随机数实现加密用户密码的方法
- asp MD5加密方式使用建议
- C#对称加密与非对称加密实例
- 浅谈C#中Md5和Sha1两种加密方式
- 基于C#对用户密码使用MD5加密与解密
- vbs shellcode转换escape加密
- PHP加密解密字符串汇总
- PHP加密解密实例分析