AES对称加密例子
- 编辑:admin -AES对称加密例子
key); byte[] cipherText=cipher.doFinal(plainText); System.out.println("Finish encryption:"); System.out.println(new String(cipherText, k = len-1; for (int i=0; i len; i++) { System.out.println(str[i]); // Determine if pattern exists in input matcher = pattern.matcher(str[i]); matchFound = matcher.find(); if (matchFound) set[j++] = str[i]; else set[k--] = str[i]; } s.setEnabledCipherSuites(set); str=s.getEnabledCipherSuites(); System.out.println("Available Suites after Set:"); for (int i=0; i str.length; i++) System.out.println(str[i]); OutputStream out = s.getOutputStream(); BufferedReader in = new BufferedReader ( new InputStreamReader(s.getInputStream())); String mesg = in.readLine(); System.out.println("Socket message: " + mesg); in.close(); } catch (Exception e) { System.out.println("Exception" + e); } } } import java.io.*; import java.security.*; import javax.net.ssl.*; import java.util.regex.*; public class HelloClientSSL { public static void main(String[] args) { // Pick all AES algorithms of 256 bits key size String patternString = "AES.*256"; Pattern pattern = Pattern.compile(patternString); Matcher matcher; boolean matchFound; try { SSLSocketFactory sslFact = (SSLSocketFactory)SSLSocketFactory.getDefault(); SSLSocket s = (SSLSocket)sslFact.createSocket(args.length == 0 ? "127.0.0.1" : args[0], skeySpec); byte[] original = cipher.doFinal(encrypted); String originalString = new String(original); System.out.println("Original string: " + originalString + " " + asHex(original)); } } client端 Java代码 import java.io.*; import java.security.*; import javax.net.ssl.*; import java.util.regex.*; public class HelloClientSSL { public static void main(String[] args) { // Pick all AES algorithms of 256 bits key size String patternString = "AES.*256"; Pattern pattern = Pattern.compile(patternString); Matcher matcher; boolean matchFound; try { SSLSocketFactory sslFact = (SSLSocketFactory)SSLSocketFactory.getDefault(); SSLSocket s = (SSLSocket)sslFact.createSocket(args.length == 0 ? "127.0.0.1" : args[0],"UTF8")); System.out.println("\nStart decryption:"); cipher.init(Cipher.DECRYPT_MODE, 2 AES和Java 从j2se1.4.2开始,它支持128, skeySpec); byte[] encrypted = cipher.doFinal((args.length == 0 ? "This is just an example" : args[0]).getBytes()); System.out.println("encrypted string: " + asHex(encrypted)); cipher.init(Cipher.DECRYPT_MODE,"UTF8")); } } 本身稍加修改的例子 Java代码 byte[] plainText="12345678".getBytes(); //Iacute;‥sup1;amp;KeyGeneratorETH;Icirc;sup3;Eacute;Ograve;amp;amp;amp;key System.out.println("\nStart generate AES key"); KeyGenerator keyGen=KeyGenerator.getInstance("AES"); String pwd = "passord"; keyGen.init(128。
skeySpec); byte[] encrypted = cipher.doFinal((args.length == 0 ? "This is just an example" : args[0]).getBytes()); System.out.println("encrypted string: " + asHex(encrypted)); cipher.init(Cipher.DECRYPT_MODE, k = len-1; for (int i=0; i len; i++) { System.out.println(str[i]); // Determine if pattern exists in input matcher = pattern.matcher(str[i]); matchFound = matcher.find(); if (matchFound) set[j++] = str[i]; else set[k--] = str[i]; } s.setEnabledCipherSuites(set); str=s.getEnabledCipherSuites(); System.out.println("Available Suites after Set:"); for (int i=0; i str.length; i++) System.out.println(str[i]); OutputStream out = s.getOutputStream(); BufferedReader in = new BufferedReader ( new InputStreamReader(s.getInputStream())); String mesg = in.readLine(); System.out.println("Socket message: " + mesg); in.close(); } catch (Exception e) { System.out.println("Exception" + e); } } } 运行功效