DES 加密 解密 iOS Android PHP

腾讯云服务器 双11 优惠
https://cloud.tencent.com/act/double11/reserve?spread_hash_key=Pel1qM

PHP代码 和 iOS 代码 
摘自 http://blog.toright.com/posts/2657/ios-objective-c-與-php-des-加解密演算法實作.html
三个平台共用 一个密匙,尽量为8或8的倍数
1.php代码

  

2.iOS 代码(如果出现无法解析,先查看是否申请的内存足够使用) 我统一1024 * 5
之前出现解析不出, 吃了很大的亏

+(NSString*) decryptUseDES:(NSString*)cipherText key:(NSString*)key {  
    // 利用 GTMBase64 解碼 Base64 字串  
      
    NSData* cipherData = [cipherText base64DecodedData];  
    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] autorelease];  
    }  
    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 = [dataTemp base64EncodedString];  
    }else{  
//        NSLog(@"DES加密失败");  
    }  
    return plainText;  
}  


3.Android 代码
密钥  如果 密匙不足 8个 字符  java代码 要加 \0
例如 secretKey = "xxxxxxx\0"; 
由于我们后台设置的密匙为7位,造成了一系列的麻烦,后经过查询php 文档解决,添加secretKey = "xxxxxxx\0"; 

public class Des4 {  
    // 密钥  
    private final static String secretKey = "";  
    private static byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0};  
  
    public static String encode(String plainText) throws Exception {  
        IvParameterSpec zeroIv = new IvParameterSpec(iv);  
        SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "DES");  
        Cipher cipher = Cipher.getInstance("DES");  
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);  
        byte[] encryptedData = cipher.doFinal(plainText.getBytes());  
        return Base64.encodeToString(encryptedData, 1);  
    }  
  
    public static String decode(String encryptText) throws Exception {  
        IvParameterSpec zeroIv = new IvParameterSpec(iv);  
        SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "DES");  
        Cipher cipher = Cipher.getInstance("DES");  
        cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);  
        byte[] decryptData = cipher.doFinal(Base64.decode(encryptText, 1));  
        return new String(decryptData);  
    }  
}  

你可能感兴趣的:(DES 加密 解密 iOS Android PHP)