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

      

  • 相关阅读:
    系统架构
    创造HTTPS的是个神
    为Chrome开发插件提高工作效率
    Javascript 控制style 小结
    svcutil 生成代理类时的问题
    xeam Build Definition Extension uninstall 卸载
    看看 Delphi XE2 为 VCL 提供的 14 种样式
    FireMonkey 绘图(1)
    终于, Delphi XE2 携带 GDI+ 库了
    关于禁止程序重复启动的另一种需要与实现
  • 原文地址:https://www.cnblogs.com/woftlcj/p/10109400.html
Copyright © 2011-2022 走看看