在Objective-C Xcode中转换android的AES加密 [英] Convert AES encryption of android in Objective-c xcode
问题描述
我必须使用AES加密和解密类似的代码
I have to use AES Encryption and decryption similiar to below code
需要像Android一样传递类似的数据来生成KEY
Need to pass similiar data like android did to generate KEY
package encypt.com;
import java.io.BufferedReader;
import java.io.FileReader;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
public class Testing {
private static final String ALGORITHM = "AES";
private static final int ITERATIONS = 2;
private static final byte[] keyValue =
new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};
public static String encrypt(String value, String salt) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
String valueToEnc = null;
String eValue = value;
for (int i = 0; i < ITERATIONS; i++) {
valueToEnc = salt + eValue;
byte[] encValue = c.doFinal(valueToEnc.getBytes());
eValue = new BASE64Encoder().encode(encValue);
}
return eValue;
}
public static String decrypt(String value, String salt) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
String dValue = null;
String valueToDecrypt = value;
for (int i = 0; i < ITERATIONS; i++) {
byte[] decordedValue = new BASE64Decoder().decodeBuffer(valueToDecrypt);
byte[] decValue = c.doFinal(decordedValue);
dValue = new String(decValue).substring(salt.length());
valueToDecrypt = dValue;
}
return dValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
// key = keyFactory.generateSecret(new DESKeySpec(keyValue));
return key;
}
我使用的iOS
我通过大量研究发现了这段代码:
I've found this code through lots of research:
#import "<CommonCrypto/CommonCryptor.h>"
@implementation NSMutableData(AES)
对于加密:
- (NSMutableData*) EncryptAES:(NSString *)key {
char keyPtr[kCCKeySizeAES256+1];
bzero( keyPtr, sizeof(keyPtr) );
[key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];
size_t numBytesEncrypted = 0;
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
NSMutableData *output = [[NSData alloc] init];
CCCryptorStatus result = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL, [self mutableBytes], [self length], buffer, bufferSize, &numBytesEncrypted);
output = [NSMutableData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
if(result == kCCSuccess) {
return output;
}
return NULL;
}
用于解密:
- (NSMutableData*)DecryptAES: (NSString*)key andForData:(NSMutableData*)objEncryptedData {
char keyPtr[kCCKeySizeAES256+1];
bzero( keyPtr, sizeof(keyPtr) );
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF16StringEncoding];
size_t numBytesEncrypted = 0;
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer_decrypt = malloc(bufferSize);
NSMutableData *output_decrypt = [[NSData alloc] init];
CCCryptorStatus result = CCCrypt(kCCDecrypt , kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL, [self mutableBytes], [self length], buffer_decrypt, bufferSize, &numBytesEncrypted);
output_decrypt = [NSMutableData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];
if(result == kCCSuccess) {
return output_decrypt;
}
return NULL;
}
}
这是我编写的与上面的代码相对应的代码:
This is the code I made that I would like to correspond with the above code:
- (void)Encrypt {
//Convert NSString to NSData so that it can be used to encrypt the Input
NSString *Input = [Inputbox text];
NSData *InputData = [Input dataUsingEncoding:NSUTF8StringEncoding];
//What to do here
}
这些方法如何使用此代码?它在我的实施文件中的什么位置? 顶部附近的这一行表示您正在向NSMutableData添加AES功能:
How do I use this code, these methods? Where does it go in my Implementation file? This line near the top says you're adding AES functionality to NSMutableData:
@implementation NSMutableData(AES)
在Objective-C中,这称为类别;类别使您可以扩展现有的类.
In Objective-C, this is called a category; categories let you extend an existing class.
此代码通常会放入名为NSMutableData-AES.m的文件中.还要创建一个头文件NSMutableData-AES.h.它应包含:
This code would typically go in a file named NSMutableData-AES.m. Create a header file too, NSMutableData-AES.h. It should contain:
@interface NSMutableData(AES)
- (NSMutableData*) EncryptAES: (NSString *) key;
@end
在主文件中包含(#import)该标头.在您的代码中添加对加密功能的调用:
Include (#import) that header in your main file. Add a call to the encryption function in your code:
NSData *InputData = [Input dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [InputData EncryptAES:@"myencryptionkey"];
类似地用于解密.
但是我无法像Android一样设置keyValue和SALT! 请帮助
BUT i am unable to set keyValue and SALT like android did! Please help
private static final byte[] keyValue =
new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};
valueToEnc = salt + eValue;
推荐答案
您可以使用第三方库 RNCryptor 在cocoapods中进行加密/解密
You can use third party library RNCryptor in cocoapods to en/decryption
// Encryption
NSData *data = ...
NSString *password = @"Secret password";
NSData *ciphertext = [RNCryptor encryptData:data password:password];
// Decryption
NSError *error = nil;
NSData *plaintext = [RNCryptor decryptData:ciphertext password:password error:&error];
if (error != nil) {
NSLog(@"ERROR:%@", error);
return
}
// ...
这篇关于在Objective-C Xcode中转换android的AES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!