TripleDES加解密Java、C#、php通用代码
package com.jaamy.common.util; import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64; public class DefaultEncryptor { private static final String ALGORITHM = "DESede"; //默认为 DESede/ECB/PKCS5Padding
private static final String CIPHER_TRANSFORMAT = "DESede/ECB/PKCS5Padding"; private static final String ENCODING = "UTF-8"; public static String encryptToBase64(String plainText, String key) throws Exception {
SecretKey deskey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher c1 = Cipher.getInstance(CIPHER_TRANSFORMAT);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] result = c1.doFinal(plainText.getBytes(ENCODING));
return Base64.encodeBase64String(result);
} public static String decryptFromBase64(String base64, String key) throws Exception {
SecretKey deskey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher c1 = Cipher.getInstance(CIPHER_TRANSFORMAT);
c1.init(Cipher.DECRYPT_MODE, deskey);
byte[] result = c1.doFinal(Base64.decodeBase64(base64));
return new String(result, ENCODING);
}
}
C#版本:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web; namespace DesEncryptor
{
class DefaultEncryptor
{
#region DES 加密
public static string encryptToBase64(string plainText, string key)
{
try
{
// Create a MemoryStream.
MemoryStream mStream = new MemoryStream();
TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();
tripleDESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(key);
tripleDESCryptoServiceProvider.Padding = PaddingMode.PKCS7;//补位
tripleDESCryptoServiceProvider.Mode = CipherMode.ECB;//CipherMode.CBC
CryptoStream cStream = new CryptoStream(mStream,
tripleDESCryptoServiceProvider.CreateEncryptor(),
CryptoStreamMode.Write); // Convert the passed string to a byte array.
byte[] toEncrypt = Encoding.UTF8.GetBytes(plainText); // Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, , toEncrypt.Length);
cStream.FlushFinalBlock(); // Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
byte[] ret = mStream.ToArray(); // Close the streams.
cStream.Close();
mStream.Close(); // Return the encrypted buffer.
return Convert.ToBase64String(ret);
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
#endregion #region DES解密
public static string decryptFromBase64(string base64, string key)
{
try
{
byte[] inputByteArray = Convert.FromBase64String(base64);
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(inputByteArray); // Create a CryptoStream using the MemoryStream
TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();
tripleDESCryptoServiceProvider.Key = ASCIIEncoding.ASCII.GetBytes(key);
tripleDESCryptoServiceProvider.Padding = PaddingMode.PKCS7;//补位
tripleDESCryptoServiceProvider.Mode = CipherMode.ECB;//CipherMode.CBC
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
tripleDESCryptoServiceProvider.CreateDecryptor(),
CryptoStreamMode.Read); // Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[inputByteArray.Length]; // Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, , fromEncrypt.Length); //Convert the buffer into a string and return it.
return Encoding.UTF8.GetString(fromEncrypt).TrimEnd('\0');
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
} }
#endregion
}
}
php版本:
<?php
class CryptDes { function encrypt($input, $key){
$size = mcrypt_get_block_size(MCRYPT_3DES,MCRYPT_MODE_ECB);
$input = $this->pkcs5_pad($input, $size);
$key = str_pad($key,24,'0');
$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
@mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
} function decrypt($encrypted, $key){
$encrypted = base64_decode($encrypted);
$key = str_pad($key,24,'0');
$td = mcrypt_module_open(MCRYPT_3DES,'',MCRYPT_MODE_ECB,'');
$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$ks = mcrypt_enc_get_key_size($td);
@mcrypt_generic_init($td, $key, $iv);
$decrypted = mdecrypt_generic($td, $encrypted);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$y=$this->pkcs5_unpad($decrypted);
return $y;
} function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
} function pkcs5_unpad($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);
}
} $des = new CryptDes();
$encrypt_str = $des->encrypt("1111","012345678901234567890123");//加密字符串
echo $encrypt_str."\n";
echo $decrypt_str = $des->decrypt($encrypt_str,"012345678901234567890123");//解密字符串
?>
TripleDES加解密Java、C#、php通用代码的更多相关文章
- TripleDES加解密Java、C#、php
TripleDES说明: TripleDES(3Des)和Des都是对称加密算法,TripleDes是Des加密算法的增强版本,这里主要说的是TripleDes加密算法的应用. 工作中 ...
- Rsa加解密Java、C#、php通用代码 密钥转换工具
之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...
- 自己实现简单的RSA秘钥生成与加解密(Java )
最近在学习PKI,顺便接触了一些加密算法.对RSA着重研究了一下,自己也写了一个简单的实现RSA算法的Demo,包括公.私钥生成,加解密的实现.虽然比较简单,但是也大概囊括了RSA加解密的核心思想与流 ...
- Des加解密(Java端和Js端配套)解析
一.什么是DES加密 des对称加密,对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码( ...
- PHP RSA加解密详解(附代码)
前言:RSA加密一般用在涉及到重要数据时所使用的加密算法,比如用户的账户密码传输,订单的相关数据传输等. 加密方式说明:公钥加密,私钥解密.也可以 私钥加密,公钥解密 一.RSA简介 RSA公钥加密 ...
- SM2的非对称加解密java工具类
maven依赖 <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov- ...
- AES 加解密 Java实现
package com.smt.cipher.symmetry; import java.nio.charset.Charset; import java.security.SecureRandom; ...
- Java 窗体居中 通用代码
Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包 Dimension screenSize = kit.getScreenSize() ...
- 加解密---Java安全
一.概述 1.JCA(Java Cryptography Architecture) 提供基本的加密框架(消息摘要.数字签名......) 2.JCE(Java Cryptography Extens ...
随机推荐
- C#中双问号、双冒号等几个特殊关键字
1.@ 这个东东看似和邮件有关啊,但是在C#的世界里,可跟邮件没有一毛钱关系,它是string的女朋友(当然了string有N多女友),二者结合就可以发挥作用了.你可以给它起个名字,叫做“逐字字符串” ...
- [wxWidgets] 1. 安装及"hello world"程序
关于wxWidgets的优越已经在它的官方网站有所阐述,本文不再赘述. 本系列主要记录学习这个软件包过程中遇到的问题以及心得. 1. 安装 从源码安装虽然大多时候不是一件轻松的过程,但是基于以下两个 ...
- C# WinForm 禁止最大化、最小化、双击标题栏、双击图标等操作(转载)
protected override void WndProc(ref Message m) { if (m.Msg==0x112) { switch ((int) m.WParam) { //禁止双 ...
- 【学】jQuery的源码思路1——后代选择器
jQuery的源码思路1--后代选择器 这里探讨一下jQuery中后代选择器的封装原理,并自己写一下 getEle('#div1 ul li .box');接受的参数就是个后代选择器,类似于这样: # ...
- ( C++) Access the hard drive.
// Open up the volume HANDLE hVolume = CreateFile(wstrPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHA ...
- 最新版STS因为JDK版本太低无法启动的解决办法
-startup plugins/org.eclipse.equinox.launcher_1..jar --launcher.library plugins/org.eclipse.equinox. ...
- Oracle补习班第八天
The best hearts are always the bravest. 心灵最高尚的人,也总是最勇敢的人. 1,权限.角色.与用户 创建用户 create user aa identified ...
- Drools mvel方言drl断点调试方法
开发环境:myeclipse2014, jdk1.8.0.91,drools6.4.0.Final, drools-eclipse-plugin,mvel2-2.2.6.Final问题描述:drl使 ...
- Python多线程join的用法
import threading, time def Myjoin(): print 'hello world!' time.sleep(1) for i in range(5): t=threadi ...
- DbEntry.Net.v3.5 快速教程
1.DbEntry 介绍 EN&Download——[DbEntry Framework下载][Tutorials For Version 3.5] CN&Summary:总体特性的介 ...