Java Reference
In-Depth Information
Figure 9-38
displays the
pseudocode that
outlines the logic for
the encrypt() method.
The encryption
method specified in
the requirements
document requires the
password to be cut in
half and the two halves
inverted. Then the
order of the characters
in the resulting string
is reversed (spelled
backward). Each char-
acter is compared with
the character in the
original string and
manipulated bitwise.
Finally, a hash code is computed for the original password and appended to the
encrypted one.
Figure 9-39 displays the code for the encrypt() method of the Password class.
encrypt() method (returns String: encrypted password; parameter String: password)
Begin
Obtain password length
Calculate midpoint of password
Encrypted password = last half of password (from midpoint to end)
+ first half of password (from beginning to midpoint, exclusive)
Reverse order of characters in encrypted password
Loop through all characters in the encrypted password
Set current character to a bitwise AND with corresponding original character
End loop
Compute hash code for original password
Append hash code to encrypted password
Return string value of encrypted password
End
FIGURE 9-38
221
// Encrypts original password returning new encrypted String
222
private String encrypt ( String pswd )
223
{
224
StringBuffer encryptPswd;
225
int pswdSize = 0;
226
int midpoint = 0;
227
int hashCode = 0;
228
229
// swap first and last half of password
230
pswdSize = pswd.length () ;
231
midpoint = pswdSize/2;
232
encryptPswd = new StringBuffer ( pswd.substring ( midpoint )
// get last half of pswd
233
+ pswd.substring ( 0,midpoint )) ;
// and concatenate first
half
234
235
encryptPswd.reverse () ; // reverses order of characters in password
236
237
for ( int i=0; i < pswdSize; ++i )
// encrypt each character
238
encryptPswd.setCharAt ( i, ( char )( encryptPswd.charAt ( i ) & pswd.charAt ( i )) ) ;
239
240
hashCode = pswd.hashCode () ; // hash code for original password
241
encryptPswd.append ( hashCode ) ;
242
243
return encryptPswd.toString () ;
244
}
245 }
FIGURE 9-39
The first step in the encryption algorithm implemented by the encrypt()
method is to exchange the first half of the password with the last half. As shown
in Figure 9-39, this is accomplished in code by obtaining the length of the string,
using the String length() method (line 230 of Figure 9-39) and then using
Search WWH ::




Custom Search