public class Security3DES { private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish /** * @Title: encryptData * @Description: 加密 * @param cryptKey 密钥 * @param src * @return */ public static byte[] encryptData(String cryptKey, byte[] src) { try { //添加新安全算法,如果用JCE就要把它添加进去 //Security.addProvider(new com.sun.crypto.provider.SunJCE()); SecretKey deskey = new SecretKeySpec(build3DesKey(cryptKey), Algorithm); Cipher cip = Cipher.getInstance(Algorithm); cip.init(Cipher.ENCRYPT_MODE, deskey); return cip.doFinal(src); } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } /** * @Title: decryptData * @Description: 解密 * @param cryptKey 密钥 * @param src * @return */ public static byte[] decryptData(String cryptKey, byte[] src) { try { SecretKey deskey = new SecretKeySpec(build3DesKey(cryptKey),Algorithm); Cipher cip = Cipher.getInstance(Algorithm); cip.init(Cipher.DECRYPT_MODE, deskey); return cip.doFinal(src); } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } /** * @Title: build3DesKey * @Description: 将key转为24位byte * @param keyStr * @return * @throws UnsupportedEncodingException */ public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException { byte[] key = new byte[24]; byte[] temp = keyStr.getBytes("UTF-8"); if (key.length > temp.length) { System.arraycopy(temp, 0, key, 0, temp.length); } else { System.arraycopy(temp, 0, key, 0, key.length); } return key; } public static void main(String[] args){ try { String keyBytes = "012345678901234567890123"; String szSrc = "This is a 3DES test. 测试"; System.out.println("加密前的字符串:" + szSrc); byte[] encoded = encryptData(keyBytes, szSrc.getBytes()); BASE64Encoder enc = new BASE64Encoder(); String cipherString = enc.encode(encoded); System.out.println("加密后的字符串:" + cipherString); BASE64Decoder base64 = new BASE64Decoder(); byte[] byStr = base64.decodeBuffer(cipherString); byte[] srcBytes = decryptData(keyBytes, byStr); System.out.println("解密后的字符串:" + (new String(srcBytes))); } catch (IOException e) { e.printStackTrace(); } }
相关推荐
网络安全DES算法,进行简单的3DES加密和解密操作
加密方法: com.linkage.security.SecurityManager.Encrypt3DES(content, key); 解密方法: com.linkage.security.SecurityManager.Decrypt3DES(content, key)
将bcprov-ext-jdk16-146.jar和bcprov-jdk16-146.jar放入安装目录/jre7/lib/ext目录和安装目录/jre7/lib/security目录下 打开JAVA_HOME/jre7/lib/security下的java.security文件,在下面加上 security.provider.x=...
A Roadmap for Readers and Instructors 3 Internet and Web Resources 4 Standards 5 Overview 7 Computer Security Concepts 9 The OSI Security Architecture 14 Security Attacks 15 Security Services 17 ...
security algorithms s des diffie helamn rsa rc4
本程序是java语言实现的DES加密算法的实例。
des code for criptography and security data
import java.security.spec.*; import javax.crypto.*; import javax.crypto.spec.*; class DES01 { private String strkey; private SecretKey skey=null; private String[] algo= {"DES/ECB/PKCS5Padding",...
MD5的全称是Message-Digest Algorithm 5,在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明,经MD2、MD3和MD4发展而来。 RSA是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很...
Security_04_密码技术-现代密码学-DES-智能信息安全.zip
最近一个项目中,因为服务端是用的java开发的,客户端是用的C#,由于通信部分采用到了3DES加密,所以做个记录,以备以后需要的时候直接用。 这是对方(java)的加密算法,和网上流传的代码也差不多(主密钥直接写...
以前的课设,拿上来分享。程序是Linux下的,GTK+的界面。压缩包里有源文件,GTK的.glade文件和编译好的程序。... * Company: the State Key Laboratory of Information Security * CAS, Beijing, China.
import java.security.*; public class EncryptionExample { protected String calg = "Blowfish"; // AES. DES, Blowfish protected int keyLen = 128; // 128 for AES, Blowfish, 64 for DES public ...
RSA+DES混合文件加密 RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。 RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最...
Network Security: Private Communication in a Public World, Second Edition By Charlie Kaufman, Radia Perlman, Mike Speciner ............................................... Publisher: Prentice Hall ...
本文实例讲述了C#实现简单的3DES加密解密功能。分享给大家供大家参考,具体如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; ...
Oracle Database 10g Security 安全策略和安全管理专著
Artur Hecker, Ecole Nationale Supérieure des Télécommunications (ENST), France Mohamad Badra, National Center for Scientific Research, France Chapter XIX Security Architectures for B3G Mobile ...
SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是...其它特点包括文本手稿、易于使用的工具条、用户的键位图编辑器、可定制的ANSI颜色等.SecureCRT的SSH协议支持DES,3DES和RC4密码和密码与RSA鉴别。
在C#中IDEA、DES、EAS加密算法的例子,很给力!