*********************因为两种语言很多机制都不一样,所以大致改如下,要注意的是,你必须给button和text注册事件监听器 //Option Explicit//java默认 //' Encipher the text using the pasword. public class bean6 { public bean6() {} //Private Sub Cipher(ByVal password As String, ByVal from_text As String, to_text As String) private void Cipher(String password,String from_text,String to_text) { //Const MIN_ASC = 32 ' Space. final int MIN_ASC=32; //Const MAX_ASC = 126 ' ~. final int MAX_ASC=126; //Const NUM_ASC = MAX_ASC - MIN_ASC + 1 final int NUM_ASC=MAX_ASC-MIN_ASC+1;//Dim offset As Long long offset; //Dim str_len As Integer int str_len; //Dim i As Integer int i; //Dim ch As Integer int ch; //' Initialize the random number generator. //offset = NumericPassword(password) offset = NumericPassword(password); //Rnd -1 double rnd=Rnd-1; //Randomize offset //' Encipher the string. //str_len = Len(from_text) str_len=from_text.length(); //For i = 1 To str_len for(int i=0;i<str_len;i++) { //ch = Asc(Mid$(from_text, i, 1)) ch=from_text.charAt(i); //If ch >= MIN_ASC And ch <= MAX_ASC Then if(ch>=MIN_ASC&&cn<=MAX_ASC) { ch = ch - MIN_ASC; offset = (int)((NUM_ASC + 1) * rnd); ch = ((ch + offset)%NUM_ASC); ch = ch + MIN_ASC; to_text = to_text + ((char)ch); } //Next i } //End Sub } //' Encipher the text using the pasword. //Private Sub Decipher(ByVal password As String, ByVal from_text As String, to_text As String) private void Decipher(String password,String from_text,String to_text) { //Const MIN_ASC = 32 ' Space. final int MIN_ASC=32; //Const MAX_ASC = 126 ' ~. final int MAX_ASC=126; //Const NUM_ASC = MAX_ASC - MIN_ASC + 1 final int NUM_ASC=MAX_ASC-MIN_ASC+1;//Dim offset As Long long offset; //Dim str_len As Integer int str_len; //Dim i As Integer int i; //Dim ch As Integer int ch; //' Initialize the random number generator. //offset = NumericPassword(password) offset = NumericPassword(password); //Rnd -1 //Randomize offset double rnd=Rnd-1; //' Encipher the string. //str_len = Len(from_text) str_len=from_text.length(); //For i = 1 To str_len for(int i=0;i<str_len;i++) { //ch = Asc(Mid$(from_text, i, 1)) ch=from_text.charAt(i); //If ch >= MIN_ASC And ch <= MAX_ASC Then if(ch>=MIN_ASC&&ch<=MAX_ARC) { ch = ch - MIN_ASC; offset = (int)((NUM_ASC + 1) * rnd); ch = ((ch - offset)%NUM_ASC); if (ch < 0) ch = ch + NUM_ASC; ch = ch + MIN_ASC; to_text = to_text + ((char)ch); //End If } //Next i } } //' Translate a password into an offset value. //Private Function NumericPassword(ByVal password As String) As Long private long NumericPassword(String password) { //Dim value As Long long value; //Dim ch As Long long ch; //Dim shift1 As Long long shift1; //Dim shift2 As Long long shift2; //Dim i As Integer int i; //Dim str_len As Integer int str_len; //str_len = Len(password) str_len=password.length(); //For i = 1 To str_len for(int i=0;i<str_len;i++) { //' Add the next letter. //ch = Asc(Mid$(password, i, 1)) ch=password.charAt(i); //value = value Xor (ch * 2 ^ shift1) //value = value Xor (ch * 2 ^ shift2) //' Change the shift offsets. //shift1 = (shift1 + 7) Mod 19 shift1=(shift1+7)%19; //shift2 = (shift2 + 13) Mod 23 shift2=(shift2+13)%23; //Next i } //NumericPassword = value return value; //End Function } //Private Sub cmdCipher_Click() private void actionPerformed(MouseEvent e) {//必须注册时间监听 //Dim cipher_text As String if(e.getSource()==cmdCipher) { String cipher_text; //Cipher txtPassword.Text, txtPlain.Text, cipher_text txtCipher.setText(cipher_text); } //End Sub//Private Sub cmdDecipher_Click() //Dim plain_text As String if(e.getSource()==cmdDecipher) { String plain_text;//Decipher txtPassword.Text, txtCipher.Text, plain_text txtPlain.setText(plain_text); //End Sub } }//Private Sub txtPassword_Change() private void inputMethodTextChanged(InputMethodEvent e) { if(e.getSource()!=txtPassword_Change) return; //If Len(txtPassword.Text) > 0 Then if(txtPassword.getText().length()>0) { cmdCipher.setEnabled(true); cmdDecipher.setEnabled(True); } else { cmdCipher.setEnabled(False); cmdDecipher.setEnabled(False); } //End Sub } }
//value = value Xor (ch * 2 ^ shift1) //value = value Xor (ch * 2 ^ shift2) 对了,还有这两句,异或运算,改如下: value = value^(ch * Math.pow(2,shift1)); value = value^(ch * Math.pow(2,shift2)); 其中跟vb不同的是,"^"代表异或运算,而Math.pow(m,n)代表m的n次方运算
beyond_xiruo(希偌) 是楷模阿,竟然这么充足的时间,羡慕无比。
再请问一句,在java里怎样表示键盘上的左右shift键
在键盘监听事件里 public void processKeyEvent(KeyEvent e) { if(e.getKeyCode()==17) ……………… }
上面的代码我看了,下面我把它加密的部分列出来,请多指点: import java.util.*; public class ajiami {public ajiami() { } public long NumericPassword(String password) throws Exception { long value=1; long ch; long shift1=1; long shift2=1; int i; int str_len; str_len=password.length(); for(i=0; i< str_len; i++) { ch=(byte)(password.charAt(i)); value = value^(long)(ch * Math.pow(2,shift1)); value = value^(long)(ch * Math.pow(2,shift2)); shift1=(shift1+7)% 19; shift2=(shift1+13)%23;} return value;
} public String Clipher(String password,String from_text,String to_text) { final int min_asc=32; final int max_asc=126; final int num_asc=max_asc-min_asc+1;
int str_len; int i; int ch; long ch1; String t_text=to_text; String f_text=from_text; long offset; offset= NumericPassword(password); Random r=new Random(offset); str_len= f_text.length(); for( i=0; i<=str_len; i++) { ch=(byte)(f_text.charAt(i)); if (ch>=min_asc && ch<=max_asc) { ch =ch- min_asc; offset= (int)((num_asc+1)*(r)); ch1= ((offset+ch)% num_asc); ch1=ch1+min_asc; t_text=t_text+(char)(ch1); } } return t_text; }
Rnd -1
Randomize offset
Randomize offset
Rnd -1 就是返回以-1 为种子数的随机数
//Option Explicit//java默认
//' Encipher the text using the pasword.
public class bean6 {
public bean6() {}
//Private Sub Cipher(ByVal password As String, ByVal from_text As String, to_text As String)
private void Cipher(String password,String from_text,String to_text) {
//Const MIN_ASC = 32 ' Space.
final int MIN_ASC=32;
//Const MAX_ASC = 126 ' ~.
final int MAX_ASC=126;
//Const NUM_ASC = MAX_ASC - MIN_ASC + 1
final int NUM_ASC=MAX_ASC-MIN_ASC+1;//Dim offset As Long
long offset;
//Dim str_len As Integer
int str_len;
//Dim i As Integer
int i;
//Dim ch As Integer
int ch; //' Initialize the random number generator.
//offset = NumericPassword(password)
offset = NumericPassword(password);
//Rnd -1
double rnd=Rnd-1;
//Randomize offset //' Encipher the string.
//str_len = Len(from_text)
str_len=from_text.length();
//For i = 1 To str_len
for(int i=0;i<str_len;i++) {
//ch = Asc(Mid$(from_text, i, 1))
ch=from_text.charAt(i);
//If ch >= MIN_ASC And ch <= MAX_ASC Then
if(ch>=MIN_ASC&&cn<=MAX_ASC) {
ch = ch - MIN_ASC;
offset = (int)((NUM_ASC + 1) * rnd);
ch = ((ch + offset)%NUM_ASC);
ch = ch + MIN_ASC;
to_text = to_text + ((char)ch);
}
//Next i
}
//End Sub
}
//' Encipher the text using the pasword.
//Private Sub Decipher(ByVal password As String, ByVal from_text As String, to_text As String)
private void Decipher(String password,String from_text,String to_text) {
//Const MIN_ASC = 32 ' Space.
final int MIN_ASC=32;
//Const MAX_ASC = 126 ' ~.
final int MAX_ASC=126;
//Const NUM_ASC = MAX_ASC - MIN_ASC + 1
final int NUM_ASC=MAX_ASC-MIN_ASC+1;//Dim offset As Long
long offset;
//Dim str_len As Integer
int str_len;
//Dim i As Integer
int i;
//Dim ch As Integer
int ch; //' Initialize the random number generator.
//offset = NumericPassword(password)
offset = NumericPassword(password);
//Rnd -1
//Randomize offset
double rnd=Rnd-1; //' Encipher the string.
//str_len = Len(from_text)
str_len=from_text.length();
//For i = 1 To str_len
for(int i=0;i<str_len;i++) {
//ch = Asc(Mid$(from_text, i, 1))
ch=from_text.charAt(i);
//If ch >= MIN_ASC And ch <= MAX_ASC Then
if(ch>=MIN_ASC&&ch<=MAX_ARC) {
ch = ch - MIN_ASC;
offset = (int)((NUM_ASC + 1) * rnd);
ch = ((ch - offset)%NUM_ASC);
if (ch < 0) ch = ch + NUM_ASC;
ch = ch + MIN_ASC;
to_text = to_text + ((char)ch);
//End If
}
//Next i
}
}
//' Translate a password into an offset value.
//Private Function NumericPassword(ByVal password As String) As Long
private long NumericPassword(String password) {
//Dim value As Long
long value;
//Dim ch As Long
long ch;
//Dim shift1 As Long
long shift1;
//Dim shift2 As Long
long shift2;
//Dim i As Integer
int i;
//Dim str_len As Integer
int str_len; //str_len = Len(password)
str_len=password.length();
//For i = 1 To str_len
for(int i=0;i<str_len;i++) {
//' Add the next letter.
//ch = Asc(Mid$(password, i, 1))
ch=password.charAt(i);
//value = value Xor (ch * 2 ^ shift1)
//value = value Xor (ch * 2 ^ shift2) //' Change the shift offsets.
//shift1 = (shift1 + 7) Mod 19
shift1=(shift1+7)%19;
//shift2 = (shift2 + 13) Mod 23
shift2=(shift2+13)%23;
//Next i
}
//NumericPassword = value
return value;
//End Function
}
//Private Sub cmdCipher_Click()
private void actionPerformed(MouseEvent e) {//必须注册时间监听
//Dim cipher_text As String
if(e.getSource()==cmdCipher) {
String cipher_text;
//Cipher txtPassword.Text, txtPlain.Text, cipher_text
txtCipher.setText(cipher_text);
}
//End Sub//Private Sub cmdDecipher_Click()
//Dim plain_text As String
if(e.getSource()==cmdDecipher) {
String plain_text;//Decipher txtPassword.Text, txtCipher.Text, plain_text
txtPlain.setText(plain_text);
//End Sub
}
}//Private Sub txtPassword_Change()
private void inputMethodTextChanged(InputMethodEvent e) {
if(e.getSource()!=txtPassword_Change) return;
//If Len(txtPassword.Text) > 0 Then
if(txtPassword.getText().length()>0) {
cmdCipher.setEnabled(true);
cmdDecipher.setEnabled(True);
} else {
cmdCipher.setEnabled(False);
cmdDecipher.setEnabled(False);
}
//End Sub
}
}
//value = value Xor (ch * 2 ^ shift2)
对了,还有这两句,异或运算,改如下:
value = value^(ch * Math.pow(2,shift1));
value = value^(ch * Math.pow(2,shift2));
其中跟vb不同的是,"^"代表异或运算,而Math.pow(m,n)代表m的n次方运算
public void processKeyEvent(KeyEvent e) {
if(e.getKeyCode()==17)
………………
}
import java.util.*;
public class ajiami
{public ajiami()
{ } public long NumericPassword(String password) throws Exception
{ long value=1;
long ch;
long shift1=1;
long shift2=1;
int i;
int str_len; str_len=password.length();
for(i=0; i< str_len; i++)
{ ch=(byte)(password.charAt(i));
value = value^(long)(ch * Math.pow(2,shift1));
value = value^(long)(ch * Math.pow(2,shift2)); shift1=(shift1+7)% 19;
shift2=(shift1+13)%23;}
return value;
} public String Clipher(String password,String from_text,String to_text)
{
final int min_asc=32;
final int max_asc=126;
final int num_asc=max_asc-min_asc+1;
int str_len;
int i;
int ch;
long ch1;
String t_text=to_text;
String f_text=from_text;
long offset;
offset= NumericPassword(password);
Random r=new Random(offset);
str_len= f_text.length();
for( i=0; i<=str_len; i++)
{ ch=(byte)(f_text.charAt(i));
if (ch>=min_asc && ch<=max_asc)
{ ch =ch- min_asc;
offset= (int)((num_asc+1)*(r));
ch1= ((offset+ch)% num_asc);
ch1=ch1+min_asc;
t_text=t_text+(char)(ch1);
}
}
return t_text;
}