我一直在试图找出java安全/加密库的方法,我相信我在理解发生了什么方面取得了一些进展。我想我设法让加密部分工作了。在encrypt方法中,如果我只是尝试返回返回新字符串(encryptedText,“UTF-8”);
,我会得到一些看起来像加密文本的不可读的杂乱无章的东西。当我试图不返回它,而是继续调用decrypt并进行明文-->encrypt-->decrypt是我所拥有的一切时,问题就来了
import javax.crypto.*;
import java.security.*;
import java.io.UnsupportedEncodingException;
public class AES256_GCM {
public String encrypt(String cleartext) {
try
{
// encoding format needs thought
byte[] clearTextbytes = cleartext.getBytes("UTF-8");
final SecureRandom secureKeyRandomness = SecureRandom.getInstanceStrong();
final KeyGenerator AES_keyInstance = KeyGenerator.getInstance("AES");
AES_keyInstance.init(128, secureKeyRandomness);
final SecretKey secretKey = AES_keyInstance.generateKey();
final Cipher AES_cipherInstance = Cipher.getInstance("AES/GCM/NoPadding");
AES_cipherInstance.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedText = AES_cipherInstance.doFinal(clearTextbytes);
return decrypt(encryptedText, secretKey);
}
catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e)
{
e.printStackTrace();
}
return "something went wrong with encrypt";
} // encrypt.
public String decrypt(byte[] encryptedText, SecretKey secretKey) {
try
{
final Cipher AES_cipherInstance = Cipher.getInstance("AES/GCM/NoPadding");
AES_cipherInstance.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedText = AES_cipherInstance.doFinal(encryptedText);
return new String(decryptedText, "UTF-8");
}
catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e)
{
e.printStackTrace();
}
return "something went wrong with decrypt";
} // decrypt.
} // AES256_GCM.
所以我用一些明文调用encrypt,并尝试返回明文以确保它工作。下面是我正在处理的问题,任何与javafx相关的内容都可能是无关紧要的。返回加密的字符串没有问题,只有解密的字符串抛出这个。
“Decrypt的something bour”返回,后面跟着:
java.security.InvalidKeyException: Parameters missing
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:470)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313)
at javax.crypto.Cipher.implInit(Cipher.java:801)
at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
at javax.crypto.Cipher.init(Cipher.java:1248)
at javax.crypto.Cipher.init(Cipher.java:1185)
at PackageName.AES256_GCM.decrypt(AES256_GCM.java:49)
at PackageName.AES256_GCM.encrypt(AES256_GCM.java:36)
at com.javafxpractice.PackageName.letsEncrypt.letsEnc(letsEncrypt.java:10)
at com.javafxpractice.PackageName.Controller.onClicked(Controller.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
java.security.InvalidKeyException: Parameters missing
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:470)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313)
at javax.crypto.Cipher.implInit(Cipher.java:801)
at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
at javax.crypto.Cipher.init(Cipher.java:1248)
at javax.crypto.Cipher.init(Cipher.java:1185)
at PackageName.AES256_GCM.decrypt(AES256_GCM.java:49)
at PackageName.AES256_GCM.encrypt(AES256_GCM.java:36)
at com.javafxpractice.PackageName.letsEncrypt.letsEnc(letsEncrypt.java:10)
at com.javafxpractice.PackageName.Controller.onEnter(Controller.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$352(GlassViewEventHandler.java:248)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
at com.sun.glass.ui.View.notifyKey(View.java:966)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
在IVParameterSpec的基础上添加了GCMParameterSpec。作为代码编辑的前奏,下面有一个很好的讨论是Java aes/gcm/nopadding-cipher.getiv()给了我什么?
import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import java.security.*;
import java.io.UnsupportedEncodingException;
public class EncryptDecrypt1 {
public static void main(String[] args) throws Exception {
EncryptDecrypt1 a = new EncryptDecrypt1();
String encrypted = a.encrypt("My text");
System.out.println(encrypted);
}
public String encrypt(String cleartext) throws Exception {
try
{
// encoding format needs thought
byte[] clearTextbytes = cleartext.getBytes("UTF-8");
final SecureRandom secureKeyRandomness = SecureRandom.getInstanceStrong();
final KeyGenerator AES_keyInstance = KeyGenerator.getInstance("AES");
AES_keyInstance.init(128, secureKeyRandomness);
final SecretKey secretKey = AES_keyInstance.generateKey();
final Cipher AES_cipherInstance = Cipher.getInstance("AES/GCM/NoPadding");
AES_cipherInstance.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedText = AES_cipherInstance.doFinal(clearTextbytes);
byte[] iv = AES_cipherInstance.getIV();
byte[] message = new byte[12 + clearTextbytes.length + 16];
System.arraycopy(iv, 0, message, 0, 12);
System.arraycopy(encryptedText, 0, message, 12, encryptedText.length);
return decrypt(message, secretKey);
}
catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e)
{
e.printStackTrace();
}
return "something went wrong with encrypt";
} // encrypt.
public String decrypt(byte[] encryptedText, SecretKey secretKey) throws Exception {
try
{
final Cipher AES_cipherInstance = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec params = new GCMParameterSpec(128, encryptedText, 0, 12);
AES_cipherInstance.init(Cipher.DECRYPT_MODE, secretKey, params);
byte[] decryptedText = AES_cipherInstance.doFinal(encryptedText, 12, encryptedText.length - 12);
return new String(decryptedText, "UTF-8");
}
catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e)
{
e.printStackTrace();
}
return "something went wrong with decrypt";
}
}
我正在尝试用AES解密来解密一个Base64Encoded字符串消息。 从输入中提取IV和加密文本 使用IV和相同的密码短语生成用于加密文本的密钥。密钥生成应遵循以下相同的步骤。 生成PBE密钥(256位) 用法: 任何线索将非常感谢,我想知道我是否错过了一些配置或数据转换时,试图转换在SWIFT。
这是一个错误: 1.JS
我正在编写一个Java程序来解密TLS1.2会话,它使用密码。我使用Wireshark录制了一个测试会话。主秘已知。 这里我只需要客户机密钥,因为我想解密一个客户机->服务器包。我按照RFC扩展了服务器和客户端密钥以及IVs。 即兴: 我从salt创建AES-GCM nonce(=客户端写IV)和显式nonce(=加密数据的前8字节)。 代码: 现在我把所有东西都输入到BouncyCastle中:
节点模块: Java类:主要方法现在只是用于测试,稍后将被删除。
我目前正在将我的C#AES-GCM密码代码转换为PHP。然而,经过一些研究,我的PHP系统加密的文本不能被C#one解密。我想知道这两种代码是否有区别: C#带弹跳壳: 下面是PHP系统: 有没有人能告诉我,PHP代码中是否有遗漏或不同之处,导致它们的工作方式有所不同?或者PHP函数和BouncyCastle函数之间是否存在某种内部差异,从而使它们有所不同?
我需要编写以下解密函数的反向(加密): 上面的函数允许我按照规范正确解密加密缓冲区: 之所以以它的方式编写(auth标记为最后16个字节),是因为AES的默认标准库实现就是这样在Java和Go中加密数据的。我需要能够在Go、Java和node.js之间双向解密/加密。node.js中基于库的加密不会将身份验证标记放在任何地方,开发人员希望如何存储身份验证标记,以便在解密期间传递给。在上面的代码中,