<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Dict.CN 在線詞典, 英語學(xué)習(xí), 在線翻譯

    都市淘沙者

    荔枝FM Everyone can be host

    統(tǒng)計(jì)

    留言簿(23)

    積分與排名

    優(yōu)秀學(xué)習(xí)網(wǎng)站

    友情連接

    閱讀排行榜

    評(píng)論排行榜

    關(guān)于AES(16字節(jié))加密解密算法的java實(shí)現(xiàn)

    關(guān)于AES(16字節(jié))加密解密算法的java實(shí)現(xiàn)
    根據(jù)指定的字符串來實(shí)現(xiàn)AES加密和解密,密匙可參數(shù)化配置,加密后數(shù)據(jù)的前兩個(gè)字節(jié)是數(shù)據(jù)包的長度,加密算法選用AES
    /ECB /PKCS5Padding。即采用標(biāo)準(zhǔn)AES算法,把全報(bào)文按照每塊16字節(jié)分塊進(jìn)行加解密,對(duì)于不足16字節(jié)的數(shù)據(jù)塊按照PKCS5方式補(bǔ)充,缺少N 個(gè)字節(jié)則把缺少的N個(gè)字節(jié)都以N來填充,最后一個(gè)數(shù)據(jù)塊剛好16字節(jié),則增加一個(gè)全部字節(jié)都填充為16的數(shù)據(jù)塊。
    =====================================
    AesHandler.java
    =====================================
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.security.GeneralSecurityException;
    import java.util.List;

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

    import org.dom4j.Document;
    import org.dom4j.io.SAXReader;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;

    public class AesHandler {

    private String plainTextFile = "src/encode.txt";// 明文文件
    private String cipherTextFile = "src/aesDecode.txt";// 密文文件
    private String keyFile = "src/aesKey.xml";// 加密密匙文件

    /**
    *
    @param keyName
    *            加密密匙名
    @return
    */
    public SecretKeySpec createKey(String keyName) {
    SAXReader reader 
    = new SAXReader();
    Document doc;
    SecretKeySpec skeySpec 
    = null;
    byte[] raw;
    File keyXmlFile 
    = new File(keyFile);
    try {
    doc 
    = reader.read(keyXmlFile);
    Element root 
    = doc.getRootElement();
    List items 
    = root.elements("key16");
    for(int i = 0; i < items.size(); i++){
    Element node 
    = (Element)items.get(i);
    if(keyName.equalsIgnoreCase(node.attributeValue("name"))){
    raw 
    = node.attributeValue("value").getBytes("ASCII");
    skeySpec 
    = new SecretKeySpec(raw, "AES");
    return skeySpec;
    }
    }
    catch (DocumentException e) {
    e.printStackTrace();
    // throw new HiException(HiMessageCode.ERR_PARSE_FAILURE, _fmtFile,
    // e);
    catch (MalformedURLException e) {
    e.printStackTrace();
    catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    return skeySpec;
    }

    /**
    *
    @param code
    *            選擇加密或解密操作碼
    @param keyFileStr
    *            加密密匙文件
    @param plainFile 明文文件
    @param cipherFile 密文文件
    @param keyName 密匙名          
    */
    public void run(String code, String keyName) {
    int mode = Cipher.ENCRYPT_MODE;
    InputStream in 
    = null;
    OutputStream out 
    = null;
    try {
    SecretKeySpec skeySpec 
    = createKey(keyName);
    Cipher cipher 
    = Cipher.getInstance("AES/ECB/PKCS5Padding");
    if ("DECODE".equals(code)) {
    mode 
    = Cipher.DECRYPT_MODE;
    in 
    = new FileInputStream(cipherTextFile);
    out 
    = new FileOutputStream(plainTextFile);
    cipher.init(mode, skeySpec);
    decrypt(in, out, cipher);
    // 解密
    else {
    in 
    = new FileInputStream(plainTextFile);
    out 
    = new FileOutputStream(cipherTextFile);
    cipher.init(mode, skeySpec);
    encrypt(in, out, cipher);
    // 加密
    }
    in.close();
    out.close();
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    public static byte[] shortToByteArray(int valor) {
    byte[] result = new byte[2];
    for (int i = 0; i < result.length; i++) {
    result[
    1 - i] = (byte) (valor & 0xFF);
    valor 
    = valor >> 8;
    }
    return result;
    }

    /**
    * 加密算法
    *
    @param in
    *            明文數(shù)據(jù)流
    @param out
    *            密文數(shù)據(jù)流
    @param cipher
    @throws IOException
    @throws ShortBufferException
    @throws GeneralSecurityException
    */
    public static void encrypt(InputStream in, OutputStream out, Cipher cipher)
    throws IOException, ShortBufferException, GeneralSecurityException {
    int blockSize = cipher.getBlockSize();
    int outputSize = cipher.getOutputSize(blockSize);
    byte[] inBytes = new byte[blockSize];
    byte[] outBytes = new byte[outputSize];
    byte[] appendAllBytes = new byte[blockSize];
    byte[] appendBytes = new byte[blockSize];
    int inLength = 0;
    int length1 = in.available();
    boolean more = true;
    int yushu = length1 % 16;
    if (yushu == 0) {
    for (int i = 0; i < 16; i++) {
    appendAllBytes[i] 
    = new Integer(16).byteValue();
    }
    out.write(shortToByteArray(length1 
    + blockSize));
    else {
    int N = blockSize - yushu;
    out.write(shortToByteArray(length1 
    + N));
    }
    while (more) {
    inLength 
    = in.read(inBytes);
    if (inLength == blockSize) {
    int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
    out.write(outBytes, 
    0, outLength);
    else
    more 
    = false;
    }
    if (inLength > 0 && inLength < blockSize) {// 不足16字節(jié)的數(shù)據(jù)塊按照PKCS5方式補(bǔ)充,缺少N個(gè)字節(jié)則把缺少的N個(gè)字節(jié)都以N來填充
    int N = blockSize - inLength;
    for (int M = inLength; M < blockSize; M++) {
    inBytes[M] 
    = new Integer(N).byteValue();
    }
    outBytes 
    = cipher.doFinal(inBytes, 0, inLength);
    out.write(outBytes);
    else if (inLength == 0) {// 如果正好是16位,則增加一個(gè)全部字節(jié)都填充為16的數(shù)據(jù)塊
    int outLength = cipher.doFinal(appendBytes, 0, blockSize, outBytes);
    out.write(outBytes, 
    0, outLength);
    }
    out.flush();
    }

    /**
    * 解密算法
    *
    @param in
    *            密文數(shù)據(jù)流
    @param out
    *            明文數(shù)據(jù)流
    @param cipher
    @throws IOException
    @throws ShortBufferException
    @throws GeneralSecurityException
    */
    public static void decrypt(InputStream in, OutputStream out, Cipher cipher)
    throws IOException, ShortBufferException, GeneralSecurityException {
    int blockSize = cipher.getBlockSize();
    int outputSize = cipher.getOutputSize(blockSize);
    byte[] inBytes = new byte[blockSize];
    byte[] outBytes = new byte[outputSize];
    byte[] dataLength = new byte[2];
    int inLength = 0;
    boolean more = true;
    in.read(dataLength);
    // 將數(shù)據(jù)包的長度讀入到byte數(shù)組中
    while (more) {
    inLength 
    = in.read(inBytes);
    if (inLength == blockSize) {
    int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
    out.write(outBytes, 
    0, outLength);
    else
    more 
    = false;
    }
    if (inLength > 0) {
    outBytes 
    = cipher.doFinal(inBytes, 0, inLength);
    else {
    outBytes 
    = cipher.doFinal();
    }
    out.write(outBytes);
    out.flush();
    }

    public String getPlainTextFile() {
    return plainTextFile;
    }

    public void setPlainTextFile(String plainTextFile) {
    this.plainTextFile = plainTextFile;
    }

    public String getCipherTextFile() {
    return cipherTextFile;
    }

    public void setCipherTextFile(String cipherTextFile) {
    this.cipherTextFile = cipherTextFile;
    }
    public String getKeyFile() {
    return keyFile;
    }

    public void setKeyFile(String keyFile) {
    this.keyFile = keyFile;
    }
    }
    =======================================================================
    aesKey.xml文件
    =============================
    <?xml version="1.0" encoding="GB2312"?>
    <KEY>
    <key16 name="test" value="16BAESBocHK2Mcis"></key16>
    </KEY>
    =======================================================================
    測試:
    加密密鑰: 16BAESBocHK2Mcis
    明文: ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$
    %!&*()_+-=:;'?,./
    密文為:  
    =========================From 001 Lines to 016============================
    Debug: 
    -1--2--3--4--5--6--7--8--9-HEX-1--2--3--4--5--6   ---ASCII Value--
    0000100 40 0e 49 32 64 f8 07 c0 fd 22 80 49 6f 80 32   .@.I2d..例".Io.2
    0000250 c5 97 73 f4 55 83 fb 59 6d 85 3c 82 1c 30 a3   P艞s.U凔Ym.<..0.
    00003: d8 49 45 fc e2 83 3d 39 a2 d2 6e 06 4f a6 94 1c   .IE .=9⒁n.O .
    0000419 2d 38 6d 81 e7 34 3b f7 5d 2b ff 83 a6 14 c2   .-8m佺4;.]+?..
    00005: 2d 0e                                            >-.
    ====================================end================================

    posted on 2011-03-16 18:11 都市淘沙者 閱讀(4441) 評(píng)論(0)  編輯  收藏 所屬分類: 加密解密/其他分類

    主站蜘蛛池模板: 又黄又大又爽免费视频| 国产成人精品无码免费看| 国产成人精品日本亚洲语音| 久久亚洲国产最新网站| 亚洲fuli在线观看| 77777午夜亚洲| 亚洲人成网站999久久久综合| 亚洲一区二区三区乱码在线欧洲| 亚洲中文无码a∨在线观看| 亚洲成AV人综合在线观看| 激情综合亚洲色婷婷五月APP| 亚洲一区二区三区播放在线| 亚洲粉嫩美白在线| 亚洲精品无码专区在线| 国产亚洲精品国产福利在线观看| 免费大片黄在线观看| 一区二区三区在线免费观看视频| 午夜成人无码福利免费视频| GOGOGO免费观看国语| 久久精品国产大片免费观看| 四虎最新永久免费视频| 免费中文熟妇在线影片| 日本一线a视频免费观看| 无码欧精品亚洲日韩一区夜夜嗨| 亚洲不卡AV影片在线播放| 亚洲一区二区三区偷拍女厕| 久久久久亚洲av无码尤物| 91亚洲精品自在在线观看| 亚洲成aⅴ人片久青草影院按摩| 粉色视频免费入口| a级片在线免费看| 精品国产sm捆绑最大网免费站| 色婷婷7777免费视频在线观看 | 国产高清不卡免费视频| 57pao国产成视频免费播放| 好男人www免费高清视频在线| 午夜亚洲福利在线老司机| 亚洲AV永久无码区成人网站| 亚洲导航深夜福利| 羞羞漫画小舞被黄漫免费| 亚洲AV一二三区成人影片|