繁体   English   中英

用Java加密十六进制字符串

[英]Encrypt a hex string in java

我想问关于我的问题的任何建议。 我需要加密一个十六进制字符串。 我一定不要使用Java的内置函数,因为它在我的服务器中不起作用。 简而言之,我必须对算法进行硬编码或对消息进行加密的任何方法。 有人可以帮助我吗? 非常感谢!

这是代码。

public Encrypt(SecretKey key, String algorithm) {

 try {
     ecipher = Cipher.getInstance(algorithm);
     dcipher = Cipher.getInstance(algorithm);
     ecipher.init(Cipher.ENCRYPT_MODE, key);
     dcipher.init(Cipher.DECRYPT_MODE, key);
 } catch (NoSuchPaddingException e) {
     System.out.println("EXCEPTION: NoSuchPaddingException");
 } catch (NoSuchAlgorithmException e) {
     System.out.println("EXCEPTION: NoSuchAlgorithmException");
 } catch (InvalidKeyException e) {
     System.out.println("EXCEPTION: InvalidKeyException");
 }
}

public void useSecretKey(String secretString) {


 try {
     SecretKey desKey       = KeyGenerator.getInstance("DES").generateKey();
     SecretKey blowfishKey  = KeyGenerator.getInstance("Blowfish").generateKey();
     SecretKey desedeKey    = KeyGenerator.getInstance("DESede").generateKey();

     Encrypt desEncrypter = new Encrypt(desKey, desKey.getAlgorithm());
     Encrypt blowfishEncrypter = new Encrypt(blowfishKey, blowfishKey.getAlgorithm());
     Encrypt desedeEncrypter = new Encrypt(desedeKey, desedeKey.getAlgorithm());

     desEncrypted       = desEncrypter.encrypt(secretString);
     blowfishEncrypted  = blowfishEncrypter.encrypt(secretString);
     desedeEncrypted    = desedeEncrypter.encrypt(secretString);
 } catch (NoSuchAlgorithmException e) {}
}

这些是我使用的方法。 没问题,如果它作为应用程序运行,但是当我将它放到我的服务器上时,发生了异常,它说没有这样的算法。

忘记更改代码-整理环境。

您说它作为命令行应用程序运行时可以工作-我认为您的意思是在桌面上。 您可以在服务器上做同样的事情吗?

您在每个地方都使用什么版本的Java? 确保检查Glassfish中使用的版本-在命令行上运行java -version时获得的版本可能不相同。

顺便说一句,我希望您的真实代码不会吞噬此类异常。

尝试RC4算法,它很容易实现。

问题很可能是由于Glassfish启动时强制执行的Java bootclasspath造成的。 从IDE启动应用程序服务器时,这通常会令人讨厌。 例如:

> On 30-5-2005 8:00, Uwe Peuker wrote:
>
> It's probably caused by the way how/when Eclipse passes
> the -Xbootclasspath parameter to the java executable that's being
> launched.
>
> Don't know for 3.1RC1, but for older releases it depends on the setting
> "Use system default libraries" in the JRE configuration (Window ->
> Preferences -> Java -> Installed JREs -> (select JRE) -> Edit)
>
> When "Use system default libraries" is unchecked (off), Eclipse adds
> the -Xbootclasspath parameter --with all the libs in the list-- to the
> java executable. If the list doesn't include the crypto libraries, it
> results in the NoSuchAlgorithmException.
> Otherwise, when "Use system default libraries" is checked, Eclipse doesn't
> add -Xbootclasspath, so it allows the java executable to discover its own
> boot classpath including the crypto libraries.
> --
> Regards,
>
> Roland de Ruiter
> ___ ___
> /__/ w_/ /__/
> / \ /_/ / \

编辑 :对OP的评论:

(如果不是很明显,我既不是Roland de Ruiter也不是Uwe Peuker。我只是在Google搜索中找到了该电子邮件,并将其张贴在这里供您参考。)

无论如何,由于从Eclipse启动Glassfish时会出现问题,因此您应该尝试的第一件事是从命令行启动Glassfish(使用您的应用程序)。 如果这行得通(正如我预期的那样),那么问题显然出在Eclipse和/或您使用它的方式上。

假设我是对的,下一步将是捕获并检查启动JRE运行Glassfish时Eclipse使用的整套命令行参数。 特别是,您需要查看Eclipse是否提供--bootclasspath选项,以及它的值是什么。

自己执行某些密码算法具有很大的教学价值,但是正确地实施并非易事。 对于对称加密,通常的建议是AES,在FIPS-197中对其进行了详细描述(非常清晰)。 AES是一种块密码,即,它对16字节的块进行加密。 要加密“消息”(据说长于16个字节),您需要一些链接和填充(将消息转换为16个字节的块以供AES处理的一组约定); 这也不是一件容易的事。 有关填充和链接的介绍,请参见Wikipedia条目

但是,无法访问标准Java密码实现是可疑的。 您应该尝试先诊断一下。 尝试使用以下代码列出(使用一些Java代码)注册的提供程序:

for (Provider p : Security.getProviders()) {
    System.out.printf("%s -> %s\n", p.getName(), p.getInfo());
}

然后将输出与文档指定的内容进行比较。 特别是Sun提供程序列表

(注意:我们谈论的是服务器虚拟机-您的代码在其中运行-可能是其他供应商(例如IBM)的虚拟机;标准提供程序的列表可能有所不同。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM