|
| 1 | +package cn.aofeng.demo.encrypt; |
| 2 | + |
| 3 | +import static cn.aofeng.demo.util.LogUtil.log; |
| 4 | + |
| 5 | +import java.io.UnsupportedEncodingException; |
| 6 | +import java.security.InvalidAlgorithmParameterException; |
| 7 | +import java.security.InvalidKeyException; |
| 8 | +import java.security.NoSuchAlgorithmException; |
| 9 | + |
| 10 | +import javax.crypto.BadPaddingException; |
| 11 | +import javax.crypto.IllegalBlockSizeException; |
| 12 | +import javax.crypto.NoSuchPaddingException; |
| 13 | +import javax.crypto.SecretKey; |
| 14 | + |
| 15 | +import org.apache.commons.codec.binary.Base64; |
| 16 | + |
| 17 | +/** |
| 18 | + * Blowfish加密解密。 |
| 19 | + * |
| 20 | + * @author <a href="mailto:[email protected]">聂勇</a> |
| 21 | + */ |
| 22 | +public class Blowfish extends EncryptAndDecrypt { |
| 23 | + |
| 24 | + public static void main(String[] args) throws UnsupportedEncodingException, |
| 25 | + InvalidKeyException, IllegalBlockSizeException, |
| 26 | + BadPaddingException, NoSuchAlgorithmException, |
| 27 | + NoSuchPaddingException, InvalidAlgorithmParameterException { |
| 28 | + String data = "炎黄,汉字,english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-"; |
| 29 | + log("待加密的数据:\n%s", data); |
| 30 | + |
| 31 | + Blowfish bf = new Blowfish(); |
| 32 | + String encryptType = "Blowfish"; |
| 33 | + SecretKey secretKey = bf.createSecretKey(encryptType, |
| 34 | + "abcdefgh_1234567"); |
| 35 | + byte[] secretData = bf.encrypt(encryptType, secretKey, data); |
| 36 | + log("使用%s加密后的数据:", encryptType); |
| 37 | + log(Base64.encodeBase64String(secretData)); |
| 38 | + |
| 39 | + String srcStr = bf.decrypt(encryptType, secretKey, secretData); |
| 40 | + log("解密后的数据:\n%s", srcStr); |
| 41 | + } |
| 42 | + |
| 43 | +} |
0 commit comments