当前位置: 技术问答>java相关
请高手帮忙,如何把下面的VB代码转化成java代码?是有关加密的问题
来源: 互联网 发布时间:2015-11-08
本文导语: Option Explicit ' Encipher the text using the pasword. Private Sub Cipher(ByVal password As String, ByVal from_text As String, to_text As String) Const MIN_ASC = 32 ' Space. Const MAX_ASC = 126 ' ~. Const NUM_ASC = MAX_ASC - MIN_ASC + 1 Dim off...
Option Explicit
' Encipher the text using the pasword.
Private Sub Cipher(ByVal password As String, ByVal from_text As String, to_text As String)
Const MIN_ASC = 32 ' Space.
Const MAX_ASC = 126 ' ~.
Const NUM_ASC = MAX_ASC - MIN_ASC + 1
Dim offset As Long
Dim str_len As Integer
Dim i As Integer
Dim ch As Integer
' Initialize the random number generator.
offset = NumericPassword(password)
Rnd -1
Randomize offset
' Encipher the string.
str_len = Len(from_text)
For i = 1 To str_len
ch = Asc(Mid$(from_text, i, 1))
If ch >= MIN_ASC And ch 0 Then
cmdCipher.Enabled = True
cmdDecipher.Enabled = True
Else
cmdCipher.Enabled = False
cmdDecipher.Enabled = False
End If
End Sub
' Encipher the text using the pasword.
Private Sub Cipher(ByVal password As String, ByVal from_text As String, to_text As String)
Const MIN_ASC = 32 ' Space.
Const MAX_ASC = 126 ' ~.
Const NUM_ASC = MAX_ASC - MIN_ASC + 1
Dim offset As Long
Dim str_len As Integer
Dim i As Integer
Dim ch As Integer
' Initialize the random number generator.
offset = NumericPassword(password)
Rnd -1
Randomize offset
' Encipher the string.
str_len = Len(from_text)
For i = 1 To str_len
ch = Asc(Mid$(from_text, i, 1))
If ch >= MIN_ASC And ch 0 Then
cmdCipher.Enabled = True
cmdDecipher.Enabled = True
Else
cmdCipher.Enabled = False
cmdDecipher.Enabled = False
End If
End Sub
|
*********************因为两种语言很多机制都不一样,所以大致改如下,要注意的是,你必须给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= MIN_ASC And ch =MIN_ASC&&cn 0 Then
if(txtPassword.getText().length()>0) {
cmdCipher.setEnabled(true);
cmdDecipher.setEnabled(True);
} else {
cmdCipher.setEnabled(False);
cmdDecipher.setEnabled(False);
}
//End Sub
}
}
//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= MIN_ASC And ch =MIN_ASC&&cn 0 Then
if(txtPassword.getText().length()>0) {
cmdCipher.setEnabled(true);
cmdDecipher.setEnabled(True);
} else {
cmdCipher.setEnabled(False);
cmdDecipher.setEnabled(False);
}
//End Sub
}
}