RSA和Base64编码字节太多 [英] RSA and Base64 encoding too many bytes

查看:882
本文介绍了RSA和Base64编码字节太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现与Base64编码RSA加密。顺序是:

I am trying to implement RSA encryption with Base64 encoding. The sequence is:

String -> RSA encrypt -> Base64 encoder -> network -> Base64 decoder* -> RSA decrypt > String

我用发送的base64 EN codeD串在网络上,并把它读作另一边的字符串,毕竟Base64是文本,对吧?

I'm sending the base64 encoded string with a over the network and read it as a string on the other side, after all Base64 is text, right?

现在由于某种原因,当我去code为Base64,我得到更多的字节出比我原来发送。

Now For Some Reason when I decode the Base64, I am getting more bytes out than which I originally sent.

在发送方,我的RSA字符串是512字节。经过Base64编码的1248长(这每次都不同)。
在接收端,我的Base64 EN codeD收到字符串仍1248长,但是当我去code它,然后我突然得到936个字节。然后因为ciper.doFinal方法挂起,我不能RSA破译它。

On the sender side, my RSA string is 512 bytes. After Base64 encoding its 1248 long (this varies each time). On the receiver side, my Base64 encoded received string is still 1248 long but when I decode it then I suddenly get 936 bytes. Then I can not decipher it with RSA because the ciper.doFinal method hangs.

我假定这有什么用的todo字节到UNI code字符串转换,但我想不出在哪一步出现这种情况,我该如何解决这个问题。

I am assuming this has something todo with byte to unicode string conversion but I cannot figure out in which step this happens and how I can fix it.

发送方code:

cipher = Cipher.getInstance("RSA/NONE/OAEPWithSHA256AndMGF1Padding");
cipher.init(Cipher.ENCRYPT_MODE, getPublicKey());
byte[] base64byes = loginMessage.getBytes();
byte[] cipherData = cipher.doFinal(base64byes);
System.out.println("RSA: " + cipherData.length); //is 512 long
//4. Send to scheduler
Base64PrintWriter base64encoder = new Base64PrintWriter(out);
base64encoder.writeln(new String(cipherData)); //send string is 1248 long
base64encoder.flush();

接收方code:

Receiver side code:

System.out.println("Base 64: " + encodedChallenge.length()); //1248 long
byte[] base64Message = encodedChallenge.getBytes();
byte[] rsaEncodedMessage = Base64.decode(base64Message);
System.out.println("RSA: " + rsaEncodedMessage.length); //936 long
cipher = Cipher.getInstance("RSA/NONE/OAEPWithSHA256AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
cipherData = cipher.doFinal(rsaEncodedMessage); //hangs up
System.out.println("Ciper: " + new String(cipherData));

P.S。 Base64PrintWriter是我装修写出来之前,每个输出为Base64转换一个PrintWriter。

P.S. Base64PrintWriter is a PrintWriter that I have decorated to convert every output to base64 before writing it out.

推荐答案

有什么不对您的编码。使用,而不是碱256碱64装置的8位/ 6位的增加需要或1/3 1/4例如一滴同样的解码结果1248 * 6/8 = 936

There is something wrong with your encoding. Using base 64 instead of base 256 means an increase of 8-bit/6-bits required or 1/3 similarly decoding results in a drop of 1/4 e.g. 1248 * 6 / 8 = 936

这个问题似乎是要转换的8位数据转换成16位编码字符串之前。这需要512 * 16/6 =字节〜1365。

The problem appears to be that you are converting 8-bit data into 16-bit string before encoding. This requires 512 * 16 / 6 bytes = ~1365.

您需要一个Base64流采用字节字符,而不是/串。

You need a Base64 stream which takes bytes instead of chars/Strings.

或许,使用Base64.en code()是你需要什么?

Perhaps using Base64.encode() is what you need?

这篇关于RSA和Base64编码字节太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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