Refine search
/** * 加密 * * @param plainByte 明文 * * @return 密文 */ public byte[] encrypt(byte[] plainByte) { byte[] cipherByte = null; try { DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory .getInstance("DESede"); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, securekey); cipherByte = cipher.doFinal(plainByte); } catch (Exception e) { logger.error(e.getMessage(), e); } return cipherByte; }
myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME; arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); ks = new DESedeKeySpec(arrayBytes); skf = SecretKeyFactory.getInstance(myEncryptionScheme); cipher = Cipher.getInstance(myEncryptionScheme); key = skf.generateSecret(ks); String encryptedString = null; try { cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT); byte[] encryptedText = cipher.doFinal(plainText); encryptedString = new String(Base64.encodeBase64(encryptedText)); } catch (Exception e) { String decryptedText=null; try { cipher.init(Cipher.DECRYPT_MODE, key); byte[] encryptedText = Base64.decodeBase64(encryptedString); byte[] plainText = cipher.doFinal(encryptedText);
/** * Create 3des secret key according to http://www.cryptosys.net/3des.html. * * @param keyBits * @return * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws InvalidKeySpecException */ public static SecretKey create3desSecretKey(byte[] keyBits) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException { assert keyBits.length >= 14; byte[] key = new byte[24]; System.arraycopy(create3desSubKey(keyBits, 0), 0, key, 0, 8); // subkey1 System.arraycopy(create3desSubKey(keyBits, 7), 0, key, 8, 8); // subkey2 System.arraycopy(key, 0, key, 16, 8); // subkey3 == subkey1 (in option2 of 3des key) KeySpec spec = new DESedeKeySpec(key, 0); SecretKeyFactory desFact = SecretKeyFactory.getInstance("DESede"); return desFact.generateSecret(spec); }
public RetailMAC(Key key,IvParameterSpec iv) { try { SecretKeyFactory fac=SecretKeyFactory.getInstance("DESede"); DESedeKeySpec spec=(DESedeKeySpec)fac.getKeySpec((SecretKey)key, DESedeKeySpec.class); byte[] desedekeydata=spec.getKey(); DESKeySpec spec2=new DESKeySpec(desedekeydata); fac=SecretKeyFactory.getInstance("DES"); this.deskey=fac.generateSecret(spec2); this.desedekey=(SecretKey)key; this.ivspec=iv; this.cipher=Cipher.getInstance("DES"); reset(); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } }
/** * Constructor * * @param password the Password used to initiate the {@link DESedeKeySpec} */ public SimpleStringEncryptor(String password) { if (password == null || password.length() < 25) { throw new IllegalArgumentException("The password must not be null and at least 25 character long"); } try { keySpec = new DESedeKeySpec(password.getBytes(UNICODE_FORMAT)); keyFactory = SecretKeyFactory.getInstance(DESEDE_ENCRYPTION_SCHEME); cipher = Cipher.getInstance(DESEDE_ENCRYPTION_SCHEME); } catch (InvalidKeyException e) { throw new IllegalArgumentException("Unable to initialize due to invalid secret key", e); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("UTF-8 is not an available char set", e); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("Not a valid encryption algorithm", e); } catch (NoSuchPaddingException e) { throw new IllegalStateException("Should not happen", e); } }
md5.digest(digest, 16, 16); iv = new IvParameterSpec(digest, 24, 8); DESedeKeySpec keySpec = new DESedeKeySpec(digest); SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede"); secret = factory.generateSecret(keySpec); cipher.init(Cipher.DECRYPT_MODE, secret, iv); return new CipherInputStream(is, cipher);
SecretKeyFactory.getInstance(desStrength); subkey1.length); spec = new DESedeKeySpec(ede, 0); if (log.isTraceEnabled()) { log.tracef("is parity-adjusted? %s", Boolean.valueOf(DESedeKeySpec.isParityAdjusted(ede, 0))); desStrength); return desFactory.generateSecret(spec);
String desKey = "0123456789abcdef0123456789abcdef0123456789abcdef"; // user value (24 bytes) byte[] keyBytes = DatatypeConverter.parseHexBinary(desKey); SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede"); SecretKey key = factory.generateSecret(new DESedeKeySpec(keyBytes));
/** * 生成 {@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; }
@Override protected KeySpec engineGetKeySpec(SecretKey secretKey, @SuppressWarnings("rawtypes") Class aClass) throws InvalidKeySpecException { if (secretKey == null) { throw new InvalidKeySpecException("Null SecretKey"); } if (aClass == SecretKeySpec.class) { try { if (!DESedeKeySpec.isParityAdjusted(secretKey.getEncoded(), 0)) { throw new InvalidKeySpecException("SecretKey is not a parity-adjusted DESEDE key"); } } catch (InvalidKeyException e) { throw new InvalidKeySpecException(e); } if (secretKey instanceof SecretKeySpec) { return (KeySpec) secretKey; } else { return new SecretKeySpec(secretKey.getEncoded(), "DESEDE"); } } else if (aClass == DESedeKeySpec.class) { try { return new DESedeKeySpec(secretKey.getEncoded()); } catch (InvalidKeyException e) { throw new InvalidKeySpecException(e); } } else { throw new InvalidKeySpecException("Unsupported KeySpec class: " + aClass); } }
protected SecretKey engineGenerateSecret( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof DESedeKeySpec) { DESedeKeySpec desKeySpec = (DESedeKeySpec)keySpec; return new SecretKeySpec(desKeySpec.getKey(), "DESede"); } return super.engineGenerateSecret(keySpec); } }
@Override protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec == null) { throw new InvalidKeySpecException("Null KeySpec"); } if (keySpec instanceof SecretKeySpec) { SecretKeySpec key = (SecretKeySpec) keySpec; try { if (!DESedeKeySpec.isParityAdjusted(key.getEncoded(), 0)) { throw new InvalidKeySpecException( "SecretKeySpec is not a parity-adjusted DESEDE key"); } } catch (InvalidKeyException e) { throw new InvalidKeySpecException(e); } return key; } else if (keySpec instanceof DESedeKeySpec) { DESedeKeySpec desKeySpec = (DESedeKeySpec) keySpec; return new SecretKeySpec(desKeySpec.getKey(), "DESEDE"); } else { throw new InvalidKeySpecException( "Unsupported KeySpec class: " + keySpec.getClass().getName()); } }
public byte[] uncTripleDes (byte [] encryptedTextBytes, byte [] key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidKeySpecException{ DESedeKeySpec keySpec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede"); SecretKey ky = keyfactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, ky); return cipher.doFinal(encryptedTextBytes); }
/** * 转换密钥 * * @param key * 二进制密钥 * * @return Key 密钥 * * @throws Exception */ private static Key toKey(byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException { // 实例化DES密钥材料 DESedeKeySpec dks = new DESedeKeySpec(key); // 实例化秘密密钥工厂 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM); // 生成秘密密钥 SecretKey secretKey = keyFactory.generateSecret(dks); return secretKey; }
public StringEncrypter(String encryptionScheme, String encryptionKey) throws EncryptionException { if (encryptionKey == null) throw new IllegalArgumentException("encryption key was null"); if (encryptionKey.trim().length() < 24) throw new IllegalArgumentException("encryption key was less than 24 characters"); try { byte[] keyAsBytes = encryptionKey.getBytes(UNICODE_FORMAT); if (encryptionScheme.equals(DESEDE_ENCRYPTION_SCHEME)) { keySpec = new DESedeKeySpec(keyAsBytes); } else if (encryptionScheme.equals(DES_ENCRYPTION_SCHEME)) { keySpec = new DESKeySpec(keyAsBytes); } else { throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme); } keyFactory = SecretKeyFactory.getInstance(encryptionScheme); cipher = Cipher.getInstance(encryptionScheme); } catch (InvalidKeyException e) { throw new EncryptionException(e); } catch (UnsupportedEncodingException e) { throw new EncryptionException(e); } catch (NoSuchAlgorithmException e) { throw new EncryptionException(e); } catch (NoSuchPaddingException e) { throw new EncryptionException(e); } }
public static byte[] hexDecode(String hex) { ByteArrayOutputStream bas = new ByteArrayOutputStream(); for (int i = 0; i < hex.length(); i+=2) { int b = Integer.parseInt(hex.substring(i, i + 2), 16); bas.write(b); } return bas.toByteArray(); } byte[] key = hexDecode("<hex representation of the Ruby key>"); byte[] iv = hexDecode("<hex representation of the Ruby IV>"); DESedeKeySpec desKeySpec = new DESedeKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); Key secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec ivSpec = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec); ... /* do the decryption */
/** * 生成 {@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; }
@Override protected KeySpec engineGetKeySpec(SecretKey secretKey, @SuppressWarnings("rawtypes") Class aClass) throws InvalidKeySpecException { if (secretKey == null) { throw new InvalidKeySpecException("Null SecretKey"); } if (aClass == SecretKeySpec.class) { try { if (!DESedeKeySpec.isParityAdjusted(secretKey.getEncoded(), 0)) { throw new InvalidKeySpecException("SecretKey is not a parity-adjusted DESEDE key"); } } catch (InvalidKeyException e) { throw new InvalidKeySpecException(e); } if (secretKey instanceof SecretKeySpec) { return (KeySpec) secretKey; } else { return new SecretKeySpec(secretKey.getEncoded(), "DESEDE"); } } else if (aClass == DESedeKeySpec.class) { try { return new DESedeKeySpec(secretKey.getEncoded()); } catch (InvalidKeyException e) { throw new InvalidKeySpecException(e); } } else { throw new InvalidKeySpecException("Unsupported KeySpec class: " + aClass); } }
protected SecretKey engineGenerateSecret( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof DESedeKeySpec) { DESedeKeySpec desKeySpec = (DESedeKeySpec)keySpec; return new SecretKeySpec(desKeySpec.getKey(), "DESede"); } return super.engineGenerateSecret(keySpec); } }
@Override protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec == null) { throw new InvalidKeySpecException("Null KeySpec"); } if (keySpec instanceof SecretKeySpec) { SecretKeySpec key = (SecretKeySpec) keySpec; try { if (!DESedeKeySpec.isParityAdjusted(key.getEncoded(), 0)) { throw new InvalidKeySpecException( "SecretKeySpec is not a parity-adjusted DESEDE key"); } } catch (InvalidKeyException e) { throw new InvalidKeySpecException(e); } return key; } else if (keySpec instanceof DESedeKeySpec) { DESedeKeySpec desKeySpec = (DESedeKeySpec) keySpec; return new SecretKeySpec(desKeySpec.getKey(), "DESEDE"); } else { throw new InvalidKeySpecException( "Unsupported KeySpec class: " + keySpec.getClass().getName()); } }