Android Open Source - base-android-utils A E S7 Padding






From Project

Back to project page base-android-utils.

License

The source code is released under:

Apache License

If you think the Android project base-android-utils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.pc.mobile.helper.v14.crypt;
/* www .ja  va2s.  c o  m*/
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES?????????7padding?????????????????????????????????????????????????????????
 * ?????????????????: 1????????2?jar??bcprov-jdk16-139.jar\local_policy.jar?????????
 * <!-- ?????aes?7padding?????????? --> <dependency> <groupId>com.encrypt</groupId>
 * <artifactId>bcprov-jdk16-139</artifactId> <version>1.0.0</version>
 * </dependency> <dependency> <groupId>com.encrypt</groupId>
 * <artifactId>local_policy</artifactId> <version>1.0.0</version> </dependency>
 * 2?????Java.security
 * 
 * ??????%JDK_Home%\ jre\lib\security\java.security???????9?????
 * 
 * security.provider.1=sun.security.provider.Sun
 * 
 * security.provider.2=sun.security.rsa.SunRsaSign
 * 
 * security.provider.3=com.sun.net.ssl.internal.ssl.Provider
 * 
 * security.provider.4=com.sun.crypto.provider.SunJCE
 * 
 * security.provider.5=sun.security.jgss.SunProvider
 * 
 * security.provider.6=com.sun.security.sasl.Provider
 * 
 * security.provider.7=org.jcp.xml.dsig.internal.dom.XMLDSigRI
 * 
 * security.provider.8=sun.security.smartcardio.SunPCSC
 * 
 * security.provider.9=sun.security.mscapi.SunMSCAPI
 * 
 * ??9???????????????
 * 
 * #??BouncyCastleProvider
 * 
 * security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider
 * 
 * ???Java.security???
 * 
 * ??????%JRE_Home%\lib\security\java.security????????????????
 * 
 * @author?JudyHuang
 * @since?2013-3-28 ????08:16:22
 * @version:
 */
public class AES7Padding {
  /**
   * ??
   * 
   * @param content
   *            ????????
   * @param password
   *            ?????
   * @return
   */
  public static byte[] encrypt(byte[] data, byte[] key) {
    CheckUtils.notEmpty(data, "data");
    CheckUtils.notEmpty(key, "key");
    if (key.length != 16) {
      throw new RuntimeException(
          "Invalid AES key length (must be 16 bytes)");
    }
    try {
      SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
      byte[] enCodeFormat = secretKey.getEncoded();
      SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, "AES");
      Cipher cipher = Cipher.getInstance(
          ConfigureEncryptAndDecrypt.CIPHER_ALGORITHM, "BC");// ??????
      cipher.init(Cipher.ENCRYPT_MODE, seckey);// ????
      byte[] result = cipher.doFinal(data);
      return result; // ??
    } catch (Exception e) {
      throw new RuntimeException("encrypt fail!", e);
    }
  }

  /**
   * ??
   * 
   * @param content
   *            ?????
   * @param password
   *            ????
   * @return
   */
  public static byte[] decrypt(byte[] data, byte[] key) {
    CheckUtils.notEmpty(data, "data");
    CheckUtils.notEmpty(key, "key");
    if (key.length != 16) {
      throw new RuntimeException(
          "Invalid AES key length (must be 16 bytes)");
    }
    try {
      SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
      byte[] enCodeFormat = secretKey.getEncoded();
      SecretKeySpec seckey = new SecretKeySpec(enCodeFormat, "AES");
      Cipher cipher = Cipher.getInstance(
          ConfigureEncryptAndDecrypt.CIPHER_ALGORITHM, "BC");// ??????
      cipher.init(Cipher.DECRYPT_MODE, seckey);// ????
      byte[] result = cipher.doFinal(data);
      return result; // ??
    } catch (Exception e) {
      throw new RuntimeException("decrypt fail!", e);
    }
  }

  public static String encryptToBase64(String data, String key) {
    try {
      byte[] valueByte = encrypt(
          data.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING),
          key.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING));
      return new String(Base64.encode(valueByte));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("encrypt fail!", e);
    }

  }

  public static String decryptFromBase64(String data, String key) {
    try {
      byte[] originalData = Base64.decode(data.getBytes());
      byte[] valueByte = decrypt(originalData,
          key.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING));
      return new String(valueByte,
          ConfigureEncryptAndDecrypt.CHAR_ENCODING);
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("decrypt fail!", e);
    }
  }

  public static String encryptWithKeyBase64(String data, String key) {
    try {
      byte[] valueByte = encrypt(
          data.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING),
          Base64.decode(key.getBytes()));
      return new String(Base64.encode(valueByte));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("encrypt fail!", e);
    }
  }

  public static String decryptWithKeyBase64(String data, String key) {
    try {
      byte[] originalData = Base64.decode(data.getBytes());
      byte[] valueByte = decrypt(originalData,
          Base64.decode(key.getBytes()));
      return new String(valueByte,
          ConfigureEncryptAndDecrypt.CHAR_ENCODING);
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("decrypt fail!", e);
    }
  }

  public static byte[] genarateRandomKey() {
    KeyGenerator keygen = null;
    try {
      keygen = KeyGenerator
          .getInstance(ConfigureEncryptAndDecrypt.AES_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(" genarateRandomKey fail!", e);
    }
    SecureRandom random = new SecureRandom();
    keygen.init(random);
    Key key = keygen.generateKey();
    return key.getEncoded();
  }

  public static String genarateRandomKeyWithBase64() {
    return new String(Base64.encode(genarateRandomKey()));
  }

}




