RSA BadPaddingException:数据必须从零开始 [英] RSA BadPaddingException : data must start with zero

查看:119
本文介绍了RSA BadPaddingException:数据必须从零开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现一个RSA算法的Java程序。我对着BadPaddingException:数据必须从零开始。 下面是用于加密和解密我的数据的方法:

I try to implement an RSA algorithm in a Java program. I am facing the "BadPaddingException : data must start with zero". Here are the methods used to encrypt and decrypt my data :

public byte[] encrypt(byte[] input)  throws Exception
{
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");//
    cipher.init(Cipher.ENCRYPT_MODE, this.publicKey);
    return cipher.doFinal(input);
}

public byte[] decrypt(byte[] input)  throws Exception
{   
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");///
    cipher.init(Cipher.DECRYPT_MODE, this.privateKey);
    return cipher.doFinal(input);
}

privateKey和公钥属性从文件中读取这样的:

privateKey and publicKey attributes are read from files this way :

public PrivateKey readPrivKeyFromFile(String keyFileName) throws IOException {
    PrivateKey key = null;
    try {
        FileInputStream fin = new FileInputStream(keyFileName);
        ObjectInputStream ois = new ObjectInputStream(fin);
        BigInteger m = (BigInteger) ois.readObject();
        BigInteger e = (BigInteger) ois.readObject();
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        key = fact.generatePrivate(keySpec);
        ois.close();
    }
    catch (Exception e) {
       e.printStackTrace();
    }
    return key;
}

私钥和公钥的创建是这样的:

Private key and Public key are created this way :

public void Initialize() throws Exception
{
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);
    keyPair = keygen.generateKeyPair();
    KeyFactory fact = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pub = fact.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
    RSAPrivateKeySpec priv = fact.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class);

    saveToFile("public.key", pub.getModulus(), pub.getPublicExponent());
    saveToFile("private.key", priv.getModulus(), priv.getPrivateExponent());
}

,然后保存文件:

and then saved in files :

public void saveToFile(String fileName, BigInteger mod, BigInteger exp) throws IOException {
    FileOutputStream f = new FileOutputStream(fileName);
    ObjectOutputStream oos = new ObjectOutputStream(f);
    oos.writeObject(mod);
    oos.writeObject(exp);
    oos.close();
}

我也没有想出如何这个问题从何而来。任何帮助将是AP preciate!

I can't figured out how the problem come from. Any help would be appreciate !

在此先感谢。

推荐答案

在原则上code看起来不错 - 我把一些登录,以确保您正在生成的关键是真正的一个是那么从文件中读取(你没有做一些愚蠢的类似数据生成一个新的密钥,然后试图用旧的阅读它,例如?)

In principle the code looks OK -- I would put some logging in to make sure the key you're generating is genuinely the one that is then read from the file (you're not doing something silly like generating data with a new key, then trying to read it with an old one, for example?)

这篇关于RSA BadPaddingException:数据必须从零开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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