⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 custencyauti.java

📁 对一个字符串通过UDP进行加密解密
💻 JAVA
字号:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.net.*;

public class CustEncyAuti {
	//定义 加密算法,可用 DES,DESede,Blowfish
	private static String Algorithm="DES"; 

	public CustEncyAuti(){}
	
	public static byte[] encryp(byte[] keybyte, byte[] src)
	{
    	try
    	{
            //生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

            //加密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal(src);
        }
        catch(Exception e){e.printStackTrace();}
        return null;
    }

	public static void main(String[] args) throws Exception
	{
		// TODO: Add your code here
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
        final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10, 0x40, 0x38
                               , 0x28, 0x25, 0x79, 0x51, (byte)0xCB, (byte)0xDD, 0x55, 0x66
                               , 0x77, 0x29, 0x74, (byte)0x98, 0x30, 0x40, 0x36, (byte)0xE2};	//24字节的密钥
        String szSrc = "This is a test!";
        System.out.println("加密前的字符串:" + szSrc);
		byte[] encoded = encryp(keyBytes, szSrc.getBytes());        
        System.out.println("加密后的字符串:" + new String(encoded));
		
		DatagramSocket ds=new DatagramSocket();
		DatagramPacket dp=new DatagramPacket(encoded,encoded.length,
						  InetAddress.getByName("127.0.0.1"),3000);
		ds.send(dp);
		ds.close();
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -