首先是第一件事。前段時間,我需要在Android中使用簡單的AES加密來加密密碼,並將其作爲密碼被解密的.net Web服務的參數發送。iOS和Android中的AES加密以及C#.NET中的解密。
以下是我的Android加密:
private static String Encrypt(String text, String key)
throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] keyBytes= new byte[16];
byte[] b= key.getBytes("UTF-8");
int len= b.length;
if (len > keyBytes.length) len = keyBytes.length;
System.arraycopy(b, 0, keyBytes, 0, len);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);
byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
String result = Base64.encodeBytes(results);
return result;
}
然後我用解密在C#中:
public static string Decrypt(string textToDecrypt, string key)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 0x80;
rijndaelCipher.BlockSize = 0x80;
string decodedUrl = HttpUtility.UrlDecode(textToDecrypt);
byte[] encryptedData = Convert.FromBase64String(decodedUrl);
byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[0x10];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return encoding.GetString(plainText);
}
這工作就像一個魅力,但是當我試圖做的問題來了在iOS中也是如此。我的iPhone/iPad的漂亮的新開發的應用程序,所以ofcause我GOOGLE了它,幾乎提供了所有的代碼示例是如下:
- (NSData *)AESEncryptionWithKey:(NSString *)key {
char keyPtr[kCCKeySizeAES128]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES128,
NULL /* initialization vector (optional) */,
[self bytes], [self length], /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
也許我是有點過於樂觀,當我希望的是平穩過渡這裏,因爲當Android是扔我的東西,如:
"EgQVKvCLS4VKLoR0xEGexA=="
那麼iOS版給我:
"yP42c9gajUra7n0zSEuVJQ=="
希望它只是我忘了的東西,或者一些設置是錯誤的?
[更新]結果現在顯示在base64編碼之後。
android版本是一個URL編碼的base64字符串,它等於'「YHH + gTxyIxvAx1cPFLcP0IEW2HcVHQVi9X11656CFsk =」''(60 71 fe 81 3c 72 23 1b c0 c7 57 0f 14 b7 0f d0 81 16 d8 77 15 1d 05 62 f5 7d 75 eb 9e 82 16 c9)'。 – Joe 2012-08-14 13:17:51
Woops,對不起,我忘了提及這一點。我使用另一種方法對iOS版本的結果進行編碼,但在編碼之前結果不同。 – Morten 2012-08-14 13:21:02
更新了問題以顯示base64編碼後的結果。使用相同的密碼和相同的密鑰。 – Morten 2012-08-14 13:45:41