SRA解密报错:Data must start with zero
来源:互联网 发布:如何看网络直播 编辑:程序博客网 时间:2024/05/17 04:54
项目背景:要对打印地址进行加密,用公钥加密后会乱码需要base64 decode一下,但是在解密时报错:javax.crypto.BadPaddingException: Data must start with zero
解决办法:
1.加解密时KeyFactory keyFactory = KeyFactory.getInstance("RSA");
2.将加解密的Cipher cipher = Cipher.getInstance(“RSA”)改为Cipher cipher = Cipher.getInstance(“RSA/ECB/NoPadding”)
困扰了两天的问题解决了,代码如下,希望有次问题的同学不必再走此弯路。
package resources.util.encryption; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.security.Key;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher;import org.junit.Test;public class EncryptionUtil { private static final String RSA = "RSA"; private static final String RSANOPADDING = "RSA/ECB/NoPadding"; private static final String PUBLIC_KEY_PATH = "public.key"; private static final String PRIVATE_KEY_PATH = "private.key"; private static final String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();// private static final String path = ""; @Test public void generateKey() throws Exception { //KeyPairGenerator引擎类用于产生密钥对,JDK(7)默认支持的算法有,DiffieHellman、DSA、RSA、EC KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA); generator.initialize(512); //产生密钥对 KeyPair keyPair = generator.generateKeyPair(); //获取公钥 PublicKey publicKey = keyPair.getPublic(); //获取私钥 PrivateKey privateKey = keyPair.getPrivate(); //将公钥与私钥写入文件,以备后用 writeKey(PUBLIC_KEY_PATH, publicKey); writeKey(PRIVATE_KEY_PATH, privateKey); } //公钥加密 public byte[] SRAEncrypt(String src) throws Exception { PublicKey publicKey= (PublicKey)readKey(path + PUBLIC_KEY_PATH); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance(RSA); publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Cipher cipher = Cipher.getInstance(RSANOPADDING); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] data = src.getBytes(); int blockSize = 53;//根据异常提示设的53 //根据块大小分块,不足一块的部分为一块 int blocksNum = (int)Math.ceil((1.0*data.length)/blockSize); //加密 for (int i = 0; i < blocksNum; i++) { if (i < blocksNum - 1) { cipher.doFinal(data, i * blockSize, blockSize); } else { cipher.doFinal(data, i * blockSize, data.length - i * blockSize); } } return data; } //私钥解密 public String SRADecrypt(byte[] data) throws Exception{ PrivateKey privateKey= (PrivateKey)readKey(path + PRIVATE_KEY_PATH); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance(RSA); privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Cipher cipher = Cipher.getInstance(RSANOPADDING); cipher.init(Cipher.DECRYPT_MODE, privateKey); int blockSize = 64;//根据异常提示设的64 //根据块大小分块,不足一块的部分为一块 int blocksNum = (int)Math.ceil((1.0*data.length)/blockSize); //解密 for (int i = 0; i < blocksNum; i++) { if (i < blocksNum - 1) { cipher.doFinal(data, i * blockSize, blockSize); } else { cipher.doFinal(data, i * blockSize, data.length - i * blockSize); } } return new String(data); } public void writeKey(String path, Key key) throws Exception { FileOutputStream fos = new FileOutputStream(path); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(key); oos.close(); } public Key readKey(String path) throws Exception { FileInputStream fis = new FileInputStream(path); ObjectInputStream bis = new ObjectInputStream(fis); Object object = bis.readObject(); bis.close(); return (Key)object; } @Test public void testEncryptAndDecrypt() throws Exception { Cipher cipher = Cipher.getInstance(RSA); //读取公钥,进行加密 PublicKey publicKey= (PublicKey) readKey("component/"+PUBLIC_KEY_PATH); cipher.init(Cipher.ENCRYPT_MODE, publicKey); //加密 String sendInfo = "我的明文"; byte[] results = cipher.doFinal(sendInfo.getBytes()); //读取私钥,进行解密 PrivateKey privateKey = (PrivateKey) readKey("component/"+PRIVATE_KEY_PATH); cipher.init(Cipher.DECRYPT_MODE, privateKey); //解密 byte[] deciphered = cipher.doFinal(results); //得到明文 String recvInfo = new String(deciphered); System.out.println(recvInfo); } @Test public void testSRA() throws Exception{ PublicKey publicKey= (PublicKey)readKey("component/" + PUBLIC_KEY_PATH); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance(RSA); publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Cipher cipher = Cipher.getInstance(RSA); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] result = cipher.doFinal("yuanyuan".getBytes()); PrivateKey privateKey= (PrivateKey)readKey("component/" + PRIVATE_KEY_PATH); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); cipher = Cipher.getInstance(RSANOPADDING); cipher.init(Cipher.DECRYPT_MODE, privateKey); result = cipher.doFinal(result); System.out.println(new String(result)); }}
0 0
- SRA解密报错:Data must start with zero
- Data must start with zero
- RSA解密时javax.crypto.BadPaddingException: Data must start with zero
- RSA Data must start with zero问题解决方法
- 利用java api 实现 RSA算法 解决(javax.crypto.BadPaddingException: Data must start with zero)
- 关于解决RSA非对称加密时,将密文存入数据库取出后,再对密文解密的时候抛出javax.crypto.BadPaddingException: Data must start with zero
- 报错:The resource name must start with a letter
- 报错:finished with non-zero exit value 2
- AndroidStudio 报错 finished with non-zero exit value 2
- httpclient domain must start with a dot
- urls must start with a leading slash
- java SRA加密与解密
- ##报错:DrawerLayout must be measured with MeasureSpec.EXACTLY.
- 使用Postpresql创建存储过程的时候报一个错误 ERROR: array value must start with "{" or dimension information
- Start Training YOLO with Our Own Data
- vagrant启动报错The following SSH command responded with a non-zero exit status.
- Android Studio编译大工程报错:java.exe'' finished with non-zero exit value 1
- vagrant启动报错The following SSH command responded with a non-zero exit status.解决方法
- 新手学习,web编程入门(二)——语言学习入门之道
- abstract_factory
- 排序学习_冒泡排序
- 判断一个数是否为Happy数
- Linux鸟哥私房菜学习笔记(二)
- SRA解密报错:Data must start with zero
- 网络基础题
- C语言基础—函数的理解及应用
- 韩顺平java 第32讲 swing编程实战
- 深入浅出PageRank算法
- HDU 4466 Triangle (边长a+b+c=M的三角形个数->且gcd(a,b,c)=1的三角形个数)
- 教训提莫 网易赛码网在线笔试
- Codeforces 8C
- 【最短路】bzoj2763 飞行路线