Java Source Code List

me.pc.mobile.helper.v14.BuildConfig.java
me.pc.mobile.helper.v14.base.BaseActivity.java
me.pc.mobile.helper.v14.base.BaseApp.java
me.pc.mobile.helper.v14.base.BaseFrag.java
me.pc.mobile.helper.v14.base.abs.BaseJsonParser.java
me.pc.mobile.helper.v14.crypt.AES7Padding.java
me.pc.mobile.helper.v14.crypt.AES.java
me.pc.mobile.helper.v14.crypt.Base64.java
me.pc.mobile.helper.v14.crypt.CheckUtils.java
me.pc.mobile.helper.v14.crypt.ConfigureEncryptAndDecrypt.java
me.pc.mobile.helper.v14.crypt.RSA.java
me.pc.mobile.helper.v14.files.ExternalStorage.java
me.pc.mobile.helper.v14.files.FileUtils.java
me.pc.mobile.helper.v14.files.Reader.java
me.pc.mobile.helper.v14.files.Writer.java
me.pc.mobile.helper.v14.http.AsyncHttpUtil.java
me.pc.mobile.helper.v14.net.Addresses.java
me.pc.mobile.helper.v14.net.NetworkUtil.java
me.pc.mobile.helper.v14.net.WifiWaker.java
me.pc.mobile.helper.v14.receiver.BatteryStateReceiver.java
me.pc.mobile.helper.v14.receiver.NetworkStateChangeReceiver.java
me.pc.mobile.helper.v14.ui.image.RoundedDrawable.java
me.pc.mobile.helper.v14.ui.image.RoundedImageView.java
me.pc.mobile.helper.v14.util.AppInstallUtil.java
me.pc.mobile.helper.v14.util.BitDrawableUtil.java
me.pc.mobile.helper.v14.util.DeviceIdentifier.java
me.pc.mobile.helper.v14.util.DisplayUtils.java
me.pc.mobile.helper.v14.util.IntentUtil.java
me.pc.mobile.helper.v14.util.IoUtils.java
me.pc.mobile.helper.v14.util.LogUtil.java
me.pc.mobile.helper.v14.util.PackageUtil.java
me.pc.mobile.helper.v14.util.PermissionAssertUtils.java
me.pc.mobile.helper.v14.util.RegexUtil.java
me.pc.mobile.helper.v14.util.SharedPrefUtil.java
me.pc.mobile.helper.v14.util.StorageUtils.java