如何将Cipher.getInstance(“AES”)更改为目标c CCCrypt

时间:2016-09-27 07:11:28

标签: ios objective-c

这是Android代码,它给了我正确的结果。

Android代码:

public class EncUtil {
    //String enc_key = "ea3401cec22dec24e9756a71904b8515";
        public String  encmsg(String msg,String enc_key)
            {
                try {
                    SecretKeySpec skeySpec = new SecretKeySpec(HexfromString(enc_key), "AES");
                    Cipher cipher = Cipher.getInstance("AES");
                    cipher.init(1, skeySpec);

                    byte encstr[] = cipher.doFinal(msg.getBytes());

                    String decmsg=""+HextoString(encstr);
//                    System.out.println(decmsg);

                    return ""+decmsg;

                } catch (InvalidKeyException 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 (IllegalBlockSizeException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (NullPointerException e){
                    e.printStackTrace();
                }
                return "0";

        }

下面是Android结果:2EAE0094E39C9F36B8C5FCF842977972556AC1B99888B174F1897335984942AA3AA43B8AE09C93378AB579A0CF7507DA7FFF7F7BCE659141154578CF3C156852D0D6C3A195E9AF5D666FDE37DD1B656E8C6542C33A4B78B13B9F29E70C479AFA227D4C3F3051CED78FA47B5821FB9BF1FA0E43B619A6DCBDA7A4B059D3513762229F840BD237648BA0CB61173BC117EE44500B6477D7CFAB76BB602F4AB0F36BDC3E3FBA2D3A041FFD9214E7FC5D1787CD50815778D0C65BC613D77199DD301DF1DC011BC8C5D94513B6C7535816E586

这是我的iOS代码,它提供了不同的值

- (NSString *)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;}


//Hex From String value is  : 6561333430316365633232646563323465393735366137313930346238353135

//这是ios的结果:     Zkyu7AIWwIHmY81pgjRYuDSHFktO1kEr8waV8 / tjBC5 / Hm2nKS8FFl5fVBYT / WVN + FjxCvRsNnWmqqLKMxN947FwMKrXGL6lo93vB4JXR8dmI1UbirgPa2 / jdlknzbmk6rkIHkAgbjgaDV3ZzWIq4w0Zh4vvfko2ArywjFR7KEPTLXrS3am4UEn5zU4WbW4QrGooDgjPDVc7KjTGVNo + TmNvX3yKHLnDcX9L9NiLBzMW9L3vP8sEvFpus8SB1ka12RPTEJEgTZYvWGu4vDrrRg ==

0 个答案:

没有答案