先简单说一下什么是DES加密:
DES加密是一种对称加密方式。所谓对称加密就是加密与解密使用同一个密钥。其特点就是数据加密速度快,适用于加密大量数据的场合
如银行、保险等行业产生的大量数据
DES加密方法:
具体参考百度(http://baike.baidu.com/link?url=zlqNLErACiJ1MF3mA1rmIqVyJjjDOaxf0tM4ViBH2pNqPA48JHL-8YuuPyfBf4VMHQZThVsiBKhtowgCNxMSfK),老实说,我看了好几遍,也处于理解与不理解的漩涡中。
好吧,进入主题,DES加密算法的代码
iOS:
//加密
+ (NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
{
NSData *data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
unsigned char buffer[1024];
memset(buffer, 0, sizeof(char));
Byte iv[] = {1,2,3,4,5,6,7,8};
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
kCCOptionPKCS7Padding|kCCOptionECBMode,
[key UTF8String], kCCKeySizeDES,
iv,
[data bytes], [data length],
buffer, 1024,
&numBytesEncrypted);
NSString *ciphertext = nil;
if (cryptStatus == kCCSuccess) {
NSData *data = [[NSData alloc] initWithBytes:buffer length:numBytesEncrypted];
ciphertext = [[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding];
}
return ciphertext;
}
//解密
+ (NSString *) decryptUseDES:(NSString*)cipherText1 key:(NSString*)key
{
NSData *cipherData = [[NSData alloc] initWithData:[GTMBase64 decodeString:cipherText1]];
unsigned char buffer[1024];
memset(buffer, 0, sizeof(char));
size_t numBytesDecrypted = 0;
Byte iv[] = {1,2,3,4,5,6,7,8};
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding|kCCOptionECBMode,
[key UTF8String],
kCCKeySizeDES,
iv,
[cipherData bytes],
[cipherData length],
buffer,
1024,
&numBytesDecrypted);
NSString *plainText1 = nil;
if (cryptStatus == kCCSuccess) {
NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
plainText1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
return plainText1;
}
PS:GTMBase64是一个第三方库,去网上搜一下即可找到,使用时请在GTMBase64.h文件中添加#import <CommonCrypto/CommonCryptor.h>,否则可能会报错,其中有关base64编码解码的问题请参考上一篇文章http://blog.csdn.net/jie863230900/article/details/49640759
Andriod:
package com.rtdl.managedriverlib.utils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* @类描述:des加密
*/
public class DesUtils3 {
static String DES = "DES";
static String encode = "UTF-8";//保持平台兼容统一使用utf-8
private static DesUtils3 utils = null;
public static DesUtils3 getInstance(){
if(utils==null){
utils = new DesUtils3();
}
return utils;
}
// 加密:DES加密,加密之后通过BASE64进行编码
public String DESEncrypt(String StringToEncypt,String ScrectKey) throws Exception{
String dataWithBase64 = Des_Base64.encode(encryptData(StringToEncypt, ScrectKey));
return this.getStringWithReplace(dataWithBase64);
}
//加密:执行DES加密操作
private byte [] encryptData(String StringToEncypt,String ScrectKey) throws Exception {
byte[] bytesToEncypt = StringToEncypt.getBytes(encode);
SecretKey key = this.getKey(ScrectKey);
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(bytesToEncypt);
}
//解密:先进行BASE64解码,然后得到的密文通过DES方式解密
public String DESDecrypt(String StringWithEncrypt,String ScrectKey) throws Exception {
byte[] decryptedData = decryptData(Des_Base64.decode(StringWithEncrypt), ScrectKey);
String textDecrypt = new String(decryptedData,encode);
return textDecrypt;
}
// 解密:执行DES解密操作
private byte [] decryptData(byte[] BytesWithEncrypt,String strKey) throws Exception {
SecretKey key = this.getKey(strKey);
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(BytesWithEncrypt);
}
// 工具:根据秘钥字符串,得到秘钥
public SecretKey getKey(String strKey) throws Exception {
DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey sk = keyFactory.generateSecret(desKeySpec);
return sk;
}
public String getStringWithReplace(String StringWithBase64){
String result = StringWithBase64.replace("+", "%2B");
return result.replace("/", "%2F");
}
}
由于楼主并不是搞Andriod的,所以对Andriod这不是很懂,但是这块已经和iOS核对过了,应该没有问题。