解决Cipher获取实例为空问题

最近维护的项目中3Des加密突然报空指针,记录下修复此次问题历程。

先看报错

跟进代码,如下: 

	static {
	        try {
	            key_ = new SecretKeySpec(keys, "TripleDES");
	            ciperEnc = Cipher.getInstance("TRIPLEDES/ecb/NoPadding");
	            ciperEnc.init(Cipher.ENCRYPT_MODE, key_);
	            ciperDec = Cipher.getInstance("TRIPLEDES/ecb/NoPadding");
	            ciperDec.init(Cipher.DECRYPT_MODE, key_);
	        } catch (Exception e) {
	        	LOGGER.error(e.getMessage(), e);
	            //e.printStackTrace();
	        }
	 }

发现是Cipher在获取3DES实例时失败,为null,百思不得其解,原本正常的代码突然报错了,百度一会后没找到答案,回归问题本身,翻阅下java文档(Java Platform SE 8),在里面找到答案。

1.选择所在的javax.crypto

 2.查看Cipher,发现代码里的transformation不在标准transformation中,查看Cipher section

3. 发现java的3DES是使用 DESede

4.修改代码

	static {
		try {
			key_ = new SecretKeySpec(keys, "DESede");
			ciperEnc = Cipher.getInstance("DESede/ecb/NoPadding");
			ciperEnc.init(Cipher.ENCRYPT_MODE, key_);
			ciperDec = Cipher.getInstance("DESede/ecb/NoPadding");
			ciperDec.init(Cipher.DECRYPT_MODE, key_);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			//e.printStackTrace();
		}
	}

5.重新编译启动,问题解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,让我们尝试将 cipher 改为多线程实现。 首先,我们需要使用 Python 中的 threading 模块来创建线程。我们可以创建一个加密线程和一个解密线程,每个线程都可以并行运行,以提高程序的效率。 下面是一个示例代码: ```python import threading def encrypt(text, key): # 加密函数代码 def decrypt(ciphertext, key): # 解密函数代码 def main(): text = "Hello, World!" key = 3 # 创建加密线程 encrypt_thread = threading.Thread(target=encrypt, args=(text, key)) encrypt_thread.start() # 创建解密线程 decrypt_thread = threading.Thread(target=decrypt, args=(ciphertext, key)) decrypt_thread.start() if __name__ == '__main__': main() ``` 在这个示例中,我们创建了 encrypt 和 decrypt 两个函数来执行加密和解密。然后,我们使用 threading.Thread 函数创建加密和解密线程,并使用 start() 方法启动它们。 注意,我们需要将加密线程和解密线程的参数传递给函数,以便在不同的线程中使用它们。 接下来,我们需要在加密和解密函数中添加多线程代码。我们可以使用 Python 的 Lock 和 Condition 对象来避免多个线程同时访问共享资源。 下面是一个多线程加密和解密函数的示例代码: ```python import threading lock = threading.Lock() cond = threading.Condition(lock) ciphertext = "" def encrypt(text, key): global ciphertext with lock: # 等待解密线程完成 cond.wait() # 执行加密 ciphertext = "" # 通知解密线程 cond.notify() def decrypt(ciphertext, key): global plaintext with lock: # 执行解密 plaintext = "" # 通知加密线程 cond.notify() # 等待加密线程完成 cond.wait() ``` 在这个示例中,我们使用 Lock 对象来保护共享资源 ciphertext 和 plaintext。我们使用 Condition 对象来协调加密和解密线程的执行顺序。具体来说,我们在加密函数中等待解密线程完成,然后执行加密操作并通知解密线程。在解密函数中,我们执行解密操作并通知加密线程,然后等待加密线程完成。 最后,我们需要更新 main 函数来调用加密和解密函数,并等待线程完成。 下面是一个完整的多线程 cipher 实现的示例代码: ```python import threading lock = threading.Lock() cond = threading.Condition(lock) ciphertext = "" plaintext = "" def encrypt(text, key): global ciphertext with lock: # 等待解密线程完成 cond.wait() # 执行加密 for char in text: ciphertext += chr((ord(char) + key - 32) % 95 + 32) # 通知解密线程 cond.notify() def decrypt(ciphertext, key): global plaintext with lock: # 执行解密 for char in ciphertext: plaintext += chr((ord(char) - key - 32) % 95 + 32) # 通知加密线程 cond.notify() # 等待加密线程完成 cond.wait() def main(): text = "Hello, World!" key = 3 # 创建加密线程 encrypt_thread = threading.Thread(target=encrypt, args=(text, key)) encrypt_thread.start() # 创建解密线程 decrypt_thread = threading.Thread(target=decrypt, args=(ciphertext, key)) decrypt_thread.start() # 等待线程完成 encrypt_thread.join() decrypt_thread.join() print("Plaintext:", plaintext) if __name__ == '__main__': main() ``` 在这个示例中,我们使用 for 循环来逐个加密和解密每个字符,并将结果存储在 ciphertext 和 plaintext 变量中。我们还使用 join() 方法等待线程完成,并在最后打印解密后的明文。 需要注意的是,多线程并不总是能提高程序的效率,特别是当程序的计算开销很小或者存在大量的 I/O 操作时。因此,在实际应用中,我们需要仔细评估是否需要使用多线程来提高程序的效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snamc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值