• 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;
    	}
    }
    

      

  • 相关阅读:
    BZOJ 1707 [Usaco2007 Nov]tanning分配防晒霜
    BZOJ2697:特技飞行
    Bridging signals(NlogN最长上升子序列)
    HDU 4291 A Short problem(矩阵+循环节)
    计蒜之道 初赛第一场B 阿里天池的新任务(简单)
    1008: [HNOI2008]越狱
    1022: [SHOI2008]小约翰的游戏John
    1192: [HNOI2006]鬼谷子的钱袋
    2456: mode
    BZOJ-1968
  • 原文地址:https://www.cnblogs.com/woftlcj/p/10109400.html
Copyright © 2020-2023  润新知