zoukankan      html  css  js  c++  java
  • AES加密

    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.util.Scanner;
    
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    
    
    public class SymmetricEncoder {
    				
    	
    	
    	public static String AESEncode(String encodeRules, String content) {
    		try {
    			
    			KeyGenerator keygen = KeyGenerator.getInstance("AES");
    			
    			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    			secureRandom.setSeed(encodeRules.getBytes());
    			keygen.init(128,secureRandom);
    			
    			SecretKey original_key = keygen.generateKey();
    			
    			byte[] raw = original_key.getEncoded();
    			
    			SecretKey key = new SecretKeySpec(raw, "AES");
    			
    			Cipher cipher = Cipher.getInstance("AES");
    			
    			cipher.init(Cipher.ENCRYPT_MODE, key);
    			
    			byte[] byte_encode = content.getBytes("utf-8");
    			
    			byte[] byte_AES = cipher.doFinal(byte_encode);
    			
    			String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
    			
    			return AES_encode;
    		} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    		} catch (NoSuchPaddingException e) {
    			e.printStackTrace();
    		} catch (InvalidKeyException e) {
    			e.printStackTrace();
    		} catch (IllegalBlockSizeException e) {
    			e.printStackTrace();
    		} catch (BadPaddingException e) {
    			e.printStackTrace();
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    
    		
    		return null;
    	}
    
    	
    	public static String AESDncode(String encodeRules, String content) {
    		try {
    			
    			KeyGenerator keygen = KeyGenerator.getInstance("AES");
    			
    			SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    			secureRandom.setSeed(encodeRules.getBytes());
    			keygen.init(128,secureRandom);
    			
    			SecretKey original_key = keygen.generateKey();
    			
    			byte[] raw = original_key.getEncoded();
    			
    			SecretKey key = new SecretKeySpec(raw, "AES");
    			
    			Cipher cipher = Cipher.getInstance("AES");
    			
    			cipher.init(Cipher.DECRYPT_MODE, key);
    			
    			byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
    			
    			byte[] byte_decode = cipher.doFinal(byte_content);
    			String AES_decode = new String(byte_decode, "utf-8");
    			return AES_decode;
    		} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    		} catch (NoSuchPaddingException e) {
    			e.printStackTrace();
    		} catch (InvalidKeyException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (IllegalBlockSizeException e) {
    			e.printStackTrace();
    		} catch (BadPaddingException e) {
    			e.printStackTrace();
    		}
    
    		
    		return null;
    	}
    }
    

      

  • 相关阅读:
    node之body-parser的使用
    node解决跨域问题
    node之post提交上传
    HDU 6397(容斥原理)
    HDU 3374(最小最大表示法+KMP)
    HDU 6396(优先队列+思维)
    HDU 6395(矩阵快速幂)
    HDU 6370(并查集)
    HDU 6356(线段树)
    HDU 6354(计算几何)
  • 原文地址:https://www.cnblogs.com/woftlcj/p/10109400.html
Copyright © 2011-2022 走看看