使用RSA在java中加密和解密大字符串 [英] Encrypt and decrypt large string in java using RSA

查看:839
本文介绍了使用RSA在java中加密和解密大字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实施PKI。我想使用RSA加密大字符串,而不使用弹性城堡。问题我得到的是数据不能超过117个字节。我试图为我失败的解决方案进行搜索。我是这个加密的新手。请帮助我提供一个大字符串作为例子,并解释一下。

解决方案

您不能使用RSA加密解密更多一次约128个字节。您必须分割数据,并在循环中执行它几乎写入String / Array的字节。如果你唯一的问题是数据的大小,你可能没有太多的事要去。只需分割数据。



一个很好的例子,可能更完整,处理128个字节的字符串: http://coding.westreicher.org/?p=23



如果您需要更多关于RSA加密的解释:



以下代码演示了如何使用KeyPairGenerator在Java中生成RSA密钥对: p>

  //获取RSA密钥生成器的实例
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
//生成键 - 可能需要在电脑上的某个时间
KeyPair myPair = kpg.generateKeyPair();

这将给你一个KeyPair对象,它保存两个键:私人和公共。为了使用这些密钥,您将需要创建一个Cipher对象,该对象将与SealedObject组合使用,以加密要通过网络终止的数据。以下是这样做:

  //获取一个用于RSA加密/解密的密码的实例
密码c = Cipher.getInstance( RSA);
//启动密码,告诉它要加密,给它公钥
c.init(Cipher.ENCRYPT_MODE,myPair.getPublic());

初始化密码后,我们准备加密数据。因为加密之后,如果你看到它们是裸的,那么结果数据就没有多大意义了,我们必须将它们封装在另一个对象中。 Java通过SealedObject类提供了这一点。 SealedObjects是加密对象的容器,通过Cipher对象对其内容进行加密和解密。



以下示例说明如何创建和加密SealedObject的内容:

  //创建一个秘密消息
String myMessage = new String(Secret Message);
//使用新的SealedObject和我们之前创建的密码加密该消息
SealedObject myEncryptedMessage = new SealedObject(myMessage,c);

生成的对象可以通过网络发送,而不用担心,因为它被加密。唯一可以解密和获取数据的人是持有私钥的人。通常这应该是服务器。为了解密该消息,我们需要重新初始化Cipher对象,但这次使用不同的模式,解密,并使用私钥而不是公钥。



这是你在Java中如何做到这一点:

  //获取一个用于RSA加密/解密
密码dec = Cipher.getInstance(RSA);
//启动密码,告诉它要解密,给它私钥
dec.init(Cipher.DECRYPT_MODE,myPair.getPrivate());

现在,密码已经可以解密,我们必须告诉SealedObject解密持有的数据。 / p>

  //告诉我们之前创建的SealedObject来解密数据并返回它
String message =(String)myEncryptedMessage。的getObject(分解);
System.out.println(foo =+ message);

当使用getObject方法时,请注意,因为它返回一个Object的实例(即使实际上一个String的一个实例),而不是加密之前的Class的一个实例,所以你必须将其转换为其先前的形式。



以上是来自: http://andreas.louca.org/ 2008/03/20 / java-rsa-encryption-an-example /


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.

The above is from: http://andreas.louca.org/2008/03/20/java-rsa-encryption-an-example/

这篇关于使用RSA在java中加密和解密大字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