//
//  EnercryTool.m
//  Open
//
//  Created by gao on 2016/11/17.
//  Copyright © 2016年 黄江涛 . All rights reserved.
//

#import "EnercryTool.h"

@implementation EnercryTool

+(NSString *)A:(NSString *)jiamiName
{
    NSString *name = jiamiName;
  
    NSData *aesdataresult =   [SecurityUtil encryptAESData:name];
  //转换成BASE64
   return [SecurityUtil encodeBase64Data:aesdataresult];
}
+(NSString*)desA:(NSString*)jianName{
 NSData *data = [jianName dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
   return  [SecurityUtil decryptAESData:[GTMBase64 decodeData:data]];
}



+(NSString*) decryptUseDES:(NSString*)cipherText key:(NSString*)key {
    // 利用 GTMBase64 解碼 Base64 字串
    NSData* cipherData = [GTMBase64 decodeString:cipherText];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;
    
    // IV 偏移量不需使用
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          [key UTF8String],
                                          kCCKeySizeDES,
                                          nil,
                                          [cipherData bytes],
                                          [cipherData length],
                                          buffer,
                                          1024,
                                          &numBytesDecrypted);
    NSString* plainText = nil;
    if (cryptStatus == kCCSuccess) {
        NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
        plainText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ;
    }
    return plainText;
}
+(NSString *) encryptUseDES:(NSString *)clearText key:(NSString *)key
{
    NSData *data = [clearText dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          [key UTF8String],
                                          kCCKeySizeDES,
                                          nil,
                                          [data bytes],
                                          [data length],
                                          buffer,
                                          1024,
                                          &numBytesEncrypted);
    
    NSString* plainText = nil;
    if (cryptStatus == kCCSuccess) {
        NSData *dataTemp = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        plainText = [GTMBase64 stringByEncodingData:dataTemp];
    }else{
    }
    return plainText;
}
+(NSString *)encryptBase64:(NSData *)imageData
{
 return [SecurityUtil encodeBase64Data:imageData];
}

+ (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation
{
    
    long double rotate = 0.0;
    
    CGRect rect;
    
    float translateX = 0;
    
    float translateY = 0;
    
    float scaleX = 1.0;
    
    float scaleY = 1.0;
    
    
    
    switch (orientation) {
            
        case UIImageOrientationLeft:
            
            rotate = M_PI_2;
            
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            
            translateX = 0;
            
            translateY = -rect.size.width;
            
            scaleY = rect.size.width/rect.size.height;
            
            scaleX = rect.size.height/rect.size.width;
            
            break;
            
        case UIImageOrientationRight:
            
            rotate = 3 * M_PI_2;
            
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            
            translateX = -rect.size.height;
            
            translateY = 0;
            
            scaleY = rect.size.width/rect.size.height;
            
            scaleX = rect.size.height/rect.size.width;
            
            break;
            
        case UIImageOrientationDown:
            
            rotate = M_PI;
            
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            
            translateX = -rect.size.width;
            
            translateY = -rect.size.height;
            
            break;
            
        default:
            
            rotate = 0.0;
            
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            
            translateX = 0;
            
            translateY = 0;
            
            break;
            
    }
    
    
    
    //UIGraphicsBeginImageContext(rect.size);
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:rect.size];
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //做CTM变换
    
    CGContextTranslateCTM(context, 0.0, rect.size.height);
    
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGContextRotateCTM(context, rotate);
    
    CGContextTranslateCTM(context, translateX, translateY);
    
    
    
    CGContextScaleCTM(context, scaleX, scaleY);
    
    //绘制图片
    
    CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    return newPic;
    
}


//AES代码加密工具
+(NSString *)AES256Encrypt:(NSString *)plainText key:(NSString *)key
{
    NSData *data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    
    char keyPtr[kCCKeySizeAES256+1];//
    bzero(keyPtr,sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = data.length;
    size_t bufferSize = dataLength +kCCBlockSizeAES128;
    void*buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding | kCCOptionECBMode, keyPtr, kCCBlockSizeAES128, NULL, data.bytes, dataLength, buffer, bufferSize, &numBytesEncrypted);
    
    NSData *aesData = nil;
    if (cryptStatus == kCCSuccess) {
        aesData =  [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
        
        aesData = [GTMBase64 encodeData:aesData];
        
        NSString *string = [[NSString alloc] initWithData:aesData encoding:NSUTF8StringEncoding];
        
        return string;
    }
    
    free(buffer);
    
    return nil;
}
//AES代码解密工具
+(NSString *)AES256Decrypt:(NSString *)plainText key:(NSString *)key
{
    NSData *data = [plainText dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
       
       NSData *newData = [GTMBase64 decodeData:data];
       
       char keyPtr[kCCKeySizeAES256+1];
       bzero(keyPtr, sizeof(keyPtr));
       [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
       NSUInteger dataLength = newData.length;
       size_t bufferSize = dataLength + kCCBlockSizeAES128;
       void *buffer = malloc(bufferSize);
       size_t numBytesDecrypted = 0;
       CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
       kCCOptionPKCS7Padding | kCCOptionECBMode,
       keyPtr, kCCBlockSizeAES128,
       NULL,
       newData.bytes, dataLength,
       buffer, bufferSize,
       &numBytesDecrypted);
       if (cryptStatus == kCCSuccess) {
           NSData* decryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
           
           NSString *string = [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding];
           
           return string;
       }
       free(buffer);
       return nil;
}

//AES128代码加密工具

//AES128代码解密工具
+(NSString *)AES128Decrypt:(NSString *)plainText key:(NSString *)key
{
    NSData *data = [plainText dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    
    NSData *newData = [GTMBase64 decodeData:data];
    
    char keyPtr[kCCKeySizeAES128+1];
    bzero(keyPtr, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = newData.length;
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128,
    kCCOptionPKCS7Padding | kCCOptionECBMode,
    keyPtr, kCCBlockSizeAES128,
    NULL,
    newData.bytes, dataLength,
    buffer, bufferSize,
    &numBytesDecrypted);
    if (cryptStatus == kCCSuccess) {
        NSData* decryptData = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
        
        NSString *string = [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding];
        
        return string;
    }
    free(buffer);
    return nil;
}




@end
