|
| 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 | +/** |
| 16 | + * 加密解密性能比较。 |
| 17 | + * |
| 18 | + * @author <a href="mailto:[email protected]">聂勇</a> |
| 19 | + */ |
| 20 | +public class PerformanceCompare extends EncryptAndDecrypt { |
| 21 | + |
| 22 | + public void blowfishPerformence(String data) |
| 23 | + throws UnsupportedEncodingException, InvalidKeyException, |
| 24 | + IllegalBlockSizeException, BadPaddingException, |
| 25 | + NoSuchAlgorithmException, NoSuchPaddingException, |
| 26 | + InvalidAlgorithmParameterException { |
| 27 | + Blowfish bf = new Blowfish(); |
| 28 | + SecretKey secretKey = createSecretKey(bf.encryptType, bf.key); |
| 29 | + long startTime = System.currentTimeMillis(); |
| 30 | + for (int j = 0; j < 100000; j++) { |
| 31 | + bf.encrypt(bf.encryptType, secretKey, data+j); |
| 32 | + } |
| 33 | + long endTime = System.currentTimeMillis(); |
| 34 | + long usedTime = endTime - startTime; |
| 35 | + log("使用%s进行%d次加密消耗时间%d毫秒", bf.encryptType, 100000, usedTime); |
| 36 | + } |
| 37 | + |
| 38 | + public void aesPerformence(String data) |
| 39 | + throws UnsupportedEncodingException, InvalidKeyException, |
| 40 | + IllegalBlockSizeException, BadPaddingException, |
| 41 | + NoSuchAlgorithmException, NoSuchPaddingException, |
| 42 | + InvalidAlgorithmParameterException { |
| 43 | + AES aes = new AES(); |
| 44 | + SecretKey secretKey = createSecretKey("AES", aes.key); |
| 45 | + long startTime = System.currentTimeMillis(); |
| 46 | + for (int j = 0; j < 100000; j++) { |
| 47 | + aes.encrypt(aes.encryptType, secretKey, data+j, |
| 48 | + aes.algorithmParam); |
| 49 | + } |
| 50 | + long endTime = System.currentTimeMillis(); |
| 51 | + long usedTime = endTime - startTime; |
| 52 | + log("使用%s进行%d次加密消耗时间%d毫秒", aes.encryptType, 100000, usedTime); |
| 53 | + } |
| 54 | + |
| 55 | + public static void main(String[] args) throws InvalidKeyException, |
| 56 | + UnsupportedEncodingException, IllegalBlockSizeException, |
| 57 | + BadPaddingException, NoSuchAlgorithmException, |
| 58 | + NoSuchPaddingException, InvalidAlgorithmParameterException { |
| 59 | + String data = "炎黄,汉字,english,do it,abcdefghijklmnopqrstuvwxyz,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, ~!@#$%^&*()_+=-"; |
| 60 | + log("待加密的数据:\n%s", data); |
| 61 | + |
| 62 | + PerformanceCompare pc = new PerformanceCompare(); |
| 63 | + // AES |
| 64 | + pc.aesPerformence(data); |
| 65 | + |
| 66 | + // Blowfish |
| 67 | + pc.blowfishPerformence(data); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments