加密密钥是java和iOS的区别
我使用以下代码使用AES/ECB/PKCS5P添加算法进行加密,函数:加密密钥是java和iOS的区别,java,ios,swift,Java,Ios,Swift,我使用以下代码使用AES/ECB/PKCS5P添加算法进行加密,函数: public static void setKey(String myKey) { MessageDigest sha = null; try { key = myKey.getBytes("UTF-8"); sha = MessageDigest.getInstance("SHA-1");
public static void setKey(String myKey) {
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
// e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// e.printStackTrace();
}
}
public static String encrypt(String strToEncrypt, String secret) {
try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")), Base64.DEFAULT);
} catch (Exception e) {
// System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
func AESEncryption(key: String) -> String? {
let keyData: NSData! = (key as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
let data: NSData! = (self as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
let cryptData = NSMutableData(length: Int(data.length) + kCCBlockSizeAES128)!
let keyLength = size_t(kCCKeySizeAES128)
let operation: CCOperation = UInt32(kCCEncrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128)
let options: CCOptions = UInt32(kCCOptionECBMode + kCCOptionPKCS5Padding)
var numBytesEncrypted :size_t = 0
let cryptStatus = CCCrypt(operation,
algoritm,
options,
keyData.bytes, keyLength,
nil,
data.bytes, data.length,
cryptData.mutableBytes, cryptData.length,
&numBytesEncrypted)
if UInt32(cryptStatus) == UInt32(kCCSuccess) {
cryptData.length = Int(numBytesEncrypted)
var bytes = [UInt8](repeating: 0, count: cryptData.length)
cryptData.getBytes(&bytes, length: cryptData.length)
var hexString = ""
for byte in bytes {
hexString += String(format:"%02x", UInt8(byte))
}
return hexString}
return nil //Check condition nil
}
输出:
Meeting ID : 20201228165764
Passcode: 769762
Encrypted values:
meetingid: LfD3obeP45AAGXYxPiByvQ==
passcode: fcJSdMArO3vUOL/WxwzRfw==
我在iOS中使用了以下AES加密代码,但获得了不同的加密密钥,函数:
public static void setKey(String myKey) {
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
// e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// e.printStackTrace();
}
}
public static String encrypt(String strToEncrypt, String secret) {
try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")), Base64.DEFAULT);
} catch (Exception e) {
// System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
func AESEncryption(key: String) -> String? {
let keyData: NSData! = (key as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
let data: NSData! = (self as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
let cryptData = NSMutableData(length: Int(data.length) + kCCBlockSizeAES128)!
let keyLength = size_t(kCCKeySizeAES128)
let operation: CCOperation = UInt32(kCCEncrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithmAES128)
let options: CCOptions = UInt32(kCCOptionECBMode + kCCOptionPKCS5Padding)
var numBytesEncrypted :size_t = 0
let cryptStatus = CCCrypt(operation,
algoritm,
options,
keyData.bytes, keyLength,
nil,
data.bytes, data.length,
cryptData.mutableBytes, cryptData.length,
&numBytesEncrypted)
if UInt32(cryptStatus) == UInt32(kCCSuccess) {
cryptData.length = Int(numBytesEncrypted)
var bytes = [UInt8](repeating: 0, count: cryptData.length)
cryptData.getBytes(&bytes, length: cryptData.length)
var hexString = ""
for byte in bytes {
hexString += String(format:"%02x", UInt8(byte))
}
return hexString}
return nil //Check condition nil
}
在android代码中,生成的密钥不同于iOS。您能帮助从AES/ECB/PKS5P添加算法中获取相同的加密密钥吗?我正在使用link进行检查。在您的Swift代码中,您只是按原样使用字符串字节,而没有进行哈希运算
请注意,您不应该为此使用SHA-1;您应该改用一个,或者(最理想的)一个预打包的库来为您执行所有这些容易出错的操作。我在iOS中获得与android相同的密钥,但没有获得与android相同的加密密钥。请编辑您的帖子,并为两个系统(android/iOS)添加示例数据,以便我们可以比较输出,谢谢。以上代码文本为:20201228165764的Android机密:Tata@123#Secretkey:1EVNN6xH8I/qd8rMmClhSQ==加密密钥:cFbx46JOjlK8Q0TpmkOrKg==SHA1密钥的Ios代码:+(NSData*)SHA1 HASHFROMSTRING:(NSString*)stringToHash{NSData*stringData=[stringToHash数据使用编码:NSASCISTRINGENCODE];uint8[CC_SHA1_DIGEST_LENGTH]={16};CC_SHA1([stringData bytes],[stringData LENGTH],DIGEST);NSData*hashedData=[NSData dataWithBytes:DIGEST LENGTH:16];return hashedData;}获得与android相同的密钥,但加密密钥不同: