三重Des对称加密在Android、Ios 和Java 平台的实现
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 |
三重Des对称加密在Android、Ios 和Java 平台的实现的更多相关文章
- Java和.NET使用DES对称加密的区别
Java和.NET的系统类库里都有封装DES对称加密的实现方式,但是对外暴露的接口却各不相同,甚至有时会让自己难以解决其中的问题,比如Java加密后的结果在.NET中解密不出来等,由于最近项目有跨Ja ...
- .NET中的DES对称加密
DES是一种对称加密(Data Encryption Standard)算法,于1977年得到美国政府的正式许可,是一种用56位密钥来加密64位数据的方法.一般密码长度为8个字节,其中56位加密密钥, ...
- pyDes 实现 Python 版的 DES 对称加密/解密--转
https://my.oschina.net/leejun2005/blog/586451 手头有个 Java 版的 DES 加密/解密程序,最近想着将其 Python 重构下,方便后续脚本解析,捣鼓 ...
- .NET和JAVA同等加密方法,MD5和DES对称加密记录
C#版: using System; using System.Security.Cryptography; using System.Text; namespace ConsoleApplicati ...
- 对称加密详解,以及JAVA简单实现
(原) 常用的加密有3种 1.正向加密,如MD5,加密后密文固定,目前还没办法破解,但是可以能过数据库撞库有一定概率找到,不过现在一般用这种方式加密都会加上盐值. 2.对称加密,通过一个固定的对称密钥 ...
- 3DES在Android、Ios 和Java 平台的加密解密
DES简介: DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法, 算法的入口参数有三个:Key.Data.Mode. K ...
- DES跨(C# Android IOS)三个平台通用的加解密方法
#region 跨平台加解密(c# 安卓 IOS) // public static string sKey = "12345678"; ...
- C# DES对称加密解密
/// <summary> /// 加密 /// </summary> /// <param name="str"></param> ...
- DES对称加密
DES是对称性加密里面常见一种,全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法.密钥长度是64位(bit),超过位数密钥被忽略.所谓对称性加密,加密 ...
随机推荐
- Creating an generated Earth AVI with C++
Creating an generated Earth AVI with C++ EarthGenerator.cpp /* EarthGenerator.cpp An examp ...
- WEB前端研发工程师编程能力成长之路(2)
四.[入微] 最强解决方案.你能够走在需求的前面,将当前需求里有的.没有直接提出来的.现在暂时没有但将来可能有的等等,及前端编程潜规则等各个方方面面都综合考虑,给出最优方案.以一招胜万招. var s ...
- git命令(待补充)
git log查看历史 git log -p -2 -p选项表示显示每次提交的内容差异,-2表示最近两次的更新
- python xpath 中的全部用法
不好意思 ,太仓促只能给你们个url 链接:https://blog.csdn.net/hhtnan/article/details/77509549
- Winter-1-A A + B 解题报告及测试数据
Time Limit:1000MS Memory Limit:32768KB Description Calculate A + B. Input Each line will contain two ...
- rdesktop install notes
在centos7上安装rdesktop来访问windows桌面,需要安装EPEL源,另外还有需要安装专门的YUM源. rpm -Uvh https://dl.fedoraproject.org/pub ...
- Docker+.Net Core 的那些事儿-1.准备工作
1.下载centos 地址:https://www.centos.org/download/ 我使用的是DVD ISO,这么做的目的是为了在之后的docker填坑的路上,方便使用centos中Fire ...
- Maven 中央仓库搭建
Maven中央仓库搭建 搭建系统:Linux Centos 7.4 x64 安装环境:JDK1.8.maven3.5.4.nexus-3.13 下载:nexus-3.13.0-01-unix.tar. ...
- logstash运输器以及kibana的更多操作
为了达到不会因为ELK中的某一项组件因为故障而导致整个ELK工作出问题,于是 将logstash收集到的数据存入到消息队列中如redis,rabbitMQ,activeMQ或者kafka,这里以red ...
- 20145301 《Java程序设计》第八周学习总结
20145301 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章部分 - 通用API 日志API 日志: 日志对信息安全意义重大,审计.取证.入侵检测等都会用到日志信息 L ...