我正在为我的Android应用程序编写服务器,他们必须加密通信。 我为数据选择了AES加密,为AES Key选择了RSA。 Android应用程序使用SpongyCastle,因此要拥有一个正常工作的服务器,我需要一个(几乎)相等的提供程序来使用其他通信伙伴的RSA公钥。 我尝试使用bouncycastle,但我找不到解决此问题的工作方法。
服务器:Eclipse /客户端:Android Studio
所描述的解决方案here不起作用,已经尝试过了。
//包和文件路径已缩短
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import java.security.*;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
public class Cryption {
private Key publicKey;
private PrivateKey privateKey;
private String AES = "AES";
private String RSA = "RSA";
public Cryption() {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyPair keyPair = null;
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA, "BC");
keyPairGenerator.initialize(1024);
keyPair = keyPairGenerator.generateKeyPair();
} catch (Exception e) {
e.printStackTrace();
}
publicKey = keyPair.getPublic();
privateKey = keyPair.getPrivate();
}
public byte[] getPublicKeyBytes() {
return publicKey.getEncoded();
}
////////////ENCRYPT
public EncryptedData encrypt(Object o, byte[] keyRSA) {
return encrypt(Utils.serialize(o), keyRSA);
}
public EncryptedData encrypt(byte[] bytes, byte[] keyRSA) {
try {
//get AES Random Key
KeyGenerator keygen = KeyGenerator.getInstance(AES, "BC");
keygen.init(128);
Key aesKey= keygen.generateKey();
byte[] aesKeyByte = aesKey.getEncoded();
//AES encryption
byte[] dataEncyptedAES = null;
Cipher aescipher = Cipher.getInstance(AES, "BC");
aescipher.init(Cipher.ENCRYPT_MODE, aesKey);
dataEncyptedAES = aescipher.doFinal(bytes);
//encode AES Key
byte[] encodedAESkey = encodeRSA(aesKeyByte, keyRSA);
return new EncryptedData(encodedAESkey, dataEncyptedAES);
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
private byte[] encodeRSA(byte[] data, byte[] keyRSA) {
try {
AsymmetricKeyParameter publicKey = PublicKeyFactory.createKey(keyRSA);
AsymmetricBlockCipher e = new RSAEngine();
e = new org.bouncycastle.crypto.encodings.PKCS1Encoding(e);
e.init(true, publicKey);
List<Byte> value = new ArrayList<Byte>();
int i = 0;
int len = e.getInputBlockSize();
while(i<data.length) {
if(i+len > data.length) len = data.length - i;
byte[] hexEncodedCipher = e.processBlock(data, i ,len);
for(Byte b : hexEncodedCipher) {
value.add(b);
}
i+=e.getInputBlockSize();
}
return Utils.convert(value.toArray(new Byte[value.size()]));
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
///////////////////DECRYPT
public Object decryptToObject(EncryptedData encryptedData) {
return Utils.deserialize(decrypt(encryptedData));
}
private byte[] decrypt(EncryptedData encryptedData) {
if(encryptedData == null) System.out.println("ENCRYPTED DATA == NULL");
try {
//decode AES key
byte[] decodedAESKey = decryptRSA(encryptedData.getEncryptedAESKey());
//decrypt data
byte[] decodedBytes = null;
Cipher cipherData = Cipher.getInstance(AES, "BC");
cipherData.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decodedAESKey, 0, decodedAESKey.length, AES));
decodedBytes = cipherData.doFinal(encryptedData.getDataEncryptedAES());
return decodedBytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private byte[] decryptRSA(byte[] data) {
try {
AsymmetricKeyParameter privateKey = PrivateKeyFactory.createKey(this.privateKey.getEncoded());
AsymmetricBlockCipher e = new RSAEngine();
e = new org.bouncycastle.crypto.encodings.PKCS1Encoding(e);
e.init(false, privateKey);
List<Byte> value = new ArrayList<Byte>();
int i = 0;
int len = e.getInputBlockSize();
while(i<data.length) {
if(i+len > data.length) len = data.length - i;
byte[] hexEncodedCipher = e.processBlock(data, i ,len);
for(Byte b : hexEncodedCipher) {
value.add(b);
}
i+=e.getInputBlockSize();
}
return Utils.convert(value.toArray(new Byte[value.size()]));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void clear() {
privateKey = null;
publicKey = null;
}
}
之后是NullPointerException,因为解密失败了。
这是我的Cryption类:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "person", for: indexPath) as! personTableCell
cell.nameLabel.text = searchResults[indexPath.row].name
cell.emailLabel.text = searchResults[indexPath.row].email
cell.inviteButton.addTarget(self, action: #selector(NewGroupVC.addInvite), for: UIControlEvents.touchUpInside)
return cell
}
func addInvite(sender:UIButton!) {
print("Invite pressed")
}
Utils.convert([])在Byte []和byte []
之间切换我希望这是您找到解决方法的足够信息,我在这里失败了。
度过愉快的一天