目标:
能够在ios上加密一段数据(字符串),在android上解密,反之亦然,为用户提供端到端的加密。
不幸的是,java中的加密消息与swift中的加密消息不匹配。
java:gtwbbtcie+km/5lw3ywltr/sd5aon6ii66cqsvbisae=
swift:ifa3j0lbpiyz64ge0m67pbpwlyesvgshvwy2m+andq+lruauqoq9b3clqfh1
我尝试的是:
java实现
package com.company;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
public class Main {
public static void main(String[] args) throws Exception {
// Get Base64 encoder
Base64.Encoder b64e = Base64.getEncoder();
// Message to encrypt
String str = "Hello, playground";
// Password to use for encryption
String key = "password";
// The bytes of the string requiring encryption
byte[] strByteArray = str.getBytes(StandardCharsets.UTF_8);
// Print the message in plaintext
System.out.println("Message: " + str);
// Print the Base64 encoded message bytes
System.out.println("Message B64: " + b64e.encodeToString(strByteArray));
// The bytes of the key
byte[] keyByteArray = key.getBytes(StandardCharsets.UTF_8);
// Print the key in plaintext
System.out.println("Key: " + key);
// Create an instance of MessageDigest to hash the key using the SHA-256 algorithm
MessageDigest hasher = MessageDigest.getInstance("SHA-256");
// The bytes of the hash digest
byte[] keyHashByteArray = hasher.digest(keyByteArray);
// Print the Base64 encoded key hash bytes
System.out.println("Key B64: " + b64e.encodeToString(keyHashByteArray));
// Create an instance of a SecretKey from the key hash bytes
SecretKey secretKey = new SecretKeySpec(keyHashByteArray, "AES");
// Create an instance of an AES cipher
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
// Set the cipher mode to encryption and supply the previously computed SecretKey
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// Encrypt the message string bytes using an AES-256 cipher using the secret key
byte[] ciphertext = cipher.doFinal(strByteArray);
// Print the Base64 encoded ciphertext bytes
System.out.println("Encrypted Message: " + b64e.encodeToString(ciphertext));
}
}
java输出:
留言:你好,Playground
消息b64:sgvsbg8sihbsyxlncm91bmq=
密钥:密码
b64键:Xohimnoobhfr0ovvjcypj3ngpqq73wkhvch0vqtg=
加密信息:gtwbbtcie+km/5lw3ywltr/sd5aon6iii66cqsvbisae=
迅速实施
import Cocoa
import CryptoKit
// Message to encrypt
var str = "Hello, playground"
// Print the message in plaintext
print("Message: \(str)")
// The bytes of the string requiring encryption
var messageByteArray = Data(str.utf8)
// Print the Base64 encoded message bytes
print("Message B64: \(Data(messageByteArray).base64EncodedString())")
// Password to use for encryption
var key = "password"
// Print the key in plaintext
print("Key: \(key)")
// The bytes of the key
var keyByteArray = Data(key.utf8)
// The bytes of the hash digest
var keyHashByteArray = SHA256.hash(data: keyByteArray);
// Create an instance of a SymmetricKey from the key hash bytes
var symetricKeyFromHash = SymmetricKey(data: keyHashByteArray)
// Print the Base64 encoded key hash bytes
symetricKeyFromHash.withUnsafeBytes {body in
print("Key B64: \(Data(body).base64EncodedString())")
}
// Encrypt the message string bytes using an AES-256 cipher using the secret key
let sealed = try AES.GCM.seal(messageByteArray, using: symetricKeyFromHash)
// Print the Base64 encoded ciphertext bytes
print("Encrypted Message: \(sealed.combined!.base64EncodedString())")
快速输出:
留言:你好,Playground
消息b64:sgvsbg8sihbsyxlncm91bmq=
密钥:密码
b64键:Xohimnoobhfr0ovvjcypj3ngpqq73wkhvch0vqtg=
加密消息:nayn6w22c4icjuyypxakbmlq5yktfugdpycovirfxisdak4xrjhh9yv+15z9
暂无答案!
目前还没有任何答案,快来回答吧!