java和android的加密差异
我在java应用程序中使用带有公钥的RSA加密将数据发送到服务器。当我使用java来做这件事时,一切都正常,但当我尝试用android做同样的事情时,我在服务器中遇到以下错误:java和android的加密差异,java,android,encryption,Java,Android,Encryption,我在java应用程序中使用带有公钥的RSA加密将数据发送到服务器。当我使用java来做这件事时,一切都正常,但当我尝试用android做同样的事情时,我在服务器中遇到以下错误: java.security.InvalidKeyException: Illegal key size or default parameters 这是我在android和java上加密数据的代码: import org.apache.commons.codec.binary.Base64; import javax
java.security.InvalidKeyException: Illegal key size or default parameters
这是我在android和java上加密数据的代码:
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.io.*;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.SecureRandom;
public class Encryption {
private static String base64Encode(byte[] bytes) {
return Base64.encodeBase64String(bytes);
}
private static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException {
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while (more) {
inLength = in.read(inBytes);
if (inLength == blockSize) {
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
} else more = false;
}
if (inLength > 0) outBytes = cipher.doFinal(inBytes, 0, inLength);
else outBytes = cipher.doFinal();
out.write(outBytes);
}
public static String encryptWithPublicKey(String property) {
String result = null;
KeyGenerator keygen;
SecureRandom random;
Key publicKey;
SecretKey key;
Cipher cipher;
ObjectInputStream keyIn = null;
ByteArrayOutputStream baos = null;
DataOutputStream out = null;
InputStream in = null;
try {
keygen = KeyGenerator.getInstance("AES");
random = new SecureRandom();
keygen.init(random);
key = keygen.generateKey();
keyIn = new ObjectInputStream(new FileInputStream("public.key"));
publicKey = (Key) keyIn.readObject();
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.WRAP_MODE, publicKey);
byte[] wrappedKey = cipher.wrap(key);
baos = new ByteArrayOutputStream();
out = new DataOutputStream(baos);
out.writeInt(wrappedKey.length);
out.write(wrappedKey);
in = new ByteArrayInputStream(property.getBytes("UTF-8"));
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipher);
result = base64Encode(baos.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Util.close(baos);
Util.close(out);
Util.close(in);
Util.close(keyIn);
}
return result;
}
}
当我使用java代码中的
encryptWithPublicKey
时,结果很好,但当我从android中使用它时,我出现了错误!这两个系统之间的差异是什么?这可能是因为您没有为密码
对象指定完整的算法/模式/填充
选项
例如,将“RSA”
更改为“RSA/ECB/PKCS1添加”
。和“AES”
到“AES/CBC/pkcs5p添加”
。或者任何合适的值
原因:不同的加密提供程序具有不同的填充和模式默认值。如果您只是指定算法,那么您依赖的默认值可能在不同的系统之间不兼容。我将代码更改为您所说的,错误更改为:“java.security.InvalidKeyException:非法密钥大小或默认参数”问题在这一行内:keygen.init(随机);我把它改为keygen.init(128)@S.Yavari您的第二个错误是由于您的计算机上没有安装无限强度管辖权文件造成的。谷歌搜索一下,你就会知道该应用什么补丁(如果合法的话)。