用流密码实现加密java语言_使用java的流密码

我想创建一个使用Java的流密码,在该密码中,我从文件中将纯文本加密(使用种子值的简单XOR与随机密钥)并将其存储在不同的文件中,然后进行解密,再次从文件中取出密文并对其进行解密(与密钥相同的XOR操作与加密相同)并存储在文件中。使用java的流密码

但是我在试图加密大字符串时遇到问题。一半的字符串正在被正确解密,但另一半仍然是不可读的格式。

FileReader fileReader = new FileReader(file);

// Always wrap FileReader in BufferedReader.

BufferedReader bufferedReader =

new BufferedReader(fileReader);

FileWriter fileWriter =

new FileWriter(file2);

// Always wrap FileWriter in BufferedWriter.

BufferedWriter bufferedWriter =

new BufferedWriter(fileWriter);

while((line = bufferedReader.readLine()) != null) {

sb = new StringBuffer (line);

int lenStr = line.length();

int lenKey = String.valueOf(random).length();

// For each character in our string, encrypt it...

for (int i = 0, j = 0; i < lenStr; i++, j++)

{

if (j >= lenKey) j = 0; // Wrap 'round to beginning of key string.

//

// XOR the chars together. Must cast back to char to avoid compile error.

//

String key = random + "";

bufferedWriter.write((char)(line.charAt(i)^key.charAt(j)));

}

}

// Always close files.

bufferedReader.close();

bufferedWriter.close();

+0

哪一半?你的琴弦有多久了? –

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CTR(Counter)模式是一种对称加密模式,它将块密码(如AES)转化为密码。CTR 模式的基本思想是,将计数器和密钥作为输入,通过加密算法产生伪随机,然后将明文和伪随机异或得到密文。 CTR 模式的加密过程如下: 1. 将计数器初始化为一个随机数或者 0,计数器的初始值和密钥共同组成一个输入。 2. 加密算法对输入进行加密,得到伪随机。 3. 将明文和伪随机进行异或运算,得到密文。 4. 将计数器加 1,重复步骤 2 和 3,直到所有明文块都被加密。 CTR 模式的解密过程与加密过程类似,只需要将密文和伪随机进行异或运算即可还原明文。 下面是使用 Java 实现 CTR 模式的加密和解密: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class CtrMode { public static void main(String[] args) throws Exception { String plaintext = "Hello, world!"; String key = "0123456789abcdef"; String iv = "fedcba9876543210"; byte[] ciphertext = ctrEncrypt(plaintext, key, iv); System.out.println("Ciphertext: " + bytesToHex(ciphertext)); String decrypted = ctrDecrypt(ciphertext, key, iv); System.out.println("Decrypted: " + decrypted); } public static byte[] ctrEncrypt(String plaintext, String key, String iv) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(hexToBytes(key), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(hexToBytes(iv)); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); return cipher.doFinal(plaintext.getBytes()); } public static String ctrDecrypt(byte[] ciphertext, String key, String iv) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(hexToBytes(key), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(hexToBytes(iv)); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] plaintext = cipher.doFinal(ciphertext); return new String(plaintext); } public static byte[] hexToBytes(String hex) { int len = hex.length(); byte[] bytes = new byte[len / 2]; for (int i = 0; i < len; i += 2) { bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i+1), 16)); } return bytes; } public static String bytesToHex(byte[] bytes) { char[] hexArray = "0123456789abcdef".toCharArray(); char[] hexChars = new char[bytes.length * 2]; for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xff; hexChars[i * 2] = hexArray[v >>> 4]; hexChars[i * 2 + 1] = hexArray[v & 0x0f]; } return new String(hexChars); } } ``` 这段代码使用 AES 算法和 CTR 模式对字符串进行加密和解密。注意,计数器的初始值和密钥都需要是 16 字节的十六进制字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值