diff --git a/src/MD5.java b/src/MD5.java new file mode 100644 index 0000000000000000000000000000000000000000..42f026c3bb97df4539c725e62ffcd3af7949eafe --- /dev/null +++ b/src/MD5.java @@ -0,0 +1,42 @@ +import java.security.MessageDigest; +import java.util.logging.Logger; + +/** + * MD5加密算法 + */ +public class MD5 { + + public static String MD5(String key) { + char hexDigits[] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' + }; + try { + byte[] btInput = key.getBytes(); + // 获得MD5摘要算法的 MessageDigest 对象 + MessageDigest mdInst = MessageDigest.getInstance("MD5"); + // 使用指定的字节更新摘要 + mdInst.update(btInput); + // 获得密文 + byte[] md = mdInst.digest(); + // 把密文转换成十六进制的字符串形式 + int j = md.length; + char str[] = new char[j * 2]; + int k = 0; + for (int i = 0; i < j; i++) { + byte byte0 = md[i]; + str[k++] = hexDigits[byte0 >>> 4 & 0xf]; + str[k++] = hexDigits[byte0 & 0xf]; + } + return new String(str); + } catch (Exception e) { + return null; + } + } + + public static void main(String[] args){ + + String MD5_String; + MD5_String = MD5("weiqingde"); + System.out.println(MD5_String); + } +} \ No newline at end of file diff --git a/src/encryption/EncrypDDD.java b/src/encryption/EncrypDDD.java new file mode 100644 index 0000000000000000000000000000000000000000..fded77328851ee016f65c69cdc89e687edf81e51 --- /dev/null +++ b/src/encryption/EncrypDDD.java @@ -0,0 +1,88 @@ +package encryption; + +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.Security; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.KeyGenerator; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; + +public class EncrypDDD { + + //KeyGenerator 提供对称密钥生成器的功能,支持各种算法 + private KeyGenerator keygen; + //SecretKey 负责保存对称密钥 + private SecretKey deskey; + //Cipher负责完成加密或解密工作 + private Cipher c; + //该字节数组负责保存加密的结果 + private byte[] cipherByte; + + public EncrypDDD() throws NoSuchAlgorithmException, NoSuchPaddingException{ + Security.addProvider(new com.sun.crypto.provider.SunJCE()); + //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常) + keygen = KeyGenerator.getInstance("DES"); + //生成密钥 + deskey = keygen.generateKey(); + //生成Cipher对象,指定其支持的DES算法 + c = Cipher.getInstance("DES"); + } + + /** + * 对字符串加密 + * @param str + * @return + * @throws InvalidKeyException + * @throws IllegalBlockSizeException + * @throws BadPaddingException + */ + public byte[] Encrytor(String str) throws InvalidKeyException, + IllegalBlockSizeException, BadPaddingException { + // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式 + c.init(Cipher.ENCRYPT_MODE, deskey); + byte[] src = str.getBytes(); + // 加密,结果保存进cipherByte + cipherByte = c.doFinal(src); + return cipherByte; + } + + /** + * 对字符串解密 + * + * @param buff + * @return + * @throws InvalidKeyException + * @throws IllegalBlockSizeException + * @throws BadPaddingException + */ + public byte[] Decryptor(byte[] buff) throws InvalidKeyException, + IllegalBlockSizeException, BadPaddingException { + // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式 + c.init(Cipher.DECRYPT_MODE, deskey); + cipherByte = c.doFinal(buff); + return cipherByte; + } + + /** + * @param args + * @throws NoSuchPaddingException + * @throws NoSuchAlgorithmException + * @throws BadPaddingException + * @throws IllegalBlockSizeException + * @throws InvalidKeyException + */ + public static void main(String[] args) throws Exception { + EncrypDES de1 = new EncrypDES(); + String msg ="郭XX-搞笑相声全集"; + byte[] encontent = de1.Encrytor(msg); + byte[] decontent = de1.Decrytor(encontent); + System.out.println("明文是:" + msg); + System.out.println("加密后:" + new String(encontent)); + System.out.println("解密后:" + new String(decontent)); + } + +} diff --git a/src/encryption/EncrypDES.java b/src/encryption/EncrypDES.java new file mode 100644 index 0000000000000000000000000000000000000000..fadcc908e49b3956366865d937e5344fc7c1a5b3 --- /dev/null +++ b/src/encryption/EncrypDES.java @@ -0,0 +1,78 @@ +package encryption; + +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.Security; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.KeyGenerator; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +/** + * DES加密算法 + * DES是一种对称加密算法 + * 采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密, + * 这种加密方法称为对称加密,也称为单密钥加密。 + * @author AI + * 2018年11月8日-下午3:05:20 + */ +public class EncrypDES { + //KeyGenerator 提供对称密钥生成器的功能,支持各种算法 + private KeyGenerator keygen; + //SecretKey 负责保存对称密钥 + private SecretKey deskey; + //Cipher负责完成加密或解密工作 + private Cipher c; + //该字节数组负责保存加密的结果 + private byte[] cipherByte; + + /** + * 被创建时初始化参数 + */ + public EncrypDES() throws NoSuchAlgorithmException, NoSuchPaddingException { + Security.addProvider(new com.sun.crypto.provider.SunJCE()); + //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常) + keygen = KeyGenerator.getInstance("DES"); + //生成密钥 + deskey = keygen.generateKey(); + //生成Cipher对象,指定其支持的DES算法 + c = Cipher.getInstance("DES"); + } + + /** + * 对字符串加密 + * @param str 需要加密的字符串 + * @return + * @throws InvalidKeyException + * @throws IllegalBlockSizeException + * @throws BadPaddingException + */ + public byte[] Encrytor(String str) throws InvalidKeyException, + IllegalBlockSizeException, BadPaddingException { + // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式 + c.init(Cipher.ENCRYPT_MODE, deskey); + byte[] src = str.getBytes(); + System.out.println(new String(src)); + // 加密,结果保存进cipherByte + cipherByte = c.doFinal(src); + return cipherByte; + } + + /** + * 做解密 + * @param buff + * @return + * @throws InvalidKeyException + * @throws IllegalBlockSizeException + * @throws BadPaddingException + */ + public byte[] Decrytor(byte[] buff) throws InvalidKeyException, + IllegalBlockSizeException, BadPaddingException { + c.init(Cipher.ENCRYPT_MODE, deskey); + cipherByte = c.doFinal(buff); + return cipherByte; + } + +} diff --git a/src/encryption/MD5.java b/src/encryption/MD5.java new file mode 100644 index 0000000000000000000000000000000000000000..0c62b802104c5cf751d7e1bc7d3e1169da5e4e23 --- /dev/null +++ b/src/encryption/MD5.java @@ -0,0 +1,30 @@ +package encryption; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import sun.misc.BASE64Encoder; + +public class MD5 { + + /**利用MD5进行加密*/ + public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{ + //确定计算方法 + MessageDigest md5=MessageDigest.getInstance("MD5"); + BASE64Encoder base64en = new BASE64Encoder(); + //加密后的字符串 + String newstr=base64en.encode(md5.digest(str.getBytes("utf-8"))); + return newstr; + } + + /**判断用户密码是否正确 + *newpasswd 用户输入的密码 + *oldpasswd 正确密码*/ + public boolean checkpassword(String newpasswd,String oldpasswd) throws NoSuchAlgorithmException, UnsupportedEncodingException{ + if(EncoderByMd5(newpasswd).equals(oldpasswd)) + return true; + else + return false; + } +} diff --git a/src/encryption/TanXin.java b/src/encryption/TanXin.java new file mode 100644 index 0000000000000000000000000000000000000000..232a4785a10c6bd25c346b5f734634ab96ea8427 --- /dev/null +++ b/src/encryption/TanXin.java @@ -0,0 +1,27 @@ +package encryption; + +public class TanXin { + + public static void main(String[] args) { + String str = sub(2,"15735896"); + System.out.println(str); + } + public static String sub(int x,String num) { + String tNum = num; + boolean has = false; + for(int j=0;jtNum.charAt(i+1)) { + tNum = tNum.substring(0, i)+tNum.substring(i+1); + has = true; + break; + } + } + if(!has) { + tNum = tNum.substring(0, tNum.length()); + } + } + return tNum; + } + +} diff --git a/src/encryption/TestEncryption.java b/src/encryption/TestEncryption.java new file mode 100644 index 0000000000000000000000000000000000000000..d30b071894130c60b531fd6e1aa0adde30674a38 --- /dev/null +++ b/src/encryption/TestEncryption.java @@ -0,0 +1,127 @@ +package encryption; + +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; + +import org.junit.jupiter.api.Test; + +public class TestEncryption { + + @Test + public void testDes() throws NoSuchAlgorithmException, NoSuchPaddingException { + EncrypDES e1 = new EncrypDES(); + try { + String str = "ccc"; + byte[] b1 = e1.Encrytor("abc"); + System.out.println("#####加密之后:" + new String(b1)); + byte[] b2 = e1.Decrytor(b1); + System.out.println("#####解密:" + new String(b2)); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + e.printStackTrace(); + } catch (BadPaddingException e) { + e.printStackTrace(); + } + } + + @Test + public void tess() throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException { + EncrypDES de1; + try { + de1 = new EncrypDES(); + String msg = "郭XX-搞笑相声全集"; + byte[] encontent = de1.Encrytor(msg); + byte[] decontent = de1.Decrytor(encontent); + System.out.println("明文是:" + msg); + System.out.println("加密后:" + new String(encontent)); + System.out.println("解密后:" + new String(decontent)); + } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { + e.printStackTrace(); + } + + } + + @Test + public void test2() throws NoSuchAlgorithmException, NoSuchPaddingException { + EncrypDES e1 = new EncrypDES(); + try { + byte[] b1 = e1.Encrytor("abc"); + System.out.println("#####加密之后:" + new String(b1)); + byte[] b2 = e1.Decrytor(b1); + System.out.println("#####解密:" + new String(b2)); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + e.printStackTrace(); + } catch (BadPaddingException e) { + e.printStackTrace(); + } + } + + @Test + public void testMD5() { + MD5 md = new MD5(); + String str = "apple"; + try { + String newString = md.EncoderByMd5(str); + System.out.println(newString); + } catch (Exception e) { + + } + } + + @Test + public void asd() { + String str = removeKDigits1("1432219", 3); + System.out.println(str); + } + + public static String removeKdigits(String num, int k) { + int len = num.length() - k; + char[] stack = new char[len]; + for (int i = 0; i < num.length(); i++) { + char c = num.charAt(i); + while (k > 0 && stack[k - 1] > c) { + k--; + } + stack[k + 1] = c; + } + int x = 0; + while (x < len && stack[x] == '0') { + x++; + } + return x == len ? "0" : new String(stack, x, len - x); + } + + public static String removeKDigits1(String num,int k){ + + // 新整数的最终长度 = 原整数长度 - k + int newLength = num.length()- k; + // 创建一个栈,用于接收所有的数字 + char[] stack =new char[num.length()]; + int top =0; + for(int i =0; i < num.length();++i){ + // 遍历当前数字 + char c = num.charAt(i); + // 当栈顶数字大于遍历到的当前数字,栈顶数字出栈(相当于删除数字) + while(top >0&& stack[top - 1]> c && k >0){ + top -=1; + k -=1; + } + // 遍历到的当前数字入栈 + stack[top++]= c; + } + // 找到栈中第一个非零数字的位置,以此构建新的整数字符串 + int offset =0; + while(offset < newLength && stack[offset]=='0'){ + offset++; + } + return offset == newLength ?"0" :new String(stack, offset, newLength - offset); + } + +} diff --git a/src/testMD5.java b/src/testMD5.java new file mode 100644 index 0000000000000000000000000000000000000000..25cc028aaa5807d11aaced67d66007c0ee77103f --- /dev/null +++ b/src/testMD5.java @@ -0,0 +1,52 @@ +import java.security.MessageDigest; +import java.sql.Time; + +import org.junit.jupiter.api.Test; + +public class testMD5 { + private static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e","f" }; + + public static void main(String[] args) throws Exception { + String str = "abc123"; + String str1 = MD5Encode(str); + System.out.println(str1); + } + + public static String byteArrayTOHexDigitsString(byte[] b) { + StringBuffer sb = new StringBuffer(); + for(int i = 0;i < b.length; i++) { + sb.append(buteTOHexDigitsString(b[i])); + } + return null; + } + + public static String buteTOHexDigitsString(byte ss) { + int n = ss; + if(n < 0) { + n+=256; + } + int d1 = n/16; + int d2 = n%16; + return hexDigits[d1]+hexDigits[d2]; + } + + public static String MD5Encode(String pwd) throws Exception{ + String result = null; + try { + result = new String(pwd); + MessageDigest md = MessageDigest.getInstance("MD5"); + System.out.println("****************"+result); + result = byteArrayTOHexDigitsString(md.digest(result.getBytes())); + System.out.println("$$$$$$$$$$$$$44444"+result); + }catch(Exception e) { + e.printStackTrace(); + } + System.out.println("#############"+result); + return result; + } + + @Test + public void timmme() { + System.out.println(new Time(System.currentTimeMillis())); + } +}