我发现了一些其他问题,但没有找到原因的解释。我用Java编写了一个函数来加密数据,将其存储到一个文件中,然后再次加载文件进行解密。由于这是一种非对称加密,我有一个公钥和一个私钥,它们直接存储在代码中用于测试目的。
问题似乎是加密本身的不同,因为该功能在安卓和安装了java RE 7的windows机器上都能工作。但在JRE7系统上,不可能使用相同的密钥和恐惧来加密来自Android的数据,结果是"填充错误"。
出于测试目的,我只使用一个数据块。由于每个系统都可以进行加密/解密,我确信问题不在于密钥。最终版本将对二进制数据进行编码和解码,所以我只使用字节,不必担心字符串中的不同字符集。在我了解原因后,我将使用pureRSA对PHP中的数据进行解码。
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import android.util.Base64;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException;
import javax.crypto.ShortBufferException;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
public class test
{
public static void main (String args[]) throws FileNotFoundException, FileNotFoundException,
UnsupportedEncodingException, IOException, NoSuchAlgorithmException, InvalidKeySpecException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
ShortBufferException, NoSuchProviderException, InvalidAlgorithmParameterException {
Cipher CheckCipher = Cipher.getInstance("RSA");
KeyFactory fact = KeyFactory.getInstance("RSA");
// one Block has 320 Bytes encrypted data or up to 309 Bytes decrypted data
byte[] encryptedBuffer = new byte[320];
// public key
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger("348349717397217997016459044429876445798461822630613593827475516398929980499253048018915109820386668925158598090923917424889721436728681941788735398708226655486687058044818981687475046995966049152458371358433593409946449307673641432434645409125209056679079565878576322466469782078766479519331465759437687570449909733209874455595086928288016285095448723528960074392901021635076277130066403766286870175583368291319265676733584876043054873738027872260931903992491855282040202055048433338277681906526276056353076551640420301024861314931536846223479281788109044598118149113100016473709709779349227018363507068247312704031804719673776595496482605126340630418742698424978233098157782472641000027452801691972881854034867234891280067908910242015420737297077922221753325197819914227"), new BigInteger("65537"));
RSAPublicKey publicKey = (RSAPublicKey) fact.generatePublic(pubKeySpec);
// private key
byte[] encoded = Base64.decode("MIIF3gIBADANBgkqhkiG9w0BAQEFAASCBcgwggXEAgEAAoIBQQDNz13heSUvu7J4d+d1VJBkx6D31ZBcaF3AAA0AFYPPkkPfGF9FneTITk/ft7n1PCT9MzBRzCIFyPEkk93I4tLdP4POUDCRGC1uDBOKBkjo8hMYROt+DtK6vAafhvs/sVJVcMAxYEOZl9wG/SS9KEFU9NktM2pBfq5x747AfjVAJzZuQkL7Mfqejnby7CNgkSk0FFS4top/6EQoe+NeQj1zcDCQQ4/ze5sahc0CF7aaPsq1+aI17CCBh2PFUOvG9/YM5Hj7An2cDURChHr0OJjT8/QwqR49cSSS5P4yhaVxOQqszUAinV+r5FtLGo7iXSMnE/XYV0vpZqJHAD9cPyymcl2N7v1dKWBTmamEp7GsM6T7YnDtSJIeYt9cTi+0IFYMPAe+W93zgH2g/r+nmL/eZapgUD5Gi2ky1bHe0vwn8wIDAQABAoIBQBJ7JYYCt+kiZLNWqQ0rK9Aw8O5wWgdCQ/Di0EgKpox0KO4WpS9+LzYheiCvwd4YqYWnHBasSv3T/nt3X7oTTDYb5v0WzFWrIyE3qmWBjPiGDFrojXEDab7k9X9LwrIEfU0407lbWJapQVPLVYo63CsB1aKudEMvWgDXLy1v59475zJdduBRq5yjVGEZ/U9FmbqYfMcddTBpLf2oqlEjh5r10BWONOKx6tpUd/ALBgC34XNSUgRF6Q+D80hkay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G4ND9QuvgwJUW0ys4Xo1BX5wNWP77GasAEb9EYB1uF7Apcp8yfGax+pRuqd/t0e5HDcd4JOhwfcWHAZYQqEm6d9yxq+eE0o/junEIVEPggNgcq3xJTryhq7kpzduj8GDdbq8PjuMFouJezjYHnbcidqAgLNfofqkj9cxAn5xIg2jhMIJh8RUqFlNEp8EKulTlGVTA8sj8Lf2Z1B", Base64.DEFAULT);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encoded);
RSAPrivateKey debugprivateKey = (RSAPrivateKey) fact.generatePrivate(privateKeySpec);
// selftest (encrypt data)
String OutPutEncryption = "debug.exponent.javatest2.txt";
CheckCipher.init(Cipher.ENCRYPT_MODE, publicKey);
DataOutputStream cryptFile = new DataOutputStream(new FileOutputStream(OutPutEncryption));
byte[] decryptedBuffer = new byte[] {100, 110, 112, 99, 64}; // some data 5 bytes
CheckCipher.doFinal(decryptedBuffer, 0, 5, encryptedBuffer);
cryptFile.write(encryptedBuffer);
cryptFile.close();
// decrypt data from selftext or external source
String SourceFile = "debug.exponent.javatest2.txt";
CheckCipher.init(Cipher.DECRYPT_MODE, debugprivateKey);
DataInputStream readCryptFileDeb = new DataInputStream(new FileInputStream(SourceFile));
readCryptFileDeb.read(encryptedBuffer);
readCryptFileDeb.close();
byte[] Test = CheckCipher.doFinal(encryptedBuffer);
System.out.println(new String(Test));
}
}
不幸的是,我目前没有运行的android系统来测试它,但我想可能会选择不同的填充算法。
因此,请尝试并明确选择填充算法,如下所示:
Cipher CheckCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
请注意,在这种情况下,您还必须选择块链接模式。在这种情况下,我选择了"ECB"(=没有区块链),因为我认为这是默认的。