rsa java 加密 c 解密,使用RSA在java中加密和解密大字符串

I am trying to implement PKI. I want encrypt large string using RSA in java without using bouncy castle. Problem i am getting is Data must not be longer than 117 bytes. I tried seaching for the solution where i failed. I am newbie in this encryption. Please help me out by giving an large string as an example and explain it.

解决方案

You cannot use an RSA encryption decryption on more than approx 128 bytes at a time. You must split up the data and do it in a loop pretty much writing the bytes to String/Array as you go. If your only problem is the size of the data, you probably don't have much more to go. Just splitting the data.

A great example, possibly more complete for you, dealing with strings larger than 128 bytes: http://coding.westreicher.org/?p=23

If you need more explanation on RSA encryption in general:

The following code demonstrates how to use KeyPairGenerator to generate an RSA key-pair in Java:

// Get an instance of the RSA key generator

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");

// Generate the keys — might take sometime on slow computers

KeyPair myPair = kpg.generateKeyPair();

This will give you a KeyPair object, which holds two keys: a private and a public. In order to make use of these keys, you will need to create a Cipher object, which will be used in combination with SealedObject to encrypt the data that you are going to end over the network. Here’s how you do that:

// Get an instance of the Cipher for RSA encryption/decryption

Cipher c = Cipher.getInstance("RSA");

// Initiate the Cipher, telling it that it is going to Encrypt, giving it the public key

c.init(Cipher.ENCRYPT_MODE, myPair.getPublic());

After initializing the Cipher, we’re ready to encrypt the data. Since after encryption the resulting data will not make much sense if you see them “naked”, we have to encapsulate them in another Object. Java provides this, by the SealedObject class. SealedObjects are containers for encrypted objects, which encrypt and decrypt their contents with the help of a Cipher object.

The following example shows how to create and encrypt the contents of a SealedObject:

// Create a secret message

String myMessage = new String("Secret Message");

// Encrypt that message using a new SealedObject and the Cipher we created before

SealedObject myEncryptedMessage= new SealedObject( myMessage, c);

The resulting object can be sent over the network without fear, since it is encrypted. The only one who can decrypt and get the data, is the one who holds the private key. Normally, this should be the server. In order to decrypt the message, we’ll need to re-initialize the Cipher object, but this time with a different mode, decrypt, and use the private key instead of the public key.

This is how you do this in Java:

// Get an instance of the Cipher for RSA encryption/decryption

Cipher dec = Cipher.getInstance("RSA");

// Initiate the Cipher, telling it that it is going to Decrypt, giving it the private key

dec.init(Cipher.DECRYPT_MODE, myPair.getPrivate());

Now that the Cipher is ready to decrypt, we must tell the SealedObject to decrypt the held data.

// Tell the SealedObject we created before to decrypt the data and return it

String message = (String) myEncryptedMessage.getObject(dec);

System.out.println("foo = "+message);

Beware when using the getObject method, since it returns an instance of an Object (even if it is actually an instance of String), and not an instance of the Class that it was before encryption, so you’ll have to cast it to its prior form.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值