茉莉网

aes 解密 报错 解决Linux操作系统下AES解密失败的问题

2017年11月09日 来源:aes 解密 报错 大字体小字体

高分求大神帮忙编写一个简单的在Linux下AES

Oracle VirtualBox 50 正式版发布 Linux新闻

  Java代码 

  importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.security.InvalidKeyException;importjava.security.NoSuchAlgorithmException;importjava.security.SecureRandom;importjavax.crypto.BadPaddingException;importjavax.crypto.Cipher;importjavax.crypto.IllegalBlockSizeException;importjavax.crypto.KeyGenerator;importjavax.crypto.NoSuchPaddingException;importjavax.crypto.SecretKey;importjavax.crypto.spec.SecretKeySpec;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder; publicclassSecurityAES{    privatefinalstaticStringencoding="UTF-8";     /**    *AES加密    *     *@paramcontent    *@parampassword    *@return    */    publicstaticStringencryptAES(Stringcontent,Stringpassword){        byte[]encryptResult=encrypt(content,password);        StringencryptResultStr=parseByte2HexStr(encryptResult);        //BASE64位加密        encryptResultStr=ebotongEncrypto(encryptResultStr);        returnencryptResultStr;    }     /**    *AES解密    *     *@paramencryptResultStr    *@parampassword    *@return    */    publicstaticStringdecrypt(StringencryptResultStr,Stringpassword){        //BASE64位解密        Stringdecrpt=ebotongDecrypto(encryptResultStr);        byte[]decryptFrom=parseHexStr2Byte(decrpt);        byte[]decryptResult=decrypt(decryptFrom,password);        returnnewString(decryptResult);    }         /**    *加密字符串    */    publicstaticStringebotongEncrypto(Stringstr){        BASE64Encoderbase64encoder=newBASE64Encoder();        Stringresult=str;        if(str!=null&&str.length()>0){            try{                byte[]encodeByte=str.getBytes(encoding);                result=base64encoder.encode(encodeByte);            }catch(Exceptione){                e.printStackTrace();            }        }        //base64加密超过一定长度会自动换行需要去除换行符        returnresult.replaceAll("\r\n","").replaceAll("\r","").replaceAll("\n","");    }     /**    *解密字符串    */    publicstaticStringebotongDecrypto(Stringstr){        BASE64Decoderbase64decoder=newBASE64Decoder();        try{            byte[]encodeByte=base64decoder.decodeBuffer(str);            returnnewString(encodeByte);        }catch(IOExceptione){            e.printStackTrace();            returnstr;        }    }    /**      *加密      *       *@paramcontent需要加密的内容      *@parampassword 加密密码      *@return      */      privatestaticbyte[]encrypt(Stringcontent,Stringpassword){               try{                                  KeyGeneratorkgen=KeyGenerator.getInstance("AES");                     //防止linux下随机生成key                    SecureRandomsecureRandom=SecureRandom.getInstance("SHA1PRNG");                       secureRandom.setSeed(password.getBytes());                       kgen.init(128,secureRandom);                    //kgen.init(128,newSecureRandom(password.getBytes()));                       SecretKeysecretKey=kgen.generateKey();                       byte[]enCodeFormat=secretKey.getEncoded();                       SecretKeySpeckey=newSecretKeySpec(enCodeFormat,"AES");                       Ciphercipher=Cipher.getInstance("AES");//创建密码器                       byte[]byteContent=content.getBytes("utf-8");                       cipher.init(Cipher.ENCRYPT_MODE,key);//初始化                       byte[]result=cipher.doFinal(byteContent);                       returnresult;//加密               }catch(NoSuchAlgorithmExceptione){                       e.printStackTrace();               }catch(NoSuchPaddingExceptione){                       e.printStackTrace();               }catch(InvalidKeyExceptione){                       e.printStackTrace();               }catch(UnsupportedEncodingExceptione){                       e.printStackTrace();               }catch(IllegalBlockSizeExceptione){                       e.printStackTrace();               }catch(BadPaddingExceptione){                       e.printStackTrace();               }               returnnull;       }        /**解密      *@paramcontent 待解密内容      *@parampassword解密密钥      *@return      */      privatestaticbyte[]decrypt(byte[]content,Stringpassword){               try{                        KeyGeneratorkgen=KeyGenerator.getInstance("AES");                    //防止linux下随机生成key                     SecureRandomsecureRandom=SecureRandom.getInstance("SHA1PRNG");                        secureRandom.setSeed(password.getBytes());                        kgen.init(128,secureRandom);                     //kgen.init(128,newSecureRandom(password.getBytes()));                        SecretKeysecretKey=kgen.generateKey();                        byte[]enCodeFormat=secretKey.getEncoded();                        SecretKeySpeckey=newSecretKeySpec(enCodeFormat,"AES");                                    Ciphercipher=Cipher.getInstance("AES");//创建密码器                       cipher.init(Cipher.DECRYPT_MODE,key);//初始化                       byte[]result=cipher.doFinal(content);                       returnresult;//加密               }catch(NoSuchAlgorithmExceptione){                       e.printStackTrace();               }catch(NoSuchPaddingExceptione){                       e.printStackTrace();               }catch(InvalidKeyExceptione){                       e.printStackTrace();               }catch(IllegalBlockSizeExceptione){                       e.printStackTrace();               }catch(BadPaddingExceptione){                       e.printStackTrace();               }               returnnull;       }       /**将二进制转换成16进制      *@parambuf      *@return      */      publicstaticStringparseByte2HexStr(bytebuf[]){               StringBuffersb=newStringBuffer();               for(inti=0;i

为什么Android使用弱加密算法?

  aes是一种对称加密算法。应该双方都持有一个密钥。

      atcom.sun.crypto.provider.SunJCE_f.b(DashoA13*..)    atcom.sun.crypto.provider.SunJCE_f.b(DashoA13*..)    atcom.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)    atjavax.crypto.Cipher.doFinal(DashoA13*..)    atchb.test.crypto.AESUtils.crypt(AESUtils.java:386)    atchb.test.crypto.AESUtils.AesDecrypt(AESUtils.java:254)    atchb.test.crypto.AESUtils.main(AESUtils.java:40) 

恭贺东方佳联QSC品牌连获三个TEC大奖提名

  [AES解密]、右边是放密文,解密后,解密的内容在右边。密码加密的那一定要相同密码解密,要不然内容为空。

  研究论文:http://research.microsoft.com/en-us/projects/cryptanalysis/aesbc.pdf

VeraCrypt 最佳免费开源跨平台的文件加密工

TCPDUMP命令行操作 抓包筛选高级筛选

嵌入式安全通信系统的fpga实现 linux c

使用 VeraCrypt 加密闪存盘 Linux教程

能强大界面友好的加密程序 TrueCrypt

相关内容

编辑精选

Copyright © 2015 茉莉网 http://www.szmlwh.cn. All rights reserved.