AES是一種對稱的私鑰加密技術
從j
現在的java支持
例
Java代碼
import java
import javax
import javax
import java
/**
* This program generates a AES key
* then reinstantiates a AES key from the key bytes
* The reinstantiated key is used to initialize a AES cipher for
* encryption and decryption
*/
public class AES {
/**
* Turns array of bytes into string
*
* @param buf Array of bytes to convert to hex string
* @return Generated hex string
*/
public static String asHex (byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf
int i;
for (i =
if (((int) buf[i] &
strbuf
strbuf
}
return strbuf
}
public static void main(String[] args) throws Exception {
String message=
// Get the KeyGenerator
KeyGenerator kgen = KeyGenerator
kgen
// Generate the secret key specs
SecretKey skey = kgen
byte[] raw = skey
SecretKeySpec skeySpec = new SecretKeySpec(raw
// Instantiate the cipher
Cipher cipher = Cipher
cipher
byte[] encrypted =
cipher
System
cipher
byte[] original =
cipher
String originalString = new String(original);
System
originalString +
}
}
import java
import javax
import javax
import java
/**
* This program generates a AES key
* then reinstantiates a AES key from the key bytes
* The reinstantiated key is used to initialize a AES cipher for
* encryption and decryption
*/
public class AES {
/**
* Turns array of bytes into string
*
* @param bufArray of bytes to convert to hex string
* @returnGenerated hex string
*/
public static String asHex (byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf
int i;
for (i =
if (((int) buf[i] &
strbuf
strbuf
}
return strbuf
}
public static void main(String[] args) throws Exception {
String message=
// Get the KeyGenerator
KeyGenerator kgen = KeyGenerator
kgen
// Generate the secret key specs
SecretKey skey = kgen
byte[] raw = skey
SecretKeySpec skeySpec = new SecretKeySpec(raw
// Instantiate the cipher
Cipher cipher = Cipher
cipher
byte[] encrypted =
cipher
System
cipher
byte[] original =
cipher
String originalString = new String(original);
System
originalString +
}
}
kgen
按照原文提示的地址可以更新下一個包
server端
Java代碼
import java
import java
import
import java
public class HelloServerSSL {
public static void main(String[] args) {
SSLServerSocket s;
// Pick all AES algorithms of
String patternString =
Pattern pattern = pile(patternString);
Matcher matcher;
boolean matchFound;
try {
SSLServerSocketFactory sslSrvFact =
(SSLServerSocketFactory)
SSLServerSocketFactory
s =(SSLServerSocket)sslSrvFact
SSLSocket in = (SSLSocket)s
String str[]=in
int len = str
String set[] = new String[len];
int j=
for (int i=
// Determine if pattern exists in input
matcher = pattern
matchFound = matcher
if (matchFound)
set[j++] = str[i];
else
set[k
}
in
str=in
System
for (int i=
System
System
(in
PrintWriter out = new PrintWriter (in
true);
out
in
} catch (Exception e) {
System
}
}
}
import java
import java
import
import java
public class HelloServerSSL {
public static void main(String[] args) {
SSLServerSocket s;
// Pick all AES algorithms of
String patternString =
Pattern pattern = pile(patternString);
Matcher matcher;
boolean matchFound;
try {
SSLServerSocketFactory sslSrvFact =
(SSLServerSocketFactory)
SSLServerSocketFactory
s =(SSLServerSocket)sslSrvFact
SSLSocket in = (SSLSocket)s
String str[]=in
int len = str
String set[] = new String[len];
int j=
for (int i=
// Determine if pattern exists in input
matcher = pattern
matchFound = matcher
if (matchFound)
set[j++] = str[i];
else
set[k
}
in
str=in
System
for (int i=
System
System
(in
PrintWriter out = new PrintWriter (in
true);
out
in
} catch (Exception e) {
System
}
}
}
client端
Java代碼
import java
import java
import
import java
public class HelloClientSSL {
public static void main(String[] args) {
// Pick all AES algorithms of
String patternString =
Pattern pattern = pile(patternString);
Matcher matcher;
boolean matchFound;
try {
SSLSocketFactory sslFact =
(SSLSocketFactory)SSLSocketFactory
SSLSocket s =
(SSLSocket)sslFact
String str[]=s
int len = str
String set[] = new String[len];
int j=
for (int i=
System
// Determine if pattern exists in input
matcher = pattern
matchFound = matcher
if (matchFound)
set[j++] = str[i];
else
set[k
}
s
str=s
System
for (int i=
System
OutputStream out = s
BufferedReader in = new BufferedReader (
new InputStreamReader(s
String mesg = in
System
in
} catch (Exception e) {
System
}
}
}
import java
import java
import
import java
public class HelloClientSSL {
public static void main(String[] args) {
// Pick all AES algorithms of
String patternString =
Pattern pattern = pile(patternString);
Matcher matcher;
boolean matchFound;
try {
SSLSocketFactory sslFact =
(SSLSocketFactory)SSLSocketFactory
SSLSocket s =
(SSLSocket)sslFact
String str[]=s
int len = str
String set[] = new String[len];
int j=
for (int i=
System
// Determine if pattern exists in input
matcher = pattern
matchFound = matcher
if (matchFound)
set[j++] = str[i];
else
set[k
}
s
str=s
System
for (int i=
System
OutputStream out = s
BufferedReader in = new BufferedReader (
new InputStreamReader(s
String mesg = in
System
in
} catch (Exception e) {
System
}
}
}
運行結果
Available Suites after Set:TLS_RSA_WITH_AES_
別人的例子
Java代碼
/**
*PrivateExmaple
*Copyright
*/
import javax
import javax
import java
/**
*Ë&frac
*/
public class PrivateExample{
public static void main(String[] args) throws Exception{
byte[] plainText=
//Í‥&sup
System
KeyGenerator keyGen=KeyGenerator
keyGen
Key key=keyGen
System
//&&&ÃÒ&&&Ë&frac
Cipher cipher=Cipher
System
//Ê&sup
System
cipher
byte[] cipherText=cipher
System
System
System
cipher
byte[] newPlainText=cipher
System
System
}
}
/**
*PrivateExmaple
*Copyright
*/
import javax
import javax
import java
/**
*Ë&frac
*/
public class PrivateExample{
public static void main(String[] args) throws Exception{
byte[] plainText=
//Í‥&sup
System
KeyGenerator keyGen=KeyGenerator
keyGen
Key key=keyGen
System
//&&&ÃÒ&&&Ë&frac
Cipher cipher=Cipher
System
//Ê&sup
System
cipher
byte[] cipherText=cipher
System
System
System
cipher
byte[] newPlainText=cipher
System
System
}
}
自己稍加修改的例子
Java代碼
byte[] plainText=
//Í‥&sup
System
KeyGenerator keyGen=KeyGenerator
String pwd =
keyGen
//keyGen
//Key key=keyGen
SecretKey skey = keyGen
byte[] raw = skey
SecretKeySpec skeySpec = new SecretKeySpec(raw
System
//&&&ÃÒ&&&Ë&frac
Cipher cipher=Cipher
System
//Ê&sup
System
cipher
byte[] cipherText=cipher
System
System
System
cipher
byte[] newPlainText=cipher
System
System
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25576.html