我正在使用"AES256/PKCS5Padding/ECB"
加密來加密文本以發送服務器。以下Android代碼運行良好,以便在NodeJS服務器中正確解密加密數據。Objective C加密代碼不能像Android AES 256,md5加密
public static String encryptAES_Java_Node(String content, String key) {
byte[] input;
String query = null;
try {
input = content.getBytes("utf-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(key.getBytes("UTF-8"));
SecretKeySpec skc = new SecretKeySpec(thedigest,
"AES/ECB/PKCS5Padding");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skc);
byte[] cipherText = newbyte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
query = Base64.encodeToString(cipherText, Base64.DEFAULT);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ShortBufferException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return query;
}
這是我在ObjC
(NSString *)encryptText:(NSString *)rawText withKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
NSData *rawData = [rawText dataUsingEncoding:NSUTF8StringEncoding];
//Convert to hash
NSString *md5Key = [self MD5String:key];
// fetch key data
[md5Key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [rawData length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding + kCCOptionECBMode,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[rawData bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
NSData *tempData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
NSString* encrypted64 = [tempData base64EncodedStringWithOptions:0];//Even i have tried base 64 encding with other options available
return encrypted64;
}
free(buffer); //free the buffer;
return nil;
}
代碼我是不是犯了一個錯誤,而轉換鍵,MD5作爲android系統中做了什麼?
在NodeJS服務中添加用於解密的代碼以供參考。
var decipher = crypto.createDecipher('aes-128-ecb', encryption_key);
chunks = []
chunks.push(decipher.update(new Buffer(fullBuffer, "base64").toString("binary")));
chunks.push(decipher.final('binary'));
var txt = chunks.join("");
txt = new Buffer(txt, "binary").toString("utf-8");
// where encryption_key = key, fullBuffer is the input and txt is output
使用Android,您將MD5作爲字節數組獲取。使用iOS,您將獲得MD5(最有可能)作爲其字符串表示形式。 – zneak
您可能想要考慮使用加密網絡協議,而不是自己使用加密基元。密碼學既重要又難以正確。 – zneak
「不工作」不是錯誤描述。 SO不是在線調試器;請顯示你曾經嘗試過/你卡在哪裏。 –