首页 > Android, php, 未分类 > php java object-c 兼容DES算法,通用于android,ios,php,java服务器平台

php java object-c 兼容DES算法,通用于android,ios,php,java服务器平台

在移动互联网行业的手机端和服务器开发中,经常需要对敏感数据进行加密。在实际开发中,除了考虑安全性之外,还有一个非常重要的就是平台间的算法兼容问题。
这边我把我们开发中应用于php,android,ios,java 四大平台的对称加密算法DES实现罗列出来,希望能够让大家少走弯路。
首先我们统一两个变量
key=”20140401″;//加密因子
iv=”12345678″/偏移量

1.php代码如下

<?php
class DES {
	var $key;
	var $iv;
	function __construct($key,$iv) {
		$this->key = $key;
		$this->iv = $iv;
	}

	/**加密*/
	function encrypt($str) {
		$size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
		$str = $this->pkcs5Pad($str, $size);

		$data = mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv);
		return base64_encode($data);
	}

	/**解密*/
	function decrypt($str) {
		$str = base64_decode($str);
		$str = mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_DECRYPT, $this->iv);
		$str = $this->pkcs5Unpad($str);
		return $str;
	}
	function pkcs5Pad($text, $blocksize) {
		$pad = $blocksize - (strlen($text) % $blocksize);
		return $text . str_repeat(chr($pad), $pad);
	}
	function pkcs5Unpad($text) {
		$pad = ord($text{strlen($text) - 1});
		if ($pad > strlen($text)) return false;
		if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
		return substr($text, 0, -1 * $pad);
	}
}
global $AES_KEY;
global $AES_IV;
$AES_KEY ="20140401";
$AES_IV = "12345678";
$MyDes = new DES($AES_KEY,$AES_IV);

 

2.Android 平台

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @ClassName: Encryption.java
 * @Description: TODO
 * @author FrankWong
 * @version V1.0
 * @Date 2013-12-9 下午10:06:16
 */
public class Encryption {

	static final String key = "20140401";
	static final String iv = "12345678";

	public static void main(String[] args) {
		try {
			System.out.println(decryptDES("FbWfZtyPPOY="));
			System.out.println(encryptDES("gitsea"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String encryptDES(String encryptString)
			throws Exception {
		IvParameterSpec zeroIv = new IvParameterSpec(iv.getBytes());
		SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, skey, zeroIv);
		byte[] encryptedData = cipher.doFinal(encryptString.getBytes());

		return Base64.encode(encryptedData);
	}

	public static String decryptDES(String decryptString)
			throws Exception {
		byte[] byteMi = Base64.decode(decryptString);
		IvParameterSpec zeroIv = new IvParameterSpec(iv.getBytes());
		SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, skey, zeroIv);
		byte decryptedData[] = cipher.doFinal(byteMi);
		return new String(decryptedData);
	}
}

3.java 服务端代码同android
4.object-c 实现

//
//  Encrypt.m
//  GodReplyMe
//
//  Created by Frank Wong on 4/4/14.
//  Copyright (c) 2014  All rights reserved.
//

#import "Encrypt.h"
#import "Base64.h"
#import "CommonCrypto/CommonDigest.h"
#import "CommonCrypto/CommonCryptor.h"

@implementation Encrypt

const NSString *key = @"20140401";
const NSString *iv = @"12345678";

+(NSString *) encryptUseDES:(NSString *)plainText
{
    NSData* ivData = [iv dataUsingEncoding: NSUTF8StringEncoding];
    Byte *ivBytes = (Byte *)[ivData bytes];
    NSString *ciphertext = nil;
    NSData *textData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [textData length];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [key UTF8String], kCCKeySizeDES,
                                          ivBytes,
                                          [textData bytes], dataLength,
                                          buffer, 1024,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        ciphertext = [Base64 encode:data];
    }
    return ciphertext;
}

+(NSString *)decryptUseDES:(NSString *)cipherText
{
    NSData* ivData = [iv dataUsingEncoding: NSUTF8StringEncoding];
    Byte *ivBytes = (Byte *)[ivData bytes];
    NSString *plaintext = nil;
    NSData *cipherdata = [Base64 decode:cipherText];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [key UTF8String], kCCKeySizeDES,
                                          ivBytes,
                                          [cipherdata bytes], [cipherdata length],
                                          buffer, 1024,
                                          &numBytesDecrypted);
    if(cryptStatus == kCCSuccess) {
        NSData *plaindata = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
        plaintext = [[NSString alloc]initWithData:plaindata encoding:NSUTF8StringEncoding];
    }
    return plaintext;
}
@end
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
*