我有一段代码需要实现一个接口,目的是在原单词字符之间插入随机字符,形成新字符串。例如,将
我知道我的代码并不完全符合这个目的,但请问有谁能够帮忙或指导我从这个起点开始做什么?
mycookisred
转换为meynciovoksidswrbendn
。另外,为了完整起见,mycleverpassword
可以变成mxyschlmezvievrppeaysisvwcoorydc
。我知道我的代码并不完全符合这个目的,但请问有谁能够帮忙或指导我从这个起点开始做什么?
import java.util.Random;
public class password implements Encryptable
{
private String message;
private boolean encrypted;
private int shift;
private Random generator;
public password(String msg)
{
message = msg;
encrypted = false;
generator = new Random();
shift = generator.nextInt(10) + 5;
}
public void encrypt()
{
if (!encrypted)
{
String masked = "";
for ( int index = 0; index < message.length(); index++)
masked = masked + (char)(message.charAt(index) +shift);
message = masked;
encrypted = true;
}
}
public String decrypt()
{
if (!encrypted)
{
String unmasked = "";
for ( int index = 0; index < message.length(); index++)
unmasked = unmasked + (char)(message.charAt(index) - shift);
message = unmasked;
encrypted = false;
}
return message;
}
public boolean isEncrypted()
{
return encrypted;
}
public String toString()
{
return message;
}
}
public class passwordTest
{
public static void main(String[] args)
{
password hide = new password("my clever password");
System.out.println(hide);
hide.encrypt();
System.out.println(hide);
hide.decrypt();
System.out.println(hide);
}
}
public interface Encryptable
{
public void encrypt();
public String decrypt();
}