AES加密

@param data
@param key
@return
@throws Exception
341226
public static String encryptData(String data, String key) throws Exception {
		// 创建密码器
		Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
		// 初始化
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(HashKit.md5(key).toLowerCase().getBytes(), ALGORITHM));
		return Base64Kit.encode(cipher.doFinal(data.getBytes()));
	}
990285
public void setKey(String key) throws CryptoException {
		final int length = key.length();
		if (length < KEY_MIN_LENGTH || length >= SBOX_LENGTH) {
			throw new CryptoException("Key length has to be between {} and {}", KEY_MIN_LENGTH, (SBOX_LENGTH - 1));
		}

		final WriteLock writeLock = this.lock.writeLock();
		writeLock.lock();
		try {
			this.key = StrUtil.utf8Bytes(key);
			this.sbox = initSBox(this.key);
		} finally {
			writeLock.unlock();
		}
	}
123081
protected function encrypt($data, $key)
    {
        $iv = random_bytes(16); // AES block size in CBC mode
        // Encryption
        $ciphertext = openssl_encrypt(
            $data,
            'AES-256-CBC',
            mb_substr($key, 0, 32, '8bit'),
            OPENSSL_RAW_DATA,
            $iv
        );
        // Authentication
        $hmac = hash_hmac(
            'SHA256',
            $iv . $ciphertext,
            mb_substr($key, 32, null, '8bit'),
            true
        );
        return $hmac . $iv . $ciphertext;
    }
284875
def aes_key(self, data):
        
        old = self.key
        if data:
            new = self._decode_input_string(data)
            if len(new) == 16:
                self.key = new
            else:
                raise yubico_exception.InputError('AES128 key must be exactly 16 bytes')

        return old
423683
@Override
    protected byte[] load(ConfidentialKey key) throws IOException {
        try {
            File f = getFileFor(key);
            if (!f.exists())    return null;

            Cipher sym = Secret.getCipher("AES");
            sym.init(Cipher.DECRYPT_MODE, masterKey);
            try (InputStream fis=Files.newInputStream(f.toPath());
                 CipherInputStream cis = new CipherInputStream(fis, sym)) {
                byte[] bytes = IOUtils.toByteArray(cis);
                return verifyMagic(bytes);
            }
        } catch (GeneralSecurityException e) {
            throw new IOException("Failed to load the key: "+key.getId(),e);
        } catch (InvalidPathException e) {
            throw new IOException(e);
        } catch (IOException x) {
            if (x.getCause() instanceof BadPaddingException) {
                return null; // broken somehow
            } else {
                throw x;
            }
        }
    }
71010
public void doAESEncryption() throws Exception{
		if(!initAESDone)
			initAES();
		cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		//System.out.println(secretKey.getEncoded());
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		AlgorithmParameters params = cipher.getParameters();
		iv = params.getParameterSpec(IvParameterSpec.class).getIV();
		secretCipher = cipher.doFinal(secretPlain);
		clearPlain();
	}
1182358
public byte[] encrypt(byte[] data) {
		lock.lock();
		try {
			if (null == this.params) {
				cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			} else {
				cipher.init(Cipher.ENCRYPT_MODE, secretKey, params);
			}
			return cipher.doFinal(data);
		} catch (Exception e) {
			throw new CryptoException(e);
		} finally {
			lock.unlock();
		}
	}
915007
public byte[] encryptData(PublicKey key,byte[] data)
    {
    	try {
    		if(data==null || data.length<=0)
    		{
    			return null;
    		}
			Cipher cipher=Cipher.getInstance(algorithm);
			cipher.init(Cipher.ENCRYPT_MODE, key);
			return cipher.doFinal(data);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
    	return null;
    }
792084
public byte[] encrypt(String data, KeyType keyType) {
		return encrypt(StrUtil.bytes(data, CharsetUtil.CHARSET_UTF_8), keyType);
	}
405347
@Inject
	public void setKey(@Named(NetworkConfig.AES_KEY) String key) throws Exception {
		final byte[] raw = key.getBytes(NetworkConfig.getStringEncodingCharset());
		final int keySize = raw.length;
		if ((keySize % 16) == 0 || (keySize % 24) == 0 || (keySize % 32) == 0) {
			this.skeySpec = new SecretKeySpec(raw, "AES"); //$NON-NLS-1$
			// this.cipher = Cipher.getInstance(ALGORITHM);
		} else {
			throw new IllegalArgumentException(Messages.AESEventEncrypter_0);
		}

	}
911148
public static byte[] decrypt(byte[] data, byte[] key) {
        checkNotNull(data);
        checkNotNull(key);
        checkArgument(key.length >= 5 && key.length <= 256);
        StreamCipher rc4 = new RC4Engine();
        rc4.init(false, new KeyParameter(key));
        byte[] decrypted = new byte[data.length];
        rc4.processBytes(data, 0, data.length, decrypted, 0);
        return decrypted;
    }
538522
public static String encrypt(String c, String key) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encoded = cipher.doFinal(c.getBytes());
            return new String(Hex.encodeHex(encoded));

        } catch (final Exception e) {
            logger.warn("Could not encrypt string", e);
            return null;
        }
    }
778696
public static String hmacSHA1(String data, String key) {
        return hmacSHA1(data.getBytes(), key.getBytes());
    }
507277
public static String decrypt(String data, byte[] key)
    {
        try
        {
            Key k = toSecretKey(key);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, k);
            byte[] toByteArray = Hex.decodeHex(data.toCharArray());
            byte[] decrypted = cipher.doFinal(toByteArray);
            return new String(decrypted, "UTF-8");
        }
        catch (GeneralSecurityException e)
        {
            throw new RuntimeException("Failed to decrypt.", e);
        }
        catch (UnsupportedEncodingException e)
        {
            throw new RuntimeException(e);
        }
        catch (DecoderException e)
        {
            throw new RuntimeException(e);
        }
    }
956699
public byte[] init(byte[] key){
		byte[] iv = new byte[blockSize];
		new SecureRandom().nextBytes(iv);
		init(iv, key);
		return iv;
	}
1218504
@Override
	public byte[] aesDecrypt(byte[] data, int offset, int length, byte[] key,
			byte[] initVector) throws CryptoException {
		try {
			SecretKeySpec scs = new SecretKeySpec(key, "AES");
			Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding", "ZBC");
			IvParameterSpec iv = new IvParameterSpec(initVector);
			ByteArrayOutputStream baos = new ByteArrayOutputStream(length);
			cipher.init(Cipher.DECRYPT_MODE, scs, iv);
			CipherOutputStream out = new CipherOutputStream(baos, cipher);
			out.write(data, offset, length);
			out.close();
			baos.close();
			return baos.toByteArray();
		} catch (Exception e) {
			throw new CryptoException(e);
		}
	}
501522
public static byte[] aesDecrypt(final byte[] input, final byte[] key, final byte[] iv)
		throws GeneralSecurityException {
		return aes(input, key, iv, Cipher.DECRYPT_MODE);
	}