ios gzip 压缩字符串

1. #import "zlib.h"

2. libz.1.2.5.tbd

3.

- (NSData *)gzipDeflate:(NSString*)str

{
    NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
    if ([data length] == 0) return data;
    
    z_stream strm;
    
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.total_out = 0;
    strm.next_in=(Bytef *)[data bytes];
    strm.avail_in = [data length];
    
    // Compresssion Levels:
    //   Z_NO_COMPRESSION
    //   Z_BEST_SPEED
    //   Z_BEST_COMPRESSION
    //   Z_DEFAULT_COMPRESSION
    
    if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil;
    
    NSMutableData *compressed = [NSMutableData dataWithLength:16384];  // 16K chunks for expansion
    
    do {
        
        if (strm.total_out >= [compressed length])
            [compressed increaseLengthBy: 16384];
        
        strm.next_out = [compressed mutableBytes] + strm.total_out;
        strm.avail_out = [compressed length] - strm.total_out;
        
        deflate(&strm, Z_FINISH);
        
    } while (strm.avail_out == 0);
    
    deflateEnd(&strm);
    
    [compressed setLength: strm.total_out];
    return [NSData dataWithData:compressed];
}

- (NSData *)gzipInflate:(NSData*)data
{
    if ([data length] == 0) return data;
    
    unsigned full_length = [data length];
    unsigned half_length = [data length] / 2;
    
    NSMutableData *decompressed = [NSMutableData dataWithLength: full_length + half_length];
    BOOL done = NO;
    int status;
    
    z_stream strm;
    strm.next_in = (Bytef *)[data bytes];
    strm.avail_in = [data length];
    strm.total_out = 0;
    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    
    if (inflateInit2(&strm, (15+32)) != Z_OK)
        return nil;
    
    while (!done)
    {
        // Make sure we have enough room and reset the lengths.
        if (strm.total_out >= [decompressed length])
            [decompressed increaseLengthBy: half_length];
        strm.next_out = [decompressed mutableBytes] + strm.total_out;
        strm.avail_out = [decompressed length] - strm.total_out;
        
        // Inflate another chunk.
        status = inflate (&strm, Z_SYNC_FLUSH);
        if (status == Z_STREAM_END)
            done = YES;
        else if (status != Z_OK)
            break;
    }
    if (inflateEnd (&strm) != Z_OK)
        return nil;
    
    // Set real length.
    if (done)
    {
        [decompressed setLength: strm.total_out];
        return [NSData dataWithData: decompressed];
    }
    else return nil;

}

4.调用:

      NSString *str = @"123456";

       NSData* en = [self gzipDeflate:str];
       NSData* unzip = [self gzipInflate:en];
       
       NSString* newStr = [[NSString alloc] initWithData:unzip encoding:NSUTF8StringEncoding];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要使用 iOS 中的 Security 框架来进行 RSA 加密操作。以下是一个简单的示例代码,演示如何使用公钥字符串进行 RSA 加密: ```swift func encryptString(_ string: String, publicKey: String) -> String? { guard let data = string.data(using: .utf8) else { return nil } // 将公钥字符串转换为 SecKey 对象 guard let publicKeyData = Data(base64Encoded: publicKey), let publicKeySecKey = try? getPublicKey(from: publicKeyData) else { return nil } // 执行 RSA 加密操作 var error: Unmanaged<CFError>? guard let encryptedData = SecKeyCreateEncryptedData(publicKeySecKey, .rsaEncryptionPKCS1, data as CFData, &error) as Data? else { return nil } return encryptedData.base64EncodedString() } func getPublicKey(from data: Data) throws -> SecKey { // 定义公钥的属性 let attributes: [CFString: Any] = [ kSecAttrKeyType: kSecAttrKeyTypeRSA, kSecAttrKeyClass: kSecAttrKeyClassPublic, kSecAttrKeySizeInBits: 2048 ] // 从数据中创建一个SecKey对象 guard let secKey = SecKeyCreateWithData(data as CFData, attributes as CFDictionary, nil) else { throw EncryptionError.invalidPublicKey } return secKey } enum EncryptionError: Error { case invalidPublicKey } ``` 在上面的代码中,`encryptString` 函数接收一个字符串和一个公钥字符串作为参数,并返回加密后的字符串。首先,将输入字符串转换为数据对象。然后,将公钥字符串转换为 `SecKey` 对象,该对象可以用于执行 RSA 加密操作。最后,使用 `SecKeyCreateEncryptedData` 函数进行加密,返回加密后的数据对象。最后,将加密后的数据对象转换为 Base64 编码的字符串,并返回它。 `getPublicKey` 函数将公钥字符串转换为 `SecKey` 对象。此函数首先定义一个字典,其中包含公钥的属性。然后,使用 `SecKeyCreateWithData` 函数从数据中创建一个 `SecKey` 对象,并将其返回。 需要注意的是,这里使用的是 PKCS#1 v1.5 填充方式进行加密。如果你需要使用其他填充方式,请根据需要更改 `SecKeyCreateEncryptedData` 函数的第二个参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值