DES简介:
package com.v1.linxun.portal.utils;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
* 3DES加密工具类
*/
public class Des3Util {
// 密钥 长度不得小于24
private final static String secretKey = "123456789012345678901234" ;
// 向量 可有可无 终端后台也要约定
private final static String iv = "01234567";
// 加解密统一使用的编码方式
private final static String encoding = "utf-8";
/**
* 3DES加密
*
* @param plainText
* 普通文本
* @return
* @throws Exception
*/
public static String encode(String plainText) throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(secretKey .getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede");
deskey = keyfactory.generateSecret( spec);
Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec( iv.getBytes());
cipher.init(Cipher. ENCRYPT_MODE, deskey, ips);
byte[] encryptData = cipher.doFinal( plainText.getBytes( encoding));
return Base64. encode( encryptData);
}
/**
* 3DES解密
*
* @param encryptText
* 加密文本
* @return
* @throws Exception
*/
public static String decode(String encryptText) throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec( secretKey.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede");
deskey = keyfactory. generateSecret( spec);
Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding" );
IvParameterSpec ips = new IvParameterSpec( iv.getBytes());
cipher. init(Cipher. DECRYPT_MODE, deskey, ips);
byte[] decryptData = cipher. doFinal(Base64. decode(encryptText ));
return new String( decryptData, encoding);
}
public static void main(String args[]) throws Exception{
String str = "你好" ;
System. out.println( "----加密前-----:" + str );
String encodeStr = Des3Util. encode( str);
System. out.println( "----加密后-----:" + encodeStr );
System. out.println( "----解密后-----:" + Des3Util.decode( encodeStr));
}
}
|
Android版:
package com.inn.test;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import android.util.Base64;
/**
* Android 3DES加密工具类
*/
public class AndroidDes3Util {
// 密钥 长度不得小于24
private final static String secretKey = "123456789012345678901234" ;
// 向量 可有可无 终端后台也要约定
private final static String iv = "01234567" ;
// 加解密统一使用的编码方式
private final static String encoding = "utf-8" ;
/**
* 3DES加密
*
* @param plainText
* 普通文本
* @return
* @throws Exception
*/
public static String encode(String plainText) throws Exception {
Key deskey = null ;
DESedeKeySpec spec = new DESedeKeySpec(secretKey .getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec( iv.getBytes());
cipher.init(Cipher. ENCRYPT_MODE , deskey, ips);
byte [] encryptData = cipher.doFinal(plainText.getBytes(encoding ));
return Base64.encodeToString(encryptData,Base64. DEFAULT );
}
/**
* 3DES解密
*
* @param encryptText
* 加密文本
* @return
* @throws Exception
*/
public static String decode(String encryptText) throws Exception {
Key deskey = null ;
DESedeKeySpec spec = new DESedeKeySpec( secretKey.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede" );
deskey = keyfactory. generateSecret(spec);
Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding" );
IvParameterSpec ips = new IvParameterSpec( iv.getBytes());
cipher. init(Cipher. DECRYPT_MODE, deskey, ips);
byte [] decryptData = cipher.doFinal(Base64. decode(encryptText, Base64. DEFAULT));
return new String (decryptData, encoding);
}
}
|
// // DES3EncryptUtil.h // DES3加解密工具 // // Created by xc on 15/12/18. // Copyright © 2015年 xc. All rights reserved. // #import <Foundation/Foundation.h> @interface DES3EncryptUtil : NSObject // 加密方法 // 解密方法 @end |
// // DES3EncryptUtil.m // DES3加解密工具 // Created by xc on 15/12/18. // Copyright © 2015年 xc. All rights reserved. #import <Foundation/Foundation.h> //秘钥 @implementation DES3EncryptUtil : NSObject // 加密方法 // 解密方法 @end |
// // CommonFunc.h // PRJ_base64 // // Created by wangzhipeng on 12-11-29. // Copyright (c) 2012年 com.comsoft. All rights reserved. // #import <Foundation/Foundation.h> #define __BASE64( text ) [CommonFunc base64StringFromText:text] @interface MyBase64 : NSObject /****************************************************************************** /****************************************************************************** /****************************************************************************** /****************************************************************************** @end |
// #import "MyBase64.h" //引入IOS自带密码库 //空字符串 static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @implementation MyBase64 : NSObject + (NSString *)base64StringFromText:(NSString *)text + (NSString *)textFromBase64String:(NSString *)base64 /****************************************************************************** /****************************************************************************** /****************************************************************************** /****************************************************************************** @end |