java 定长加密_使用RSA(Java)加密长字符串

我在使用

Java时遇到的RSA应用程序出现问题.

我必须从文件中读取一个字符串,对其进行加密,然后将加密的字符串保存在一个新文件中.

我的RSA密钥长度为1024位.

问题所在的代码部分如下:

readBytes = in.read(bytesToBeEncoded, 0, bytesToBeEncoded.length);

while(readBytes != -1){

encodedContent = ciph.update(bytesToBeEncoded, 0, readBytes);

out.write(encodedContent);

bytesToBeEncoded= new byte[(KEY_SIZE/8)-11];

readBytes = in.read(bytesToBeEncoded, 0, bytesToBeEncoded.length);

}

encodedContent = ciph.doFinal();

out.write(encodedContent);

变量定义如下:

byte[] bytesToBeEncoded = new byte[(KEY_SIZE/8)-11];

FileInputStream in = new FileInputStream(new File(file1));

FileOutputStream out = new FileOutputStream(new File(file2));

int readBytes;

关键是当我加密一个小于117字节的字符串时,它完美地工作(加密然后解密),但是当大小更大时,应用程序抛出此异常:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes

抛出:

encodedContent = ciph.doFinal();

我不知道问题出在哪里以及我必须做什么.

谁能帮助我?谢谢.

抱歉我的英语.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值