(3)使用位移运算符对密码加密和解密。提示:定义一个加密参数(位移位数),原密码根据加密参数进行左移生成一个新密码,实现对密码加密;新密码根据加密参数进行右移生成原密码,实现对密码解密。
public class PasswordEncryption {
// 加密函数
public static String encrypt(String password, int offset) {
char[] charArray = password.toCharArray();
for (int i = 0; i < charArray.length; i++) {
charArray[i] = (char) (charArray[i] << offset);
}
return new String(charArray);
}
// 解密函数
public static String decrypt(String encryptPassword, int offset) {
char[] charArray = encryptPassword.toCharArray();
for (int i = 0; i < charArray.length; i++) {
charArray[i] = (char)(charArray[i] >> offset);
}
return new String(charArray);
}
public static void main(String[] args) {
String password = "hello world";
int offset = 3;
String encryptPw = encrypt(password, offset);
String originalPw = decrypt(encryptPw, offset);
System.out.println("原始密码为:" + password);
System.out.println("加密后的密码为:" + encryptPw);
System.out.println("解密后的密码为:" + originalPw);
}
}
为了解决以上的问题,有人发明了非对称秘钥加密系统,即RSA加密,它的原理是,通过一系列数学计算,生成一对秘钥:一个私钥,一个公钥。公钥用于加密,私钥用于解密。由于数学原理的原因,私钥可以推出公钥,而公钥无法推出私钥。
在使用的时候,私钥自己保存好,而公钥可以在不加密的情况下,发给任何你想要通讯的人。当对方需要向你发送消息时,可以使用拿到的公钥对信息进行加密,然后发送给你。你在收到信息后,可以使用自己的私钥对信息进行解密。
由于其他人没有你的私钥,所以即使有人监听或者窃取了发送给你的信息,也是无法解密。从而保证了信息传输的安全。同时,因为不用再定期见面交换秘钥,从而使这种加密方式的便利性大大提高。
非对称加密,解决了信息传输安全和秘钥传输安全的双重问题,但是它也有缺陷,即:非对称加密的信息,在解密时的耗时相对较长,不适合频繁交互的通讯。
为了弥补这一缺陷,人们进一步改进了信息加密传输方式:使用非对称加密技术,传输对称加密需要使用的秘钥,在之后的通讯中,还是使用对称加密技术。这样,既保证了秘钥的安全,又保证了解密的效率。这也就是我们现在常用的信息加密传输方式。这方面的典型应用有很多,比如,SSH远程登录服务中使用的秘钥连接方式。
我可以使用位移运算符给密码进行加密和解密。具体步骤如下:
shift = 3 # 以左移3位为例
bin()
函数来转换为二进制,然后使用字符串函数的 zfill()
方法添加 0。password = "myPassword" # 原密码
binary_password = bin(int.from_bytes(password.encode(), 'big'))[2:] # 转换为二进制
binary_password = binary_password.zfill(len(binary_password) + shift - len(binary_password) % shift) # 添加 0,使得二进制长度是 shift 的整数倍
byte_password = [binary_password[i:i+shift] for i in range(0, len(binary_password), shift)]
encrypted_binary_password = ''.join([byte_password[i][shift:] + byte_password[i][:shift] for i in range(len(byte_password))])
int()
函数将二进制字符串转换为整数,然后使用 to_bytes()
方法转换为字节数组,最后使用 decode()
方法将字节数组转换为字符串。encrypted_password = int(encrypted_binary_password, 2).to_bytes((len(encrypted_binary_password) + 7) // 8, byteorder='big').decode()
binary_encrypted_password = bin(int.from_bytes(encrypted_password.encode(), 'big'))[2:].zfill(len(encrypted_password) * 8) # 转换为二进制,并添加 0,使得长度是 shift 的整数倍
byte_encrypted_password = [binary_encrypted_password[i:i+shift] for i in range(0, len(binary_encrypted_password), shift)] # 分为若干个字节,每个字节长度为 shift
decrypted_binary_password = ''.join([byte_encrypted_password[i][-shift:] + byte_encrypted_password[i][:-shift] for i in range(len(byte_encrypted_password))]) # 对每个字节进行右移操作,并合并为一个二进制字符串
decrypted_password = int(decrypted_binary_password, 2).to_bytes((len(decrypted_binary_password) + 7) // 8, byteorder='big').decode() # 将二进制字符串转换为原始密码
完整的代码如下:
def encrypt_password(password, shift):
binary_password = bin(int.from_bytes(password.encode(), 'big'))[2:] # 转换为二进制
binary_password = binary_password.zfill(len(binary_password) + shift - len(binary_password) % shift) # 添加 0,使得二进制长度是 shift 的整数倍
byte_password = [binary_password[i:i+shift] for i in range(0, len(binary_password), shift)] # 分为若干个字节,每个字节长度为 shift
encrypted_binary_password = ''.join([byte_password[i][shift:] + byte_password[i][:shift] for i in range(len(byte_password))]) # 对每个字节进行左移操作,并合并为一个二进制字符串
encrypted_password = int(encrypted_binary_password, 2).to_bytes((len(encrypted_binary_password) + 7) // 8, byteorder='big').decode() # 将二进制字符串转换为原始密码
return encrypted_password
def decrypt_password(password, shift):
binary_password = bin(int.from_bytes(password.encode(), 'big'))[2:].zfill(len(password) * 8) # 转换为二进制,并添加 0,使得长度是 shift 的整数倍
byte_password = [binary_password[i:i+shift] for i in range(0, len(binary_password), shift)] # 分为若干个字节,每个字节长度为 shift
decrypted_binary_password = ''.join([byte_password[i][-shift:] + byte_password[i][:-shift] for i in range(len(byte_password))]) # 对每个字节进行右移操作,并合并为一个二进制字符串
decrypted_password = int(decrypted_binary_password, 2).to_bytes((len(decrypted_binary_password) + 7) // 8, byteorder='big').decode() # 将二进制字符串转换为原始密码
return decrypted_password