亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

AES加密解密技術(shù)

villainhr / 3181人閱讀

摘要:一般都聽(tīng)說(shuō)過(guò)加密技術(shù),加密確實(shí)強(qiáng)大。補(bǔ)充剛學(xué)習(xí)的時(shí)候,看到網(wǎng)上有人把密鑰字節(jié)碼轉(zhuǎn)變成字符串保存,然后用來(lái)加密解密的時(shí)候,很有可能出錯(cuò)。因?yàn)椴煌Z(yǔ)言的原因,字符串轉(zhuǎn)變成字節(jié)碼就有可能不再是原來(lái)的字節(jié)碼了。

一般都聽(tīng)說(shuō)過(guò)MD5加密技術(shù),MD5加密確實(shí)強(qiáng)大。但那是非對(duì)稱加密技術(shù),也就是說(shuō)從密文變成明文不是那么容易。當(dāng)我們需要一種對(duì)稱性加密技術(shù),MD5就不適用了。比如視頻加密解密,此時(shí)就可以用ASE加密解密了。

AES有更安全、靈活、有效率等優(yōu)點(diǎn),使用范圍也很廣,所以今天就講講AES加密如何使用,這里以java開(kāi)展。

密鑰

就是把明文轉(zhuǎn)換為密文,密文轉(zhuǎn)換為明文的一把鑰匙。接下來(lái)我們會(huì)用ASE加密技術(shù)生成一把密鑰。

public void generateSecretKey() {
    KeyGenerator keyGenerator = null;
    FileOutputStream fos = null;
    try {
        keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);//size
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] keyBytes = secretKey.getEncoded();
        fos = new FileOutputStream("key");
        fos.write(keyBytes);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

keyGenerator.init(128); 這里是初始化密鑰的長(zhǎng)度。翻看底層代碼,發(fā)現(xiàn)密鑰支持的長(zhǎng)度為:128, 192 or 256。我們這里是128位的長(zhǎng)度,也就是16byte。

補(bǔ)充: 剛學(xué)習(xí)的時(shí)候,看到網(wǎng)上有人把密鑰(字節(jié)碼)轉(zhuǎn)變成字符串保存,然后用來(lái)加密解密的時(shí)候,很有可能出錯(cuò)。因?yàn)椴煌Z(yǔ)言的原因,字符串轉(zhuǎn)變成字節(jié)碼就有可能不再是原來(lái)的字節(jié)碼了。

密鑰生成后,在項(xiàng)目目錄下可以找到一個(gè)key文件。

加密

將明文和密鑰一起,作為參數(shù)傳入。

/**
 * ASE 加密
 * @param str 明文
 * @param key 秘鑰
 * @return
 */
public static String enStr(String str, byte[] key) {
    Cipher cipher = null;
    SecretKey generateKey = null;
    try {
        generateKey = new SecretKeySpec(key, "AES");
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, generateKey);
        byte[] resultBytes = cipher.doFinal(str.getBytes());
        return Hex.encodeHexString(resultBytes);
    } catch (Exception e) {
        logger.error("AES加密出錯(cuò)", e);
    }
    return null;
}
解密

解密就是加密的互逆過(guò)程,所以代碼很類似。

 /**
 * 解密
 * @param key 秘鑰
 * @param str 密文
 * @return
 */
public static String deStr(String str, byte[] key) {
    Cipher cipher = null;
    SecretKey generateKey = null;
    try {
        generateKey = new SecretKeySpec(key, "AES");
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, generateKey);
        byte[] result = Hex.decodeHex(str.toCharArray());
        return new String(cipher.doFinal(result));

    } catch(Exception e) {
        logger.error("ASE解密出錯(cuò)", e);
    }
    return null;
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/70058.html

相關(guān)文章

  • 【大量干貨】史上最完整的Tengine HTTPS原理解析、實(shí)踐與調(diào)試

    摘要:內(nèi)容主要有四個(gè)方面趨勢(shì)基礎(chǔ)實(shí)踐調(diào)試。一趨勢(shì)這一章節(jié)主要介紹近幾年和未來(lái)的趨勢(shì),包括兩大瀏覽器和對(duì)的態(tài)度,以及淘寶天貓和阿里云的實(shí)踐情況。完整性是指為了避免網(wǎng)絡(luò)中傳輸?shù)臄?shù)據(jù)被非法篡改,使用算法來(lái)保證消息的完整性。 摘要: 本文邀請(qǐng)阿里云CDN HTTPS技術(shù)專家金九,分享Tengine的一些HTTPS實(shí)踐經(jīng)驗(yàn)。內(nèi)容主要有四個(gè)方面:HTTPS趨勢(shì)、HTTPS基礎(chǔ)、HTTPS實(shí)踐、HTTPS...

    snowell 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<