Skip to content

Commit 42571e5

Browse files
author
root
committed
first
1 parent 3fa994b commit 42571e5

File tree

13 files changed

+2614
-0
lines changed

13 files changed

+2614
-0
lines changed

UDP_C.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
import jp.jbxl.*;
4+
5+
6+
7+
class UDP_C
8+
{
9+
10+
public static void main(String args[])
11+
{
12+
UDP udp = new UDP("202.26.159.139", 8000);
13+
String recv = "";
14+
15+
try {
16+
udp.sendMesgln("OK");
17+
recv = udp.recvMesg();
18+
}
19+
catch (Exception er) {
20+
//er.printStackTrace();
21+
recv = "接続失敗\n";
22+
}
23+
24+
System.out.print(recv);
25+
udp.close();
26+
}
27+
28+
}

UDP_S.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
import jp.jbxl.*;
4+
5+
6+
7+
class UDP_S
8+
{
9+
10+
public static void main(String args[])
11+
{
12+
UDP udp = new UDP(8000);
13+
String recv = "";
14+
15+
while(true){
16+
try {
17+
recv = udp.recvMesg();
18+
udp.sendMesgln("OK");
19+
}
20+
catch (Exception er) {
21+
//er.printStackTrace();
22+
recv = "接続失敗\n";
23+
System.out.print(recv);
24+
break;
25+
}
26+
27+
System.out.print(recv);
28+
}
29+
udp.close();
30+
}
31+
32+
}

clean_jbxl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
PKGNM='jp/jbxl'
4+
5+
rm -f ${PKGNM}/*.class
6+
rm -f ${PKGNM}/opt/*.class
7+
rm -f *.jar
8+
rm -f *.class
9+
rm -fr html/*
10+

jp/jbxl/Base64.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package jp.jbxl;
2+
3+
4+
5+
/**
6+
* Base64 の符号化,復号化を行なう.
7+
*
8+
* @author Fumi.Iseki
9+
* @version 1.0
10+
*/
11+
12+
public class Base64
13+
{
14+
private static String ascii = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
15+
// 0123456789012345678901234567890123456789012345678901234567890123
16+
17+
18+
/**
19+
* コンストラクタは使用しない
20+
* @deprecated
21+
*/
22+
private Base64()
23+
{
24+
}
25+
26+
27+
28+
/**
29+
* バイトデータ dec を Base64の Stringに変換する.
30+
* @param dec Base64 に変換するバイト列
31+
* @return Base64 に変換された文字列
32+
*/
33+
public static String encode(byte[] dec)
34+
{
35+
int i, j, k;
36+
int sz = (dec.length+2)/3*4;
37+
int wk, mk;
38+
String enc = "";
39+
40+
mk = 0x80;
41+
for (i=0; i<sz; i++) {
42+
wk = 0;
43+
if (dec.length*8>i*6) {
44+
for (j=0; j<6; j++) {
45+
k = (i*6 + j)/8;
46+
if (k<dec.length) {
47+
if ((dec[k]&mk)!=0) wk += 1;
48+
}
49+
wk <<= 1;
50+
mk >>>= 1;
51+
if (mk==0x00) mk = 0x80;
52+
}
53+
wk >>>= 1;
54+
enc += ascii.charAt(wk);
55+
}
56+
else {
57+
enc += "=";
58+
}
59+
}
60+
return enc;
61+
}
62+
63+
64+
65+
/**
66+
* Base64でエンコードされた String buf をバイト列に戻す.
67+
* @param buf 復号化する Base64 文字列
68+
* @return 復号化されたバイト列
69+
*/
70+
public static byte[] decode(String buf)
71+
{
72+
int i, j, k=0;
73+
int ix, wk, mk, lt=0;
74+
75+
while (lt<buf.length() && buf.charAt(lt)!='=') {
76+
lt++;
77+
if (lt==buf.length()) break;
78+
}
79+
80+
int sz = lt/4*3 + (lt%4)*3/4;
81+
byte[] dec = new byte[sz];
82+
83+
for (i=0; i<sz; i++) dec[i] = 0x00;
84+
85+
wk = 0;
86+
for (i=0; i<lt; i++) {
87+
ix = ascii.indexOf(buf.substring(i, i+1));
88+
mk = 0x20;
89+
90+
for (j=0; j<6; j++) {
91+
k = i*6 + j;
92+
if ((ix&mk)!=0) wk += 1;
93+
wk <<= 1;
94+
if (((k+1)%8)==0) {
95+
wk >>>= 1;
96+
dec[k/8] = (byte)wk;
97+
wk = 0;
98+
}
99+
mk >>>= 1;
100+
}
101+
}
102+
103+
return dec;
104+
}
105+
106+
}
107+

jp/jbxl/DHCrypt.java

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package jp.jbxl;
2+
3+
import java.security.*;
4+
import java.security.spec.*;
5+
import javax.crypto.*;
6+
import javax.crypto.spec.*;
7+
import javax.crypto.interfaces.*;
8+
9+
10+
/**
11+
* Diffie-Hellman鍵交換法 クライアント
12+
* @author Fumi.Iseki
13+
* @version 1.0
14+
*/
15+
public class DHCrypt
16+
{
17+
18+
private KeyPairGenerator myKeyPairGen;
19+
private KeyFactory myKeyFac;
20+
private KeyPair myKeyPair;
21+
private PublicKey serverPubKey;
22+
private KeyAgreement myKeyAgree;
23+
private byte[] mySharedSecret = null;
24+
25+
/**
26+
* このオブジェクトの Subject Public Key Info (DER形式) <br>
27+
* dhClient() 実行後でないと,有効な値を得られない.
28+
*/
29+
public byte[] myPubKey = null;
30+
31+
/**
32+
* Base64でエンコードされた myPubKey <br>
33+
* dhClient() 実行後でないと,有効な値を得られない.
34+
*/
35+
public String myPubKeyEnc = null;
36+
37+
38+
/**
39+
* コンストラクタ.状態を初期化する.
40+
*/
41+
public DHCrypt()
42+
{
43+
try {
44+
myKeyFac = KeyFactory.getInstance("DiffieHellman");
45+
myKeyPairGen = KeyPairGenerator.getInstance("DiffieHellman");
46+
myKeyAgree = KeyAgreement.getInstance("DiffieHellman");
47+
}
48+
catch (Exception e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
53+
54+
55+
/**
56+
* サーバの SPKI(DER形式)から自分の SPKI(DER形式)を生成して返す.また,共通鍵 mySharedSecret も計算する.
57+
*
58+
* @param serverKeyEnc サーバの Subject Public Key Info (DER)
59+
* @return このオブジェクトの Subject Public Key Info (DER)
60+
*/
61+
public byte[] dhClient(byte[] serverKeyEnc)
62+
{
63+
myPubKey = null;
64+
myPubKeyEnc = null;
65+
mySharedSecret = null;
66+
67+
try {
68+
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(serverKeyEnc);
69+
serverPubKey = myKeyFac.generatePublic(x509KeySpec);
70+
DHParameterSpec dhParamSpec = ((DHPublicKey)serverPubKey).getParams();
71+
72+
myKeyPairGen.initialize(dhParamSpec);
73+
myKeyPair = myKeyPairGen.generateKeyPair();
74+
myKeyAgree.init(myKeyPair.getPrivate());
75+
myKeyAgree.doPhase(serverPubKey, true);
76+
77+
byte[] tmpSharedSecret = new byte[256]; // for 2048 bit key
78+
int myLen = myKeyAgree.generateSecret(tmpSharedSecret, 0);
79+
mySharedSecret = new byte[myLen];
80+
for (int i=0; i<myLen; i++) mySharedSecret[i] = tmpSharedSecret[i];
81+
tmpSharedSecret = null;
82+
83+
myPubKey = myKeyPair.getPublic().getEncoded();
84+
myPubKeyEnc = Base64.encode(myPubKey);
85+
//System.err.println("SCRT = "+Tools.byteArray_toHex(mySharedSecret));
86+
}
87+
catch (Exception e) {
88+
e.printStackTrace();
89+
return null;
90+
}
91+
92+
return myPubKey;
93+
}
94+
95+
96+
97+
/**
98+
* DES(ECB) での暗号化.テスト用.<br>
99+
* 要動作確認
100+
*
101+
* @param data 暗号化するバイト列.
102+
* @return 暗号化されたバイト列.
103+
*/
104+
public byte[] dhCrypt(byte[] data)
105+
{
106+
byte[] cipher = null;
107+
108+
try {
109+
myKeyAgree.doPhase(serverPubKey, true);
110+
SecretKey myDesKey = myKeyAgree.generateSecret("DES");
111+
112+
Cipher myCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
113+
myCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
114+
cipher = myCipher.doFinal(data);
115+
}
116+
catch (Exception e) {
117+
e.printStackTrace();
118+
return null;
119+
}
120+
121+
return cipher;
122+
}
123+
124+
}
125+
126+

0 commit comments

Comments
 (0)