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
原创粉丝点击