美文网首页
AES对称密钥加密

AES对称密钥加密

作者: 账号已被注销 | 来源:发表于2019-07-05 22:45 被阅读0次

    源码地址放在文章末尾


    package Mr.He;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import java.security.Key;
    import java.security.SecureRandom;
    import java.util.Base64;
    
    /**
     * AES加密演示
     * @author wishforyou.xia@gmail.com
     * @date 2019/7/3 23:24
     */
    public class AesDemo {
        private static final String STRING = "missyou";
    
        public static void main(String[] args) throws Exception {
            Key key = createKey();
            String keyString = new String(Base64.getEncoder().encode(key.getEncoded()));
            System.out.println("密钥:" + keyString);
    
            String encodeString = encode(key);
            System.err.println("加密后的字符串为:" + encodeString);
    
            String decodeString = decode(key,encodeString);
            System.out.println("还原后的字符串为:" + decodeString);
        }
    
        private static Key createKey() throws Exception{
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128,new SecureRandom("missyou".getBytes()));
            SecretKey secretKey = keyGenerator.generateKey();
            return secretKey;
        }
    
        private static String encode(Key key) throws Exception {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE,key);
            byte[] dataByte = cipher.doFinal(STRING.getBytes());
            byte[] encodeByte = Base64.getEncoder().encode(dataByte);
            String data = new String(encodeByte);
            return data;
        }
    
        private static String decode(Key key, String string) throws Exception {
            byte[] decodeByte = Base64.getDecoder().decode(string.getBytes());
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE,key);
            byte[] dataByte = cipher.doFinal(decodeByte);
            String data = new String(dataByte);
            return data;
        }
    }
    
    /**
    *  源码地址github
    */
    https://github.com/HeQuanX/learning
    

    相关文章

      网友评论

          本文标题:AES对称密钥加密

          本文链接:https://www.haomeiwen.com/subject/jqnyhctx.html