ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:19.96KB ,
资源ID:7357451      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-7357451.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java的加密解密.docx)为本站会员(b****5)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

java的加密解密.docx

1、java的加密解密bytealgorithm加密string解密importimport java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.security.spec.InvalidKeySpecException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.Ille

2、galBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;public class DESEncryptTest private static final String DES_ALGORITHM = DES; /* * DES加密 * param pla

3、inData * param secretKey * return * throws Exception */ public String encryption(String plainData, String secretKey) throws Exception Cipher cipher = null; try cipher = Cipher.getInstance(DES_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, generateKey(secretKey); catch (NoSuchAlgorithmException e) e.pr

4、intStackTrace(); catch (NoSuchPaddingException e) e.printStackTrace(); catch(InvalidKeyException e) try / 为了防止解密时报javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher异常, / 不能把加密后的字节数组直接转换成字符串 byte buf = cipher.doFinal(plainData.getBytes(); ret

5、urn Base64Utils.encode(buf); catch (IllegalBlockSizeException e) e.printStackTrace(); throw new Exception(IllegalBlockSizeException, e); catch (BadPaddingException e) e.printStackTrace(); throw new Exception(BadPaddingException, e); /* * DES解密 * param secretData * param secretKey * return * throws E

6、xception */ public String decryption(String secretData, String secretKey) throws Exception Cipher cipher = null; try cipher = Cipher.getInstance(DES_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, generateKey(secretKey); catch (NoSuchAlgorithmException e) e.printStackTrace(); throw new Exception(NoSuch

7、AlgorithmException, e); catch (NoSuchPaddingException e) e.printStackTrace(); throw new Exception(NoSuchPaddingException, e); catch(InvalidKeyException e) e.printStackTrace(); throw new Exception(InvalidKeyException, e); try byte buf = cipher.doFinal(Base64Utils.decode(secretData.toCharArray(); retu

8、rn new String(buf); catch (IllegalBlockSizeException e) e.printStackTrace(); throw new Exception(IllegalBlockSizeException, e); catch (BadPaddingException e) e.printStackTrace(); throw new Exception(BadPaddingException, e); /* * 获得秘密密钥 * * param secretKey * return * throws NoSuchAlgorithmException *

9、/ private SecretKey generateKey(String secretKey) throws NoSuchAlgorithmException SecureRandom secureRandom = new SecureRandom(secretKey.getBytes(); / 为我们选择的DES算法生成一个KeyGenerator对象 KeyGenerator kg = null; try kg = KeyGenerator.getInstance(DES_ALGORITHM); catch (NoSuchAlgorithmException e) kg.init(se

10、cureRandom); /kg.init(56, secureRandom); / 生成密钥 return kg.generateKey(); public static void main(String a) throws Exception String input = cy11Xlbrmzyh:604:301:1353064296; String key = 37d5aed075525d4fa0fe635231cba447; DESEncryptTest des = new DESEncryptTest(); String result = des.encryption(input,

11、key); System.out.println(result); System.out.println(des.decryption(result, key); static class Base64Utils static private char alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=.toCharArray(); static private byte codes = new byte256; static for (int i = 0; i 256; i+) codesi

12、 = -1; for (int i = A; i = Z; i+) codesi = (byte) (i - A); for (int i = a; i = z; i+) codesi = (byte) (26 + i - a); for (int i = 0; i = 9; i+) codesi = (byte) (52 + i - 0); codes+ = 62; codes/ = 63; /* * 将原始数据编码为base64编码 */ static public String encode(byte data) char out = new char(data.length + 2)

13、/ 3) * 4; for (int i = 0, index = 0; i data.length; i += 3, index += 4) boolean quad = false; boolean trip = false; int val = (0xFF & (int) datai); val = 8; if (i + 1) data.length) val |= (0xFF & (int) datai + 1); trip = true; val = 8; if (i + 2) = 6; outindex + 2 = alphabet(trip ? (val & 0x3F) : 64

14、); val = 6; outindex + 1 = alphabetval & 0x3F; val = 6; outindex + 0 = alphabetval & 0x3F; return new String(out); /* * 将base64编码的数据解码成原始数据 */ static public byte decode(char data) int len = (data.length + 3) / 4) * 3; if (data.length 0 & datadata.length - 1 = =) -len; if (data.length 1 & datadata.le

15、ngth - 2 = =) -len; byte out = new bytelen; int shift = 0; int accum = 0; int index = 0; for (int ix = 0; ix = 0) accum = 8) shift -= 8; outindex+ = (byte) (accum shift) & 0xff); if (index != out.length) throw new Error(miscalculated data length!); return out; KKKKkkkkKKKKKKKKKKKKKKK/*在java中调用sun公司提

16、供的3DES加密解密算法时,需要使用到$JAVA_HOME/jre/lib/目录下如下的4个jar包:jce.jarsecurity/US_export_policy.jarsecurity/local_policy.jarext/sunjce_provider.jar Java运行时会自动加载这些包,因此对于带main函数的应用程序不需要设置到CLASSPATH环境变量中。对于WEB应用,不需要把这些包加到WEB-INF/lib目录下。 以下是java中调用sun公司提供的3DES加密解密算法的样本代码: */package com.aaa.aaa;import java.security.

17、*;import javax.crypto.*;import javax.crypto.spec.SecretKeySpec;public class ThreeDES private static final String Algorithm = DESede; / 定义 加密算法,可用/ DES,DESede,Blowfishprivate static final byte keyBase = 0x11, 0x22, 0x4F, 0x58,(byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB,(byte) 0

18、xDD, 0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40,0x36, (byte) 0xE2 ; / 24个byte的密钥/ keybyte为加密密钥,长度为24字节/ src为被加密的数据缓冲区(源)/* * 加密 */public static byte encryptMode(byte src) try / 生成密钥SecretKey deskey = new SecretKeySpec(keyBase, Algorithm);/ 加密Cipher c1 = Cipher.getInstance(Algorithm);c1.in

19、it(Cipher.ENCRYPT_MODE, deskey);return c1.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;/ keybyte为加密密钥,长度为24字节/ src为加密后的缓冲区publ

20、ic static byte decryptMode(byte src) try / 生成密钥SecretKey deskey = new SecretKeySpec(keyBase, Algorithm);/ 解密Cipher c1 = Cipher.getInstance(Algorithm);c1.init(Cipher.DECRYPT_MODE, deskey);return c1.doFinal(src); catch (java.security.NoSuchAlgorithmException e1) e1.printStackTrace(); catch (javax.cryp

21、to.NoSuchPaddingException e2) e2.printStackTrace(); catch (java.lang.Exception e3) e3.printStackTrace();return null;/ 转换成十六进制字符串public static String byte2hex(byte b) String hs = ;String stmp = ;for (int n = 0; n b.length; n+) stmp = (java.lang.Integer.toHexString(bn & 0XFF);if (stmp.length() = 1)hs

22、= hs + 0 + stmp;elsehs = hs + stmp;if (n b.length - 1)hs = hs + :;return hs.toUpperCase();public static void main(String args) / 添加新安全算法,如果用JCE就要把它添加进去Security.addProvider(new com.sun.crypto.provider.SunJCE();final byte keyBytes = 0x11, 0x22, 0x4F, 0x58, (byte) 0x88, 0x10,0x40, 0x38, 0x28, 0x25, 0x7

23、9, 0x51, (byte) 0xCB, (byte) 0xDD,0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40, 0x36,(byte) 0xE2 ;/ 24字节的密钥String szSrc = This is a 3DES test. 测试;System.out.println(加密前的字符串: + szSrc);byte encoded = encryptMode(szSrc.getBytes();System.out.println(加密后的字符串: + new String(encoded);byte srcBytes

24、= decryptMode(encoded);System.out.println(解密后的字符串: + (new String(srcBytes);dddddddddddddddddddddpackage com.snailteam.adserver.until;import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;im

25、port javax.crypto.*;import javax.crypto.spec.SecretKeySpec;/* * author :xiaofancn * version :2011-11-11 上午11:02:22 * */ public class Tool3DES private static final String Algorithm = DESede; / 定义 加密算法,可用 private static final int Keysize = 168; /* * 将byte转化成16进制字符串 * param buf * return */ public stati

26、c String parseByte2HexStr(byte buf) StringBuffer sb = new StringBuffer(); for (int i = 0; i buf.length; i+) String hex = Integer.toHexString(bufi & 0xFF); if (hex.length() = 1) hex = 0 + hex; sb.append(hex.toUpperCase(); return sb.toString(); /* * 将16进制字符串转化成byte * param buf * return */ public static byte parseHexStr2Byte(String hexStr) if (hexStr.length() 1) return null; byte result = new bytehexStr.length() / 2; for (int i = 0; i hexStr.length() / 2; i+) int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2