|
| 1 | +/* |
| 2 | + * Copyright 2017 [AllenCoderr] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.allen.apputils; |
| 18 | + |
| 19 | + |
| 20 | +import java.io.UnsupportedEncodingException; |
| 21 | +import java.security.NoSuchAlgorithmException; |
| 22 | +import javax.crypto.Cipher; |
| 23 | +import javax.crypto.NoSuchPaddingException; |
| 24 | +import javax.crypto.SecretKey; |
| 25 | +import javax.crypto.spec.SecretKeySpec; |
| 26 | + |
| 27 | +/** |
| 28 | + * 3DES 加密/解密 |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +/** |
| 35 | + * SecretUtils {3DES加密解密的工具类 } |
| 36 | +
|
| 37 | + */ |
| 38 | +public class SecretUtils { |
| 39 | + |
| 40 | + //定义加密算法,有DES、DESede(即3DES)、Blowfish |
| 41 | + private static final String Algorithm = "DESede"; |
| 42 | + private static final String PASSWORD_CRYPT_KEY = "2017abcdefghijklmnopQrst123"; |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * 加密方法 |
| 47 | + * @param src 源数据的字节数组 |
| 48 | + * @return |
| 49 | + */ |
| 50 | + public static byte[] encryptMode(byte[] src) { |
| 51 | + try { |
| 52 | + SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm); //生成密钥 |
| 53 | + Cipher c1 = Cipher.getInstance(Algorithm); //实例化负责加密/解密的Cipher工具类 |
| 54 | + c1.init(Cipher.ENCRYPT_MODE, deskey); //初始化为加密模式 |
| 55 | + return c1.doFinal(src); |
| 56 | + } catch (java.security.NoSuchAlgorithmException e1) { |
| 57 | + e1.printStackTrace(); |
| 58 | + } catch (javax.crypto.NoSuchPaddingException e2) { |
| 59 | + e2.printStackTrace(); |
| 60 | + } catch (java.lang.Exception e3) { |
| 61 | + e3.printStackTrace(); |
| 62 | + } |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * 解密函数 |
| 69 | + * @param src 密文的字节数组 |
| 70 | + * @return |
| 71 | + */ |
| 72 | + public static byte[] decryptMode(byte[] src) { |
| 73 | + try { |
| 74 | + SecretKey deskey = new SecretKeySpec(build3DesKey(PASSWORD_CRYPT_KEY), Algorithm); |
| 75 | + Cipher c1 = Cipher.getInstance(Algorithm); |
| 76 | + c1.init(Cipher.DECRYPT_MODE, deskey); //初始化为解密模式 |
| 77 | + return c1.doFinal(src); |
| 78 | + } catch (java.security.NoSuchAlgorithmException e1) { |
| 79 | + e1.printStackTrace(); |
| 80 | + } catch (javax.crypto.NoSuchPaddingException e2) { |
| 81 | + e2.printStackTrace(); |
| 82 | + } catch (java.lang.Exception e3) { |
| 83 | + e3.printStackTrace(); |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + /* |
| 90 | + * 根据字符串生成密钥字节数组 |
| 91 | + * @param keyStr 密钥字符串 |
| 92 | + * @return |
| 93 | + * @throws UnsupportedEncodingException |
| 94 | + */ |
| 95 | + public static byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException{ |
| 96 | + byte[] key = new byte[24]; //声明一个24位的字节数组,默认里面都是0 |
| 97 | + byte[] temp = keyStr.getBytes("UTF-8"); //将字符串转成字节数组 |
| 98 | + |
| 99 | + /* |
| 100 | + * 执行数组拷贝 |
| 101 | + * System.arraycopy(源数组,从源数组哪里开始拷贝,目标数组,拷贝多少位) |
| 102 | + */ |
| 103 | + if(key.length > temp.length){ |
| 104 | + //如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中 |
| 105 | + System.arraycopy(temp, 0, key, 0, temp.length); |
| 106 | + }else{ |
| 107 | + //如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中 |
| 108 | + System.arraycopy(temp, 0, key, 0, key.length); |
| 109 | + } |
| 110 | + return key; |
| 111 | + } |
| 112 | +} |
0 commit comments