aesecbjava的简单介绍
今天给各位分享aesecbjava的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java实现aes加密或者解密,不用工具包的怎么做
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
*
* @author wchun
*
* AES128 算法,加密模式为ECB,填充模式为 pkcs7(实际就是pkcs5)
*
*
*/
public class AES {
static final String algorithmStr="AES/ECB/PKCS5Padding";
static private KeyGenerator keyGen;
static private Cipher cipher;
static boolean isInited=false;
//初始化
static private void init()
{
//初始化keyGen
try {
keyGen=KeyGenerator.getInstance("AES");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
keyGen.init(128);
//初始化cipher
try {
cipher=Cipher.getInstance(algorithmStr);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isInited=true;
}
public static byte[] GenKey()
{
if(!isInited)//如果没有初始化过,则初始化
{
init();
}
return keyGen.generateKey().getEncoded();
}
public static byte[] Encrypt(byte[] content,byte[] keyBytes)
{
byte[] encryptedText=null;
if(!isInited)//为初始化
{
init();
}
Key key=new SecretKeySpec(keyBytes,"AES");
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
encryptedText=cipher.doFinal(content);
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encryptedText;
}
//解密为byte[]
public static byte[] DecryptToBytes(byte[] content,byte[] keyBytes)
{
byte[] originBytes=null;
if(!isInited)
{
init();
}
Key key=new SecretKeySpec(keyBytes,"AES");
try {
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//解密
try {
originBytes=cipher.doFinal(content);
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return originBytes;
}
}
请教个关于Java实现AES加解密的问题
JDK对DESede算法的支持
密钥长度:128位
工作模式:ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128
填充方式:Nopadding/PKCS5Padding/ISO10126Padding/
AES加密解密的java实现:
package com.kongxincai.encanddec;import java.security.Key;import java.security.NoSuchAlgorithmException;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;public class AESCoder { private static final String KEY_ALGORITHM = "AES"; private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法
public static byte[] initSecretKey() { //返回生成指定算法密钥生成器的 KeyGenerator 对象
KeyGenerator kg = null; try {
kg = KeyGenerator.getInstance(KEY_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace(); return new byte[0];
} //初始化此密钥生成器,使其具有确定的密钥大小 //AES 要求密钥长度为 128
kg.init(128); //生成一个密钥
SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded();
} private static Key toKey(byte[] key){ //生成密钥
return new SecretKeySpec(key, KEY_ALGORITHM);
} public static byte[] encrypt(byte[] data,Key key) throws Exception{ return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} public static byte[] encrypt(byte[] data,byte[] key) throws Exception{ return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} public static byte[] encrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{ //还原密钥
Key k = toKey(key); return encrypt(data, k, cipherAlgorithm);
} public static byte[] encrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{ //实例化
Cipher cipher = Cipher.getInstance(cipherAlgorithm); //使用密钥初始化,设置为加密模式 cipher.init(Cipher.ENCRYPT_MODE, key); //执行操作
return cipher.doFinal(data);
} public static byte[] decrypt(byte[] data,byte[] key) throws Exception{ return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} public static byte[] decrypt(byte[] data,Key key) throws Exception{ return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} public static byte[] decrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{ //还原密钥
Key k = toKey(key); return decrypt(data, k, cipherAlgorithm);
} public static byte[] decrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{ //实例化
Cipher cipher = Cipher.getInstance(cipherAlgorithm); //使用密钥初始化,设置为解密模式 cipher.init(Cipher.DECRYPT_MODE, key); //执行操作
return cipher.doFinal(data);
} private static String showByteArray(byte[] data){ if(null == data){ return null;
}
StringBuilder sb = new StringBuilder("{"); for(byte b:data){
sb.append(b).append(",");
}
sb.deleteCharAt(sb.length()-1);
sb.append("}"); return sb.toString();
} public static void main(String[] args) throws Exception { byte[] key = initSecretKey();
System.out.println("key:"+showByteArray(key));
Key k = toKey(key); //生成秘钥
String data ="AES数据";
System.out.println("加密前数据: string:"+data);
System.out.println("加密前数据: byte[]:"+showByteArray(data.getBytes()));
System.out.println(); byte[] encryptData = encrypt(data.getBytes(), k);//数据加密
System.out.println("加密后数据: byte[]:"+showByteArray(encryptData));// System.out.println("加密后数据: hexStr:"+Hex.encodeHexStr(encryptData)); System.out.println(); byte[] decryptData = decrypt(encryptData, k);//数据解密
System.out.println("解密后数据: byte[]:"+showByteArray(decryptData));
System.out.println("解密后数据: string:"+new String(decryptData));
}
}
能在JAVA和C之间通用的AES加密标准是什么?
public static byte[] encrypt(String content, String password) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(),
"AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] byteContent = content.getBytes("utf-8");
byte[] result = cipher.doFinal(byteContent);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static byte[] decrypt(byte[] content, String password) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(),
"AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] result = cipher.doFinal(content);
return result; //
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
这个和Linux里面的OpenSSL加解密兼容
关于aesecbjava和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。