用Java实现Swift-AES加密
我对Swift上的AES GCM加密有问题 我们在Java上有一个应用程序,加密是可以的,服务器可以读取和处理数据,但我的结果服务器不能读取 我们有两个不同的结果。 起初我尝试使用加密CBC和ECB,但他们告诉我应该使用GCM。 如果有人知道我做错了什么,请帮助我 Java代码:用Java实现Swift-AES加密,java,swift,encryption,aes,aes-gcm,Java,Swift,Encryption,Aes,Aes Gcm,我对Swift上的AES GCM加密有问题 我们在Java上有一个应用程序,加密是可以的,服务器可以读取和处理数据,但我的结果服务器不能读取 我们有两个不同的结果。 起初我尝试使用加密CBC和ECB,但他们告诉我应该使用GCM。 如果有人知道我做错了什么,请帮助我 Java代码: final String airSecretKey = "Wk+Uzyyn8991w/2V5OIqiQ=="; static Cipher cipher=null; SecretKeySpec ne
final String airSecretKey = "Wk+Uzyyn8991w/2V5OIqiQ==";
static Cipher cipher=null;
SecretKeySpec new_key=null;
Key kateKey=null;
public void onCreate() {
super.onCreate();
handler = new Handler();
if (doCryptoAes) {
new_key = new SecretKeySpec(airSecretKey.getBytes(), "AES");
kateKey = (Key) new SecretKeySpec(airSecretKey.getBytes(), "AES");
}
}
void generateCliper(){
try {
cipher = Cipher.getInstance("AES/GCM/NoPadding"); ///", "BC
} catch (NoSuchAlgorithmException e) {
Log.e("AES 1", e.toString());
} catch (NoSuchPaddingException e) {
Log.e("AES 2", e.toString());
} /*catch (NoSuchProviderException e) {
Log.e("AES 3", e.toString());
}*/
}
protected String encryptAir(String testText) {
byte[] encodedBytes = null;
String s_encode_result = "";
try {
byte[] iv = new byte[12];
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
if (cipher==null){
generateCliper();
}
cipher.init(Cipher.ENCRYPT_MODE, kateKey, ivParameterSpec); //new_key //
encodedBytes = cipher.doFinal(testText.getBytes());
for (int i=0;i<encodedBytes.length; i++){
s_encode_result+=getEncodeHex(encodedBytes[i]);//+" ";
}
} catch (Exception e) {
Log.e(e.toString());
}
return "<BJSN>"+s_encode_result+"</BJSN>";
}
protected String decryptAir(String encodedText) {
if (encodedText.length()<20) return "";
byte[] encryptedTextByte = getConvAES(encodedText);
//Base64.decode(encodedText, Base64.DEFAULT);
byte[] iv = new byte[12];
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
try {
cipher.init(Cipher.DECRYPT_MODE, kateKey, ivParameterSpec);
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
byte[] decryptedByte = new byte[0];
try {
decryptedByte = cipher.doFinal(encryptedTextByte);
} catch (BadPaddingException e) {
Log.e("AES 1", e.toString());
} catch (IllegalBlockSizeException e) {
Log.e("AES 2", e.toString());
}
String decryptedText = new String(decryptedByte);
// MainActivity.toast_str="Decrypted: "+decryptedText;
return decryptedText;
}
byte[] getConvAES(String textAesStr) {
int i_len = (textAesStr.length()-13)/2;
byte[] aesNonce= new byte[i_len];
if (textAesStr.indexOf( "<BJSN>") == 0 &&
textAesStr.indexOf( "</BJSN>") > 0 && i_len > 0) {
for (int i = 3; i < i_len+3; i++) {
String s_hex = "";
s_hex+=textAesStr.charAt(i*2);
s_hex+=textAesStr.charAt(i*2+1);
int i_binary=0;
try {
i_binary=Integer.parseInt(s_hex, 16);
aesNonce[i-3]=(byte) i_binary;
} catch (Exception e) {
aesNonce[i-3]=0;
}
}
}
return aesNonce;
}
这不是加密的工作方式。每次加密相同的明文时,必须得到不同的输出。对于GCM,每次使用一个唯一的随机IV,即一个nonce(n使用次数一次)
String airSecretKey=“Wk+Uzyyn8991w/2V5OIqiQ==”;//此密钥现在是公共密钥,因此您无法使用它
SecretKey key=newsecretkeyspec(Base64.getDecoder().decode(airSecretKey),“AES”);
SecureRandom SecureRandom=新的SecureRandom();
字节[]iv=新字节[12];
安全随机。下一字节(iv);
AlgorithmParameters params=新的GCMParameterSpec(128,iv);
Cipher Cipher=Cipher.getInstance(“AES/GCM/NoPadding”);
cipher.init(cipher.ENCRYPT_MODE,key,params,secureRandom);
cipher.updateAAD(…);//如果使用
字节[]加密=cipher.doFinal(明文);
Swift版本应执行相同的操作。Java生成标记并将其附加到密文中。在Swift中,您似乎必须手动处理它。Swift代码似乎正在加密数据本身,而不是从服务器解密任何内容。是的,Swift加密字符串“BSD AIR”,因为我有这个字符串的Java解密结果,并尝试接收相同的结果。你试图分别加密两次,得到相同的结果?任何依赖于重用nonce的系统都会被破坏。一个nonce的全部意义在于它只被使用过一次。每次加密相同的明文时,必须得到不同的密文。但是Java结果总是相同的Swift结果也总是相同的,但是Java和Swift结果是不同的,因为Java代码是错误的。看起来您的静脉输液总是为零,这可能是问题所在。我们可以联系一些messenger吗?@Аццццццюцццццц1102
import CryptoKit
let key = SymmetricKey(size: .bits192)
let plain = "BSD AIR"
func cryptoDemoCombinedData() {
let nonce = try! AES.GCM.Nonce(data: Data(base64Encoded: "fv1nixTVoYpSvpdA")!)
let tag = Data(base64Encoded: "Wk+Uzyyn8991w/2V5OIqiQ==")!
// Encrypt
let sealedBox = try! AES.GCM.seal(plain.data(using: .utf8)!, using: key, nonce: nonce, authenticating: tag)
// Decrypt
let sealedBoxRestored = try! AES.GCM.SealedBox(combined: sealedBox.combined!)
let decrypted = try! AES.GCM.open(sealedBoxRestored, using: key, authenticating: tag)
print("Crypto Demo II\n••••••••••••••••••••••••••••••••••••••••••••••••••\n")
print("Combined:\n\(sealedBox.combined!.base64EncodedString())\n")
print("Cipher:\n\(sealedBox.ciphertext.base64EncodedString())\n")
print("Nonce:\n\(nonce.withUnsafeBytes { Data(Array($0)).base64EncodedString() })\n")
print("Tag:\n\(tag.base64EncodedString())\n")
print("Decrypted:\n\(String(data: decrypted, encoding: .utf8)!)\n")
}