/** * Calculates keyed checksum. * @param data the data used to generate the checksum. * @param size length of the data. * @param key the key used to encrypt the checksum. * @return keyed checksum. * * @modified by Yanni Zhang, 12/08/99. */ public byte[] calculateKeyedChecksum(byte[] data, int size, byte[] key, int usage) throws KrbCryptoException { //check for weak keys try { if (DESKeySpec.isWeak(key, 0)) { key[7] = (byte)(key[7] ^ 0xF0); } } catch (InvalidKeyException ex) { // swallow, since it should never happen } byte[] ivec = new byte[key.length]; System.arraycopy(key, 0, ivec, 0, key.length); byte[] cksum = Des.des_cksum(ivec, data, key); return cksum; }
/** * 数据加密,算法(DES) * * @param data * 要进行加密的数据 * @param desKey DES密钥 * @return 加密后的数据 */ public static String encrypt(String data, byte[] desKey) { String encryptedData = null; try { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(desKey); // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(deskey); // 加密对象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key, sr); // 加密,并把字节数组编码成字符串 encryptedData = Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes())); } catch (Exception e) { // log.error("加密错误,错误信息:", e); throw new RuntimeException("加密错误,错误信息:", e); } return encryptedData; }
/** * 数据解密,算法(DES) * * @param cryptData * 加密数据 * @param desKey DES密钥 * @return 解密后的数据 */ public static String decrypt(String cryptData, byte[] desKey) { String decryptedData = null; try { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(desKey); // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(deskey); // 解密对象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key, sr); // 把字符串解码为字节数组,并解密 decryptedData = new String(cipher.doFinal(Base64.getDecoder().decode(cryptData))); } catch (Exception e) { // log.error("解密错误,错误信息:", e); throw new RuntimeException("解密错误,错误信息:", e); } return decryptedData; }
/** * DES算法,加密 * * @param data * 待加密字符串 * @param key * 加密私钥,长度不能够小于8位 * @return 加密后的字节数组,一般结合Base64编码使用 * @throws Exception */ public static String encode(String key, String data) { if (data == null) return null; try { DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // key的长度不能够小于8位字节 Key secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec("12345678".getBytes()); AlgorithmParameterSpec paramSpec = iv; cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); byte[] bytes = cipher.doFinal(data.getBytes()); return byte2String(bytes); } catch (Exception e) { e.printStackTrace(); return data; } }
/** * DES算法,加密 * * @param data 待加密字符串 * @param key 加密私钥,长度不能够小于8位 * @return 加密后的字节数组,一般结合Base64编码使用 * @throws InvalidAlgorithmParameterException * @throws Exception */ public static String encode(String key,String data) { if(data == null) return null; try{ DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //key的长度不能够小于8位字节 Key secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec(key.getBytes()); AlgorithmParameterSpec paramSpec = iv; cipher.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec); byte[] bytes = cipher.doFinal(data.getBytes()); return byte2hex(bytes); }catch(Exception e){ e.printStackTrace(); return data; } }
/** * DES算法,解密 * * @param data 待解密字符串 * @param key 解密私钥,长度不能够小于8位 * @return 解密后的字节数组 * @throws Exception 异常 */ public static String decode(String key,String data) { if(data == null) return null; try { DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //key的长度不能够小于8位字节 Key secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec(key.getBytes()); AlgorithmParameterSpec paramSpec = iv; cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); return new String(cipher.doFinal(hex2byte(data.getBytes()))); } catch (Exception e){ e.printStackTrace(); return data; } }
/** * 加密 * @param data * @param password * @return */ public static byte[] encrypt(byte[] data, String password) { try { SecureRandom random = new SecureRandom(); DESKeySpec desKey = new DESKeySpec(password.getBytes()); //创建一个密匙工厂,然后用它把DESKeySpec转换成 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); //Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES"); //用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, random); //现在,获取数据并加密 //正式执行加密操作 return cipher.doFinal(data); } catch (Throwable e) { e.printStackTrace(); } return null; }
/** * DES * @param src * @param password * @return */ public static byte[] decrypt(byte []src, String password) { try { // DES算法要求有一个可信任的随机数源 SecureRandom random = new SecureRandom(); // 创建一个DESKeySpec对象 DESKeySpec desKey = new DESKeySpec(password.getBytes()); // 创建一个密匙工厂 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 将DESKeySpec对象转换成SecretKey对象 SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, random); // 真正开始解密操作 return cipher.doFinal(src); } catch (Throwable e) { e.printStackTrace(); } return null; }
/** * DES加密 * * @author : chenssy * @date : 2016年5月20日 下午5:51:37 * * @param data * 待加密字符串 * @param key * 校验位 * @return */ @SuppressWarnings("restriction") protected static String encrypt(String data,String key) { String encryptedData = null; try { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(key.getBytes()); // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(deskey); // 加密对象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr); // 加密,并把字节数组编码成字符串 encryptedData = new sun.misc.BASE64Encoder().encode(cipher.doFinal(data.getBytes())); } catch (Exception e) { throw new RuntimeException("加密错误,错误信息:", e); } return encryptedData; }
/** * DES解密 * * @author : chenssy * @date : 2016年5月20日 下午5:52:23 * * @param cryptData * 待解密密文 * @param key * 校验位 * @return */ @SuppressWarnings("restriction") protected static String decrypt(String cryptData,String key) { String decryptedData = null; try { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(key.getBytes()); // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(deskey); // 解密对象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, secretKey, sr); // 把字符串解码为字节数组,并解密 decryptedData = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(cryptData))); } catch (Exception e) { throw new RuntimeException("解密错误,错误信息:", e); } return decryptedData; }
/** * DES加密 * * @param src * @param key * @return * @throws Exception */ public static byte[] encrypt(byte[] src, byte[] key) throws Exception { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密匙数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(DES); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); // 现在,获取数据并加密 // 正式执行加密操作 return cipher.doFinal(src); }
/** * DES解密 * * @param src * @param key * @return * @throws Exception */ public static byte[] decrypt(byte[] src, byte[] key) throws Exception { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密匙数据创建一个DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成 // 一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance(DES); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); // 现在,获取数据并解密 // 正式执行解密操作 return cipher.doFinal(src); }
public static void main(String[] args) throws Exception { boolean failed = false; for (int i = 0; i < weakKeys.length; i++) { DESKeySpec desSpec = new DESKeySpec(weakKeys[i]); if (!DESKeySpec.isWeak(weakKeys[i], 0)) { failed = true; System.out.println("Entry " + i + " should be weak"); } } if (failed) { throw new Exception("Failed test!!!"); } System.out.println("Passed test."); }
/** * Description 根据键值进行解密 */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
/** * Description 根据键值进行加密 * * @param data * @param key 加密键byte数组 * @return * @throws Exception */ private static byte[] desEncrypt(byte[] key, byte[] data) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
/** * Description 根据键值进行解密 * * @param data * @param key 加密键byte数组 * @return * @throws Exception */ private static byte[] desDecrypt(byte[] key, byte[] data) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
/** * 解密 * @param src byte[] * @param password String * @return byte[] * @throws Exception */ public static byte[] decrypt(byte[] src, String password) throws Exception { // DES算法要求有一个可信任的随机数源 SecureRandom random = new SecureRandom(); // 创建一个DESKeySpec对象 DESKeySpec desKey = new DESKeySpec(password.getBytes()); // 创建一个密匙工厂 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 将DESKeySpec对象转换成SecretKey对象 SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, random); // 真正开始解密操作 return cipher.doFinal(src); }
/** * Decrypts keyed checksum. * @param enc_cksum the buffer for encrypted checksum. * @param key the key. * @return the checksum. * * @modified by Yanni Zhang, 12/08/99. */ private byte[] decryptKeyedChecksum(byte[] enc_cksum, byte[] key) throws KrbCryptoException { byte[] new_key = new byte[keySize()]; System.arraycopy(key, 0, new_key, 0, key.length); for (int i = 0; i < new_key.length; i++) new_key[i] = (byte)(new_key[i] ^ 0xf0); //check for weak keys try { if (DESKeySpec.isWeak(new_key, 0)) { new_key[7] = (byte)(new_key[7] ^ 0xF0); } } catch (InvalidKeyException ex) { // swallow, since it should never happen } byte[] ivec = new byte[new_key.length]; byte[] cksum = new byte[enc_cksum.length]; Des.cbc_encrypt(enc_cksum, cksum, new_key, ivec, false); return cksum; }
/** * Verifies keyed checksum. * @param data the data. * @param size the length of data. * @param key the key used to encrypt the checksum. * @param checksum the checksum. * @return true if verification is successful. * * @modified by Yanni Zhang, 12/08/99. */ public boolean verifyKeyedChecksum(byte[] data, int size, byte[] key, byte[] checksum, int usage) throws KrbCryptoException { byte[] cksum = decryptKeyedChecksum(checksum, key); byte[] new_data = new byte[size + confounderSize()]; System.arraycopy(cksum, 0, new_data, 0, confounderSize()); System.arraycopy(data, 0, new_data, confounderSize(), size); //check for weak keys try { if (DESKeySpec.isWeak(key, 0)) { key[7] = (byte)(key[7] ^ 0xF0); } } catch (InvalidKeyException ex) { // swallow, since it should never happen } byte[] ivec = new byte[key.length]; byte[] new_cksum = Des.des_cksum(ivec, new_data, key); byte[] orig_cksum = new byte[cksumSize() - confounderSize()]; System.arraycopy(cksum, confounderSize(), orig_cksum, 0, cksumSize() - confounderSize()); return isChecksumEqual(orig_cksum, new_cksum); }
/** * DES加密 */ public static String desEncrypt(String source, String desKey) throws Exception { try { // 从原始密匙数据创建DESKeySpec对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(new DESKeySpec(desKey.getBytes())); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey); // 现在,获取数据并加密 byte[] destBytes = cipher.doFinal(source.getBytes()); StringBuilder hexRetSB = new StringBuilder(); for (byte b : destBytes) { String hexString = Integer.toHexString(0x00ff & b); hexRetSB.append(hexString.length() == 1 ? 0 : "").append(hexString); } return hexRetSB.toString(); } catch (Exception e) { throw new Exception("DES加密发生错误", e); } }
public void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Exception { DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey desKey = skf.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for // SunJCE if (mode == Cipher.ENCRYPT_MODE) { cipher.init(Cipher.ENCRYPT_MODE, desKey); CipherInputStream cis = new CipherInputStream(is, cipher); doCopy(cis, os); } else if (mode == Cipher.DECRYPT_MODE) { cipher.init(Cipher.DECRYPT_MODE, desKey); CipherOutputStream cos = new CipherOutputStream(os, cipher); doCopy(is, cos); } }
/** * 加密方法 * * @param rawKeyData * @param str * @return * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws NoSuchPaddingException * @throws InvalidKeySpecException */ public static byte[] encrypt(byte rawKeyData[], String str) throws InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, InvalidKeySpecException { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密匙数据创建一个DESKeySpec对象 DESKeySpec dks = new DESKeySpec(rawKeyData); // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, key, sr); // 现在,获取数据并加密 byte data[] = str.getBytes(); // 正式执行加密操作 byte[] encryptedData = cipher.doFinal(data); System.out.println("加密后===>" + encryptedData); return encryptedData; }
public static String DESDecrypt(String ivString, String keyString, String content) { try { if (Check.NuNStr(content)) { return null; } IvParameterSpec iv = new IvParameterSpec(ivString.getBytes()); DESKeySpec dks = new DESKeySpec(keyString.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, iv); byte[] result = cipher.doFinal(hexStr2ByteArr(content)); return new String(result, "utf-8"); } catch (Exception e) { LOGGER.error("ENCRYPT ERROR:"+e); } return null; }
/** * DES加密 * * @param content * * @return */ public static String DESEncrypt(final String ivString, final String keyString, final String content) { try { if (Check.isNullOrEmpty(content)) { return null; } IvParameterSpec iv = new IvParameterSpec(ivString.getBytes()); DESKeySpec dks = new DESKeySpec(keyString.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, iv); byte[] result = cipher.doFinal(content.getBytes("utf-8")); return byteArr2HexStr(result); } catch (Exception e) { LOGGER.error("ENCRYPT ERROR:" + e); } return null; }
public static String DESDecrypt(final String ivString, final String keyString, final String content) { try { if (Check.isNullOrEmpty(content)) { return null; } IvParameterSpec iv = new IvParameterSpec(ivString.getBytes()); DESKeySpec dks = new DESKeySpec(keyString.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, iv); byte[] result = cipher.doFinal(hexStr2ByteArr(content)); return new String(result, "utf-8"); } catch (Exception e) { LOGGER.error("ENCRYPT ERROR:" + e); } return null; }
public static SecretKey getSecretKey(String secretKey) throws Exception { // 返回生成指定算法的秘密密钥的 KeyGenerator 对象 KeyGenerator kg = KeyGenerator.getInstance(ENCRYPT_TYPE); // 初始化此密钥生成器,使其具有确定的密钥大小 SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(secretKey.getBytes()); kg.init(56, random); // 生成一个密钥 byte[] key = kg.generateKey().getEncoded(); // 实例化DES密钥规则 DESKeySpec dks = new DESKeySpec(key); // 实例化密钥工厂 SecretKeyFactory skf = SecretKeyFactory.getInstance(ENCRYPT_TYPE); // 生成密钥 return skf.generateSecret(dks); }
/** * Decrypts keyed checksum. * @param enc_cksum the buffer for encrypted checksum. * @param key the key. * @return the checksum. * * @modified by Yanni Zhang, 12/08/99. */ private byte[] decryptKeyedChecksum(byte[] enc_cksum, byte[] key) throws KrbCryptoException { //compute modified key byte[] new_key = new byte[keySize()]; System.arraycopy(key, 0, new_key, 0, key.length); for (int i = 0; i < new_key.length; i++) new_key[i] = (byte)(new_key[i] ^ 0xf0); //check for weak keys try { if (DESKeySpec.isWeak(new_key, 0)) { new_key[7] = (byte)(new_key[7] ^ 0xF0); } } catch (InvalidKeyException ex) { // swallow, since it should never happen } byte[] ivec = new byte[new_key.length]; byte[] cksum = new byte[enc_cksum.length]; Des.cbc_encrypt(enc_cksum, cksum, new_key, ivec, false); return cksum; }
/** * Verifies keyed checksum. * @param data the data. * @param size the length of data. * @param key the key used to encrypt the checksum. * @param checksum * @return true if verification is successful. * * @modified by Yanni Zhang, 12/08/99. */ public boolean verifyKeyedChecksum(byte[] data, int size, byte[] key, byte[] checksum, int usage) throws KrbCryptoException { byte[] cksum = decryptKeyedChecksum(checksum, key); byte[] new_data = new byte[size + confounderSize()]; System.arraycopy(cksum, 0, new_data, 0, confounderSize()); System.arraycopy(data, 0, new_data, confounderSize(), size); //check for weak keys try { if (DESKeySpec.isWeak(key, 0)) { key[7] = (byte)(key[7] ^ 0xF0); } } catch (InvalidKeyException ex) { // swallow, since it should never happen } byte[] ivec = new byte[key.length]; byte[] new_cksum = Des.des_cksum(ivec, new_data, key); byte[] orig_cksum = new byte[cksumSize() - confounderSize()]; System.arraycopy(cksum, confounderSize(), orig_cksum, 0, cksumSize() - confounderSize()); return isChecksumEqual(orig_cksum, new_cksum); }
/** * Generates a <code>SecretKey</code> object from the provided key * specification (key material). * * @param keySpec the specification (key material) of the secret key * * @return the secret key * * @exception InvalidKeySpecException if the given key specification * is inappropriate for this key factory to produce a public key. */ protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException { try { if (keySpec instanceof DESKeySpec) { return new DESKey(((DESKeySpec)keySpec).getKey()); } if (keySpec instanceof SecretKeySpec) { return new DESKey(((SecretKeySpec)keySpec).getEncoded()); } throw new InvalidKeySpecException( "Inappropriate key specification"); } catch (InvalidKeyException e) { throw new InvalidKeySpecException(e.getMessage()); } }
/** * Generates the DES key. * * @return the new DES key */ protected SecretKey engineGenerateKey() { DESKey desKey = null; if (this.random == null) { this.random = SunJCE.getRandom(); } try { byte[] key = new byte[DESKeySpec.DES_KEY_LEN]; do { this.random.nextBytes(key); setParityBit(key, 0); } while (DESKeySpec.isWeak(key, 0)); desKey = new DESKey(key); } catch (InvalidKeyException e) { // this is never thrown } return desKey; }
/** * Description 根据键值进行加密 */ private static byte[] encrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
/** * 解密 * @param src byte[] * @param password String * @return String * @throws Exception */ public static String decrypt(String decodeStr, String password) throws Exception { byte[] str=new BASE64Decoder().decodeBuffer(decodeStr); // DES算法要求有一个可信任的随机数源 SecureRandom random = new SecureRandom(); // 创建一个DESKeySpec对象 DESKeySpec desKey = new DESKeySpec(password.getBytes()); // 创建一个密匙工厂 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 将DESKeySpec对象转换成SecretKey对象 SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, random); // 真正开始解密操作 byte[] passByte=cipher.doFinal(str); return new String(passByte); }
/** * Description 根据键值进行加密 * @param data * @param key 加密键byte数组 * @return * @throws Exception */ private static byte[] encrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
/** * Description 根据键值进行解密 * @param data * @param key 加密键byte数组 * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
/** * DES算法,加密 * * @param data 待加密字符串 * @param key 加密私钥,长度不能够小于8位 * @return 加密后的字节数组,一般结合Base64编码使用 * @throws InvalidAlgorithmParameterException * @throws Exception */ public static String encrypt(String key, String data) { if (data == null) { return null; } try { DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //key的长度不能够小于8位字节 Key secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM_DES); AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV_PARAMS_BYTES); ; cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); byte[] bytes = cipher.doFinal(data.getBytes()); return byte2hex(bytes); } catch (Exception e) { e.printStackTrace(); return data; } }
@Override protected SecretKey engineGenerateSecret(String algorithm) throws IllegalStateException, NoSuchAlgorithmException, InvalidKeyException { byte secret[] = engineGenerateSecret(); if (debug.DEBUG) log("generating SecretKey for " + algorithm); if (algorithm.equals("DES")) { return (SecretKey)new DESKeySpec(secret); } else if (algorithm.equals("DESede")) { return (SecretKey)new DESedeKeySpec(secret); } else { /* AES and default */ return new SecretKeySpec(secret, algorithm); } }
/** * 加密 * * @param src 数据源 * @param key 密钥,长度必须是8的倍数 * @return 返回加密后的数据 * @throws Exception */ public static byte[] encrypt(byte[] src, byte[] key) throws Exception { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密匙数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec转换成 // 一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(DES); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); // 现在,获取数据并加密 // 正式执行加密操作 return cipher.doFinal(src); }
/** * 解密 * * @param src 数据源 * @param key 密钥,长度必须是8的倍数 * @return 返回解密后的原始数据 * @throws Exception */ public static byte[] decrypt(byte[] src, byte[] key) throws Exception { // DES算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密匙数据创建一个DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成 // 一个SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance(DES); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); // 现在,获取数据并解密 // 正式执行解密操作 return cipher.doFinal(src); }