解决方法:要在加密后产生的byte数组转成string时要在各byte之间加个标识符,我加了个空格,
然后再根据空格分隔转换回byte数组。如果不加标识符,由于byte值可能是一位到三位,无法知道某一个byte是在哪里结束。当然也可以在转成string时补0。或者转成16进制固定为两位长。

code:
public class RSATest {

   public static void main(String[] args) {
       try {
           RSATest encrypt = new RSATest();

           KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
           keyPairGen.initialize(1024);
           KeyPair keyPair = keyPairGen.generateKeyPair();
           RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
           RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

           String str = "Hello World!";
           System.out.println("String will be encrypted: " + str);
           byte[] e = encrypt.encrypt(publicKey, str.getBytes());
           String tmp1 = encrypt.bytesToString(e);
           System.out
                   .println("encrypted String's bytes, use
bytesToString() method convert bytes to string:  " + tmp1);
           String[] strArr = tmp1.split(" ");
           int len = strArr.length;
           byte[] clone = new byte[len];
           for (int i = 0; i < len; i++) {
               clone[i] = Byte.parseByte(strArr[i]);
           }
           System.out.println("convert to String, then back to bytes
again: " + encrypt.bytesToString(clone));
           byte[] d = encrypt.decrypt(privateKey, clone);
           System.out.println("decrypted String's bytes, use
bytesToString() method convert bytes to string:"
                   + encrypt.bytesToString(d));
           System.out.println("construct a string by decrypted
string's bytes: " + new String(d));
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   protected String bytesToString(byte[] encrytpByte) {
       String result = "";
       for (Byte bytes : encrytpByte) {
           result += bytes.toString() + " ";
       }
       return result;
   }

   protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
       if (publicKey != null) {
           try {
               Cipher cipher = Cipher.getInstance("RSA");
               cipher.init(Cipher.ENCRYPT_MODE, publicKey);
               return cipher.doFinal(obj);
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
       return null;
   }

   protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
       if (privateKey != null) {
           try {
               Cipher cipher = Cipher.getInstance("RSA");
               cipher.init(Cipher.DECRYPT_MODE, privateKey);
               return cipher.doFinal(obj);
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
       return null;
   }
}

运行结果:
String will be encrypted: Hello World!
encrypted String's bytes, use bytesToString() method convert bytes to
string:  79 62 -105 -47 -61 45 64 -11 -8 -120 30 31 37 -111 49 -30 88
-12 93 -77 3 39 -13 -18 68 -104 0 30 85 26 104 15 -126 -39 12 110 -84
68 -43 73 35 121 -20 -69 -84 85 -33 -123 -48 -68 -85 -106 41 -84 20
-17 87 -81 42 -67 -87 122 -2 37 74 27 103 112 58 -125 -87 -32 96 -56
65 -2 -103 -28 70 107 2 28 87 75 -8 -62 54 12 -7 -108 -123 120 -63 -83
13 -89 -21 58 -51 -84 66 25 103 -114 -14 110 80 58 74 95 -57 -73 -78
-46 56 -83 -72 -38 2 43 25 12 56 12 101 15 91 -37
convert to String, then back to bytes again: 79 62 -105 -47 -61 45 64
-11 -8 -120 30 31 37 -111 49 -30 88 -12 93 -77 3 39 -13 -18 68 -104 0
30 85 26 104 15 -126 -39 12 110 -84 68 -43 73 35 121 -20 -69 -84 85
-33 -123 -48 -68 -85 -106 41 -84 20 -17 87 -81 42 -67 -87 122 -2 37 74
27 103 112 58 -125 -87 -32 96 -56 65 -2 -103 -28 70 107 2 28 87 75 -8
-62 54 12 -7 -108 -123 120 -63 -83 13 -89 -21 58 -51 -84 66 25 103
-114 -14 110 80 58 74 95 -57 -73 -78 -46 56 -83 -72 -38 2 43 25 12 56
12 101 15 91 -37
decrypted String's bytes, use bytesToString() method convert bytes to
string:72 101 108 108 111 32 87 111 114 108 100 33
construct a string by decrypted string's bytes: Hello World!

RSA解密时javax.crypto.BadPaddingException: Data must start with zero的更多相关文章

  1. java rsa 解密报:javax.crypto.BadPaddingException: Decryption error

    Exception in thread "main" javax.crypto.BadPaddingException: Decryption error    at sun.se ...

  2. 关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法

    此文章转载自:http://www.myexception.cn/mobile/1259076.html 关于javax.crypto.BadPaddingException: Blocktype异常 ...

  3. javax.crypto.BadPaddingException: Given final block not properly padded 解决方法

    下面的 Des 加密解密代码,在加密时正常,但是在解密是抛出错误: javax.crypto.BadPaddingException: Given final block not properly p ...

  4. exception javax.crypto.BadPaddingException: Given final block not properly padded

      exception javax.crypto.BadPaddingException: Given final block not properly padded CreationTime--20 ...

  5. javax.crypto.BadPaddingException: Given final block not properly padded

    一.报错 写了一个加密方法,在Windows上运行没有问题,在Linux上运行时提示如下错误: javax.crypto.BadPaddingException: Given final block ...

  6. javax.crypto.BadPaddingException: Given final block not properly padded解决方案

    解密的时候报错: javax.crypto.BadPaddingException:   Given   final   block   not   properly   padded 该异常是在解密 ...

  7. 左右 android AES 所述机器的一部分 javax.crypto.BadPaddingException: pad block corrupted

    好多人 android 使用上述 AES 显现 javax.crypto.BadPaddingException: pad block corrupted 下面的代码发布没问题,比较自己.不解释! p ...

  8. android AES 部分机器javax.crypto.BadPaddingException: pad block corrupted

    package com.bbguoxue.poetry.util; import java.security.SecureRandom; import javax.crypto.Cipher; imp ...

  9. android 上AES解密是报错javax.crypto.BadPaddingException: pad block corrupted

    网上看到两种方法: 1.SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(key), "AES"); private sta ...

随机推荐

  1. Codefroces 784 愚人节题目(部分)

    A. Numbers Joke time limit per test 2 seconds memory limit per test 64 megabytes input standard inpu ...

  2. 微信浏览器的html5页面显示配置等问题汇集 1,禁止微信浏览器分享页面链接 (定点更新)

    公司新需求,要求从安卓IOS应用分享到微信的链接页面不允许二次转发,所以开始编程第一步,上网找资料... 网上大神就是多,不消两分钟,找到方案,很简单,上代码先: wx.config({ debug: ...

  3. Robot Framework初步使用

    第一步,新建一个Project:

  4. 使用Multiplayer Networking做一个简单的多人游戏例子-1/2

    原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 本文主要讲述了如何使用Multiplayer Networking开发多人游 ...

  5. [JWT] JWT Signature With RS256 - Learn The Advantages Compared to HS256

    The advantage of RS256 over HS256 is RS256 no longer need to share the secret key between client and ...

  6. innodb next-key lock解析

    參考http://blog.csdn.net/zbszhangbosen/article/details/7434637#reply 这里补充一些: (1)InnoDB默认加锁方式是next-key ...

  7. VC++的函数指针和回调函数 及友元函数

    什么是函数指针 函数指针是指向函数的指针变量.也就是说,它是一个指针变量,而且该指针指向一个函数. 对于指针变量来说,它的值是它指向的变量的地址.举个例子:指针变量pi是指向一个整型变量i的指针,则变 ...

  8. Nginx分发服务

    nginx配置分发tomcat服务 http://blog.csdn.net/yan_chou/article/details/53265775 http://www.cnblogs.com/deng ...

  9. 【MemSQL Start[c]UP 3.0 - Round 1 C】 Pie Rules

    [链接]h在这里写链接 [题意] 在这里写题意 [题解]     dp[i][0] 第i个位置,bob没有决策权     dp[i][1] 第i个位置,bob有决策权     dp[n][0] = 0 ...

  10. 【拆分版】 Docker-compose构建Logstash多实例,基于7.1.0

    [拆分版]Docker-compose构建Logstash多实例 写在最前 说起Logstash,这个组件并没有什么集群的概念,与其说是集群,不如说是各自去收集日志分析过滤存储到Elasticsear ...