AES 的加密操作

2021-07-26 02:40:22 字數 1991 閱讀 7979

/**
* @author vivid

* @param ssrc 加密data

* @param skey 加密金鑰

* @param ivstr 加密向量

* @description:加密操作

*/public static string encrypt(string ssrc, string skey,string ivstr) throws exception

// 判斷key是否為16位

if (skey.length() != 16)

byte raw = skey.getbytes();

secretkeyspec skeyspec = new secretkeyspec(raw, "aes");

cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding");//"演算法/模式/補碼方式" 密碼器

ivparameterspec iv = new ivparameterspec(ivstr.getbytes());//使用cbc模式,需要乙個向量iv,可增加加密演算法的強度1234567890123456

cipher.init(cipher.encrypt_mode, skeyspec, iv); //初始化

// ssrc= escapechar(ssrc);

byte encrypted = cipher.dofinal(ssrc.getbytes()); //執行最終的加密操作

string str=new base64encoder().encode(encrypted); 此處使用base64做轉碼功能,同時能起到2次加密的作用。

//獲取作業系統的型別(目前預設無mac作業系統)

properties prop = system.getproperties();

string os = prop.getproperty("os.name");

system.out.println("作業系統型別:"+os);

if(os.tolowercase().startswith("win"))else

return str;//new base64encoder().encode(encrypted);//此處使用base64做轉碼功能,同時能起到2次加密的作用。

}

解密操作:

// 解密

public static string decrypt(string ssrc, string skey,string ivstr) throws exception

// 判斷key是否為16位

if (skey.length() != 16)

byte raw = skey.getbytes("utf-8");

secretkeyspec skeyspec = new secretkeyspec(raw, "aes");

cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding");

ivparameterspec iv = new ivparameterspec(ivstr.getbytes());

cipher.init(cipher.decrypt_mode, skeyspec, iv);

byte encrypted1 = new base64decoder().decodebuffer(ssrc);//先用base64解密

try catch (exception e)

} catch (exception ex)

}

/*

* unescapechar 反向字元轉換

* * */

private static string unescapechar(string beforedecryptstring )

AES加密的坑

其實不應該算aes加密的坑,而是加密後使用的坑 正常的加密應該是 data openssl encrypt input,aes 256 cbc this key,openssl raw data,this hex iv 返回給使用者使用還要base64編碼,base64 encode data 就是...

Tcp的AES加密和AES 解密

下面的引數str就是tcp需要傳輸的內容 金鑰就是加密和解密需要提供一組相同的字元 加密模式 填充方式 將目標字串進行aes加密 iv和key均為secretkey 被加密字串 秘鑰 加密模式 填充方式 public static string toencryptaes this string st...

AES加密原理

0 aes簡介 美國國家標準技術研究所在2001年發布了高階加密標準 aes aes是乙個對稱分組密碼演算法,旨在取代des成為廣泛使用的標準。根據使用的密碼長度,aes最常見的有3種方案,用以適應不同的場景要求,分別是aes 128 aes 192和aes 256。本文主要對aes 128進行介紹...