iOS,Android,.NET通用AES密碼編譯演算法

來源:互聯網
上載者:User

標籤:

原文:iOS,Android,.NET通用AES密碼編譯演算法

這兩天為移動App開發API,結果實現加密驗證時碰到一大坑。這裡不得不吐槽下又臭又硬的iOS,Windows Server無法解密出正確的結果,Android則可以,後來使用了通用的AES256密碼編譯演算法才最終搞定。

搞伺服器端小夥伴沒有接觸過iOS,所以也沒料到過這種情形。他使用了AES128 with IV的密碼編譯演算法,Android端可以順利通過加密驗證。

但是iOS端使用AES128演算法後出現問題,雖然可以在本地加密解密,但是無法被伺服器解密成功。

後來經過多方尋找,才瞭解到一個蛋疼的事實,iOS只支援AES PKCS7Padding演算法,在伺服器端修改為響應演算法後,順利通過。

這裡主要參考一篇博文,以下給出通用AES演算法:

 

Objective-C:

//標頭檔
#import <Foundation/Foundation.h>@interface NSData (AES)- (NSData *)AES256EncryptWithKey:(NSString *)key;- (NSData *)AES256DecryptWithKey:(NSString *)key;@end

實現代碼:

#import "NSData+AES256.h"#import <CommonCrypto/CommonCryptor.h>@implementation NSData (AES)-(NSData *)AES256EncryptWithKey:(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)        // fetch key data    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];        NSUInteger dataLength = [self 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,                                          keyPtr, kCCKeySizeAES256,                                          NULL /* initialization vector (optional) */,                                          [self bytes], dataLength, /* 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;}-(NSData *)AES256DecryptWithKey:(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)        // fetch key data    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];        NSUInteger dataLength = [self 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 numBytesDecrypted = 0;    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,                                          keyPtr, kCCKeySizeAES256,                                          NULL /* initialization vector (optional) */,                                          [self bytes], dataLength, /* input */                                          buffer, bufferSize, /* output */                                          &numBytesDecrypted);        if (cryptStatus == kCCSuccess) {        //the returned NSData takes ownership of the buffer and will free it on deallocation        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];    }        free(buffer); //free the buffer;    return nil;}@end

 

C#:

#region        /// <summary>        /// 256位AES加密        /// </summary>        /// <param name="toEncrypt"></param>        /// <returns></returns>        public static string Encrypt(string toEncrypt)        {            // 256-AES key                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);            RijndaelManaged rDel = new RijndaelManaged();            rDel.Key = keyArray;            rDel.Mode = CipherMode.ECB;            rDel.Padding = PaddingMode.PKCS7;            ICryptoTransform cTransform = rDel.CreateEncryptor();            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);            return Convert.ToBase64String(resultArray, 0, resultArray.Length);        }        /// <summary>        /// 256位AES解密        /// </summary>        /// <param name="toDecrypt"></param>        /// <returns></returns>        public static string Decrypt(string toDecrypt)        {            // 256-AES key                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);            RijndaelManaged rDel = new RijndaelManaged();            rDel.Key = keyArray;            rDel.Mode = CipherMode.ECB;            rDel.Padding = PaddingMode.PKCS7;            ICryptoTransform cTransform = rDel.CreateDecryptor();            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);            return UTF8Encoding.UTF8.GetString(resultArray);        }        #endregion

 

Java

import java.io.UnsupportedEncodingException;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.util.Arrays;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import android.util.Base64;public class AESUtils {    /**      * 加密      * @param content 需要加密的內容      * @param password  加密密碼      * @return      */      private static String Key="GcA*23jKJf0df09Osf09834ljlJF0920";    public static String encode(String stringToEncode) throws NullPointerException {        try {            SecretKeySpec skeySpec = getKey(Key);            byte[] clearText = stringToEncode.getBytes("UTF8");            final byte[] iv = new byte[16];            Arrays.fill(iv, (byte) 0x00);            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);            String encrypedValue = Base64.encodeToString(cipher.doFinal(clearText), Base64.DEFAULT);            return encrypedValue;                    } catch (InvalidKeyException e) {            e.printStackTrace();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        } catch (BadPaddingException e) {            e.printStackTrace();        } catch (NoSuchPaddingException e) {            e.printStackTrace();        } catch (IllegalBlockSizeException e) {            e.printStackTrace();        } catch (InvalidAlgorithmParameterException e) {            e.printStackTrace();        }        return "";    }                 private static SecretKeySpec getKey(String password) throws UnsupportedEncodingException {        int keyLength = 256;        byte[] keyBytes = new byte[keyLength / 8];        Arrays.fill(keyBytes, (byte) 0x0);        byte[] passwordBytes = password.getBytes("UTF-8");        int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length;        System.arraycopy(passwordBytes, 0, keyBytes, 0, length);        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");        return key;    }        }

 

iOS,Android,.NET通用AES密碼編譯演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.