当前所在位置:珠峰网资料 >> 计算机 >> 软件水平 >> 正文
2015年初级软考辅导:javaAES算法
发布时间:2010/3/13 14:43:50 来源:城市学习网 编辑:MOON
  package com.rsa;
  import java.security.InvalidAlgorithmParameterException;
  import java.security.InvalidKeyException;
  import java.security.NoSuchAlgorithmException;
  import java.security.SecureRandom;
  import java.security.spec.AlgorithmParameterSpec;
  import javax.crypto.BadPaddingException;
  import javax.crypto.Cipher;
  import javax.crypto.IllegalBlockSizeException;
  import javax.crypto.KeyGenerator;
  import javax.crypto.NoSuchPaddingException;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.IvParameterSpec;
  public class AESEncrypter {
  public static byte iv = new byte { 82, 22, 50, 44, -16, 124, -40, -114, -87, -40, 37, 23, -56, 23, -33, 75 };
  private static AESEncrypter aes = null;
  public static byte key1 = new byte { -42, 35, 67, -86, 19, 29, -11, 84, 94, 111, 75, -104, 71, 46, 86, -21, -119, 110, -11, -32, -28, 91, -33, -46, 99, 49, 2, 66, -101, -11, -8, 56 };
  private AESEncrypter() {
  }
  public static synchronized AESEncrypter getInstance() {
  if (aes == null) {
  aes = new AESEncrypter();
  }
  return aes;
  }
  public String encrypt(String msg) {
  String str = "";
  try {
  KeyGenerator kgen = KeyGenerator.getInstance("AES");
  kgen.init(128, new SecureRandom(key1));
  AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
  SecretKey key = kgen.generateKey();
  Cipher ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
  str = asHex(ecipher.doFinal(msg.getBytes()));
  } catch (BadPaddingException e) {
  e.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
  e.printStackTrace();
  } catch (NoSuchPaddingException e) {
  e.printStackTrace();
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved