解决方法:要在加密后产生的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. ftp实现图片上传,文件也类似

    本来用得是easyui得控件 点击按钮实现选择图片得 ,但是老板非得要双击图片框实现上传代码....做个简单得记录 前端代码: 首先,<form>表单要加上 enctype="m ...

  2. 数组-reduce方法

    转自: https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/139 实现 convert 方法,把原始 list ...

  3. CList 点击表头排序 (3)两种排序的第二种

    在头两篇中介绍了CListCtrl::SortItems() 方法的使用和其中的一个排序方法,这篇介绍另一种方法 CList 点击表头排序 (1)SortItems函数 CList 点击表头排序 (2 ...

  4. CISP/CISA 每日一题 12

    CISA 每日一题(答) 支付系统模式有哪些: 电子现金模式:支付者不必在线,无条件不可追溯性 电子支票模式:支付者不必在线,涉及个人隐私 电子转帐模式:收款人不必在线 图象处理中,应该有适当的___ ...

  5. theme-windowAnimationStyle 动画设置

    对于windowAnimationStyle 的引用,目前自己发现的有两处 1.就是直接在Theme 中引用的,如下 <style name="Theme.Funui" pa ...

  6. vue使用jsonp

    axios不支持jsonp,所以需使用其他插件:vue-jsonp npm i vue-jsonp -S 然后在 src/main.js : import Vue from 'vue' import ...

  7. EventWaitHandle

    在查资料的过程中,我突然想到一个类:EventWaitHandle,也就是本文的主角. 这个类通过在线程之间设置信号量,可以非常方便的控制线程运行的顺序.具体代码如下: 首先全局申明: EventWa ...

  8. JPA 对象关系映射总结(一)---persistence.xml 文件配置要点

    1. <property name="hibernate.hbm2ddl.auto" value="update"/>,这里表示的 功能是: 自动创 ...

  9. cocos2d-x 3.x游戏开发学习笔记(1)--mac下配置cocos2d-x 3.x开发环境

    打开用户文件夹下.bash_profile文件,配置环境 vim ~/.bash_profile //按键i,进行插入编辑(假设输错d进行删除一行) 环境配置过程例如以下: 1.首先配置下androi ...

  10. Javascript和jquery事件--事件监听器

    之前看完了js和jq的冒泡捕获和事件对象event,这里看看同时使用js和jq后我最容易混淆的监听器的绑定. (1) js的监听器绑定解绑 绑定监听器有两种方式: a.on-事件type,比如oncl ...