Refine search
/** * 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); }
@Test public void shouldGenerateAValidAndSafeDESKey() throws Exception { DESCipherProvider desCipherProvider = new DESCipherProvider(new SystemEnvironment()); byte[] key = desCipherProvider.getKey(); assertThat(DESKeySpec.isWeak(key, 0), is(false)); }
private byte[] calcResponse(byte[] key, byte[] text) throws GeneralSecurityException { assert key.length == 21; DESKeySpec dks1 = new DESKeySpec(makeDesKey(key, 0)); DESKeySpec dks2 = new DESKeySpec(makeDesKey(key, 7)); DESKeySpec dks3 = new DESKeySpec(makeDesKey(key, 14)); SecretKey key1 = fac.generateSecret(dks1); SecretKey key2 = fac.generateSecret(dks2); SecretKey key3 = fac.generateSecret(dks3); cipher.init(Cipher.ENCRYPT_MODE, key1); byte[] out1 = cipher.doFinal(text, 0, 8); cipher.init(Cipher.ENCRYPT_MODE, key2); byte[] out2 = cipher.doFinal(text, 0, 8); cipher.init(Cipher.ENCRYPT_MODE, key3); byte[] out3 = cipher.doFinal(text, 0, 8); byte[] result = new byte[24]; System.arraycopy(out1, 0, result, 0, 8); System.arraycopy(out2, 0, result, 8, 8); System.arraycopy(out3, 0, result, 16, 8); return result; }
private Key genKey(String k) throws GeneralSecurityException { byte[] host = (k + "012345678").getBytes(); byte[] key = new byte[8]; System.arraycopy(host, 0, key, 0, 8); return SecretKeyFactory.getInstance("DES") // .generateSecret(new DESKeySpec(key)); }
DESKeySpec dkey = new DESKeySpec(keyBytes); key = new SecretKeySpec(dkey.getKey(), "DES"); deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); enCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); return enCipher.doFinal(input); deCipher.init(Cipher.DECRYPT_MODE, key, ivSpec); return convertFromByteArray(deCipher.doFinal(encrypted));
/** * * This method generates keys. This method is implementation specific and should not be present in any general purpose interface * extracted from this class. * * @return * @throws Exception */ public static String generateEncodedKey() throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecretKey desKey = keygen.generateKey(); // Create the cipher Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init((Cipher.WRAP_MODE), desKey); SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES"); DESKeySpec desSpec = (DESKeySpec) desFactory.getKeySpec(desKey, javax.crypto.spec.DESKeySpec.class); byte[] rawDesKey = desSpec.getKey(); return new String(Base64.encodeBase64(rawDesKey)); }
byte password[] = (WHAT YOUR WANT. STRING, NUMBER, etc.).getBytes(); DESKeySpec desKeySpec; try { desKeySpec = new DESKeySpec(password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(desKeySpec); Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); desCipher.init(Cipher.ENCRYPT_MODE, key); // Create stream FileOutputStream fos = new FileOutputStream("Your file here"); BufferedOutputStream bos = new BufferedOutputStream(fos); CipherOutputStream cos = new CipherOutputStream(bos, desCipher); }
KeyGenerator kg = KeyGenerator.getInstance("DES"); kg.init(new SecureRandom()); SecretKey key = kg.generateKey(); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); Class spec = Class.forName("javax.crypto.spec.DESKeySpec"); DESKeySpec ks = (DESKeySpec) skf.getKeySpec(key, spec); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("keyfile")); oos.writeObject(ks.getKey()); Cipher c = Cipher.getInstance("DES/CFB8/NoPadding"); c.init(Cipher.ENCRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(new FileOutputStream("ciphertext"), c); PrintWriter pw = new PrintWriter(new OutputStreamWriter(cos)); pw.println("Stand and unfold yourself"); pw.close(); oos.writeObject(c.getIV()); oos.close();
DESKeySpec keySpec = new DESKeySpec("Your secret Key phrase".getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); sun.misc.BASE64Encoder base64encoder = new BASE64Encoder(); sun.misc.BASE64Decoder base64decoder = new BASE64Decoder(); ......... // ENCODE plainTextPassword String byte[] cleartext = plainTextPassword.getBytes("UTF8"); Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe cipher.init(Cipher.ENCRYPT_MODE, key); String encryptedPwd = base64encoder.encode(cipher.doFinal(cleartext)); // now you can store it ...... // DECODE encryptedPwd String byte[] encrypedPwdBytes = base64decoder.decodeBuffer(encryptedPwd); Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe cipher.init(Cipher.DECRYPT_MODE, key); byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));
/** * 生成 {@link SecretKey} * * @param algorithm DES算法,包括DES、DESede等 * @param key 密钥 * @return {@link SecretKey} */ public static SecretKey generateDESKey(String algorithm, byte[] key) { if (StrUtil.isBlank(algorithm) || false == algorithm.startsWith("DES")) { throw new CryptoException("Algorithm [{}] is not a DES algorithm!"); } SecretKey secretKey = null; if (null == key) { secretKey = generateKey(algorithm); } else { KeySpec keySpec; try { if (algorithm.startsWith("DESede")) { // DESede兼容 keySpec = new DESedeKeySpec(key); } else { keySpec = new DESKeySpec(key); } } catch (InvalidKeyException e) { throw new CryptoException(e); } secretKey = generateKey(algorithm, keySpec); } return secretKey; }
private byte[] secretKeyToBytes(final SecretKey key) { try { final DESKeySpec ks = (DESKeySpec) secretKeyFactory.getKeySpec(key, DESKeySpec.class); return ks.getKey(); } catch (final GeneralSecurityException e) { throw new IllegalStateException(e.getMessage()); } }
protected SecretKey engineGenerateSecret( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof DESKeySpec) { DESKeySpec desKeySpec = (DESKeySpec)keySpec; return new SecretKeySpec(desKeySpec.getKey(), "DES"); } return super.engineGenerateSecret(keySpec); } }
private byte[] calcResponse(byte[] key, byte[] text) throws GeneralSecurityException { assert key.length == 21; DESKeySpec dks1 = new DESKeySpec(makeDesKey(key, 0)); DESKeySpec dks2 = new DESKeySpec(makeDesKey(key, 7)); DESKeySpec dks3 = new DESKeySpec(makeDesKey(key, 14)); SecretKey key1 = fac.generateSecret(dks1); SecretKey key2 = fac.generateSecret(dks2); SecretKey key3 = fac.generateSecret(dks3); cipher.init(Cipher.ENCRYPT_MODE, key1); byte[] out1 = cipher.doFinal(text, 0, 8); cipher.init(Cipher.ENCRYPT_MODE, key2); byte[] out2 = cipher.doFinal(text, 0, 8); cipher.init(Cipher.ENCRYPT_MODE, key3); byte[] out3 = cipher.doFinal(text, 0, 8); byte[] result = new byte[24]; System.arraycopy(out1, 0, result, 0, 8); System.arraycopy(out2, 0, result, 8, 8); System.arraycopy(out3, 0, result, 16, 8); return result; }
private Key genKey(String k) throws GeneralSecurityException { byte[] host = (k + "012345678").getBytes(); byte[] key = new byte[8]; System.arraycopy(host, 0, key, 0, 8); return SecretKeyFactory.getInstance("DES") // .generateSecret(new DESKeySpec(key)); }
// only the first 8 Bytes of the constructor argument are used // as material for generating the keySpec DESKeySpec keySpec = new DESKeySpec("YourSecr".getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); sun.misc.BASE64Encoder base64encoder = new BASE64Encoder(); sun.misc.BASE64Decoder base64decoder = new BASE64Decoder(); ......... // ENCODE plainTextPassword String byte[] cleartext = plainTextPassword.getBytes("UTF8"); Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe cipher.init(Cipher.ENCRYPT_MODE, key); String encrypedPwd = base64encoder.encode(cipher.doFinal(cleartext)); // now you can store it ...... // DECODE encryptedPwd String byte[] encrypedPwdBytes = base64decoder.decodeBuffer(encryptedPwd); Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe cipher.init(Cipher.DECRYPT_MODE, key); byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));
/** * 生成 {@link SecretKey} * * @param algorithm DES算法,包括DES、DESede等 * @param key 密钥 * @return {@link SecretKey} */ public static SecretKey generateDESKey(String algorithm, byte[] key) { if (StrUtil.isBlank(algorithm) || false == algorithm.startsWith("DES")) { throw new CryptoException("Algorithm [{}] is not a DES algorithm!"); } SecretKey secretKey = null; if (null == key) { secretKey = generateKey(algorithm); } else { KeySpec keySpec; try { if (algorithm.startsWith("DESede")) { // DESede兼容 keySpec = new DESedeKeySpec(key); } else { keySpec = new DESKeySpec(key); } } catch (InvalidKeyException e) { throw new CryptoException(e); } secretKey = generateKey(algorithm, keySpec); } return secretKey; }
protected SecretKey engineGenerateSecret( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof DESKeySpec) { DESKeySpec desKeySpec = (DESKeySpec)keySpec; return new SecretKeySpec(desKeySpec.getKey(), "DES"); } return super.engineGenerateSecret(keySpec); } }
/** * 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); }
private byte[] calcLMHash() throws GeneralSecurityException { byte[] magic = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25}; byte[] pwb = null; try { pwb = password.toUpperCase(Locale.ENGLISH).getBytes("iso-8859-1"); } catch (UnsupportedEncodingException ex) { // should never happen assert false; } byte[] pwb1 = new byte[14]; int len = password.length(); if (len > 14) len = 14; System.arraycopy(pwb, 0, pwb1, 0, len); /* Zero padded */ DESKeySpec dks1 = new DESKeySpec(makeDesKey(pwb1, 0)); DESKeySpec dks2 = new DESKeySpec(makeDesKey(pwb1, 7)); SecretKey key1 = fac.generateSecret(dks1); SecretKey key2 = fac.generateSecret(dks2); cipher.init(Cipher.ENCRYPT_MODE, key1); byte[] out1 = cipher.doFinal(magic, 0, 8); cipher.init(Cipher.ENCRYPT_MODE, key2); byte[] out2 = cipher.doFinal(magic, 0, 8); byte[] result = new byte [21]; System.arraycopy(out1, 0, result, 0, 8); System.arraycopy(out2, 0, result, 8, 8); return result; }
private Key genKey(String k) throws GeneralSecurityException { byte[] host = (k + "012345678").getBytes(); byte[] key = new byte[8]; System.arraycopy(host, 0, key, 0, 8); return SecretKeyFactory.getInstance("DES") // .generateSecret(new DESKeySpec(key)); }