我有一个使用256位AES加密的应用程序,开箱即用的Java不支持.我知道要使其正常运行我在安全文件夹中安装JCE无限强度jar.这对我作为开发人员来说很好,我可以安装它们.
我的问题是,由于此应用程序将被分发,最终用户很可能不会安装这些策略文件.让最终用户下载这些只是为了使应用程序功能不是一个有吸引力的解决方案.
有没有办法让我的应用程序运行而不覆盖最终用户机器上的文件?可以在没有安装策略文件的情况下处理它的第三方软件?或者从JAR中引用这些策略文件的方法?
我正在eclipse 上开展基于Web的文本加密和解密项目(遵循Struts 2)
每当我输入密码和纯文本时,我都会收到无效的AES密钥长度错误.
服务类(SymAES.java)
package com.anoncrypt.services;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class SymAES
{
private static final String ALGORITHM = "AES";
private static byte[] keyValue= new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
public String encode(String valueToEnc) throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = …Run Code Online (Sandbox Code Playgroud) 我得到以下错误,我有点卡住:线程"主"中的异常
java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1011)
at javax.crypto.Cipher.implInit(Cipher.java:786)
at javax.crypto.Cipher.chooseProvider(Cipher.java:849)
at javax.crypto.Cipher.init(Cipher.java:1213)
at javax.crypto.Cipher.init(Cipher.java:1153)
at net.nakou.indie.wtext.engineClass.Session.cryptString(Session.java:52)
Run Code Online (Sandbox Code Playgroud)
我被困了,因为我发现的所有答案都谈到了Java 密码学扩展(JCE),它通常包含在android SDK中.所以我认为我的问题不是这个问题.
我一定忘记了什么,但我找不到什么.也许我的代码是错误的(这是我在Java中使用加密技术的第一种方法,我不是专家,以下代码主要是教程的一些副本).
我使用此代码来加密和解密字符串:
public String cryptString(String s) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException {
byte[] KeyData = this.cryptKey.getBytes();
SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, KS);
String ret = new String(cipher.doFinal(s.getBytes("UTF-8")));
return ret;
}
public String decryptString(byte[] s) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
byte[] KeyData = this.cryptKey.getBytes();
SecretKeySpec KS = …Run Code Online (Sandbox Code Playgroud) 任何人都可以向我展示(或提供一个链接)如何使用充气城堡加密Java文件?我查看过bouncycastle.org但找不到任何API的文档.即使只知道使用哪些课程对我来说也是一个很大的帮助!
我有一个使用Mojarra 2.2.9的JSF应用程序,并在WebSphere 8.5.5.4上部署在集群环境中并javax.faces.STATE_SAVING_METHOD设置为client.
即使我的所有应用程序bean都是请求作用域,有时候当用户会话有效并且用户在他获得的页面上进行发布请求时ViewExpiredException.可能导致此问题的原因以及如何解决?将改变javax.faces.STATE_SAVING_METHOD以server解决呢?如果是这样,这对内存有什么影响?
此外,这是否与集群环境有关,也许Websphere上有一些缺少配置可以解决问题?
我收到java.security.InvalidKeyException:非法的密钥大小或默认参数,我已经休闲所有必需的步骤,安装Java密码学扩展(JCE)无限强度管辖政策文件.我也已经通过这些线程了
Java.security.InvalidKeyException:非法密钥大小或默认参数错误
但我仍然卡住并得到java.security.InvalidKeyException:非法的密钥大小或默认参数,
下面是我的代码:AESKeyGenerator.java
public class AESKeyGenerator {
private Cipher mCipher;
public AESKeyGenerator()
{
// default constructor
}
public byte[] generate_k(String dhkey, String toEncrypt)
{
byte[] retVal;
try { // Set up the Cipher class of Android to use AES to generate keys
byte[] iv = new byte[16];
for (int i = 0; i < iv.length; i++)
iv[i] = new Byte("0").byteValue();
IvParameterSpec ivspec = new IvParameterSpec(iv);
mCipher = Cipher.getInstance("AES");
// Set up key to use in algorithm
MessageDigest hasher …Run Code Online (Sandbox Code Playgroud) 这是测试人员:
public class CryptographySimpleTests extends ActivityTestCase
{
public void testsCryptographyClass_encryptAndDecrypt()
{
final String orgVal = "hi world! :D";
final String key = "key";
try
{
final byte[] encryptKey = Cryptography.deriveAES256Key(key);
final byte[] decryptKey = Cryptography.deriveAES256Key(key);
//Deviation method
Assert.assertTrue(Arrays.equals(encryptKey, decryptKey));
byte[] encrypted = Cryptography.encryptAES(encryptKey, orgVal.getBytes());
Assert.assertFalse(Arrays.equals(encrypted, orgVal.getBytes()));
byte[] decrypted = Cryptography.decryptAES(decryptKey, encrypted);
Assert.assertTrue(Arrays.equals(orgVal.getBytes(), decrypted));
}
catch (Exception e) {
Assert.fail(e.getMessage());
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于最后一个断言而失败:
Assert.fail(e.getMessage());
Run Code Online (Sandbox Code Playgroud)
尝试执行时:
byte[] decrypted = Cryptography.decryptAES(decryptKey, encrypted);
Run Code Online (Sandbox Code Playgroud)
给出这个堆栈跟踪:
javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at …Run Code Online (Sandbox Code Playgroud) JSSE在Apache Tomcat服务器中支持的密码是什么?如何启用AES256并重新排序密码?
当密钥长度为128位时,一切正常.但是当我使用长度为192或256位的密钥时,我得到以下异常.
java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
Run Code Online (Sandbox Code Playgroud)
我发现这个Java安全:非法密钥大小或默认参数?.但是在我下载jar文件并将其放入之后${java.home}/lib/security/,我仍然得到了相同的异常.
所以我有(这不起作用b/c十六进制可能是错误的,键和IV没有正确转换):
(aesKey和aesIV作为第三方的十六进制字符串提供)
它们看起来像这样(不一样,但应该足以使用我替换键中的一些值,所以它们不完全相同:
<cfparam name="aesKey" default="C20648780E8843795325F3BA5EC43183C8BFA2D26B5470BC309ED5BA6B142EFA"/>
<cfparam name="aesIV" default="A53F0A6E6972A0095CFFDBE4F47C3CF8"/>
<cfset token = Encrypt(encryptString, aesKey, "AES/CBC/PKCS5Padding", "hex", aesIV)>
Run Code Online (Sandbox Code Playgroud)
错误是:
指定的密钥不是此加密的有效密钥:非法密钥大小或默认参数.
(我也不确定"十六进制"是对的)
我也有来自第三方的这个
第三方使用以下参数进行AES加密:
块长度256位
填充PKCS7
密码模式CBC
密钥长度256位(由第三方以十六进制格式提供)
初始化向量长度128位(由第三方以十六进制格式提供)秘密(私有)密钥和初始化向量用于对明文令牌执行AES加密.然后将加密的字符串传递给第三方SSO进程,在该进程中使用匹配的密钥和初始化向量对其进行解密.
所以我没有使用密钥或iv进行任何格式化或转换,但错误表明我需要管理它.
但那是我猜的地方(它确实需要一个字符串,它只是我传递的字符串是错误的)
我知道我很接近,我确实有一个"无论如何使其工作"的解决方案(我从CF转到.net并使用提供的示例代码),但我不想这样做,...但我确实拥有它.(这是我第二次从语言B回到语言A,因为我有一些有用的东西)
所以我想在一个方法来解密消息,但它不工作,因为我需要做cipher.init(Cipher.ENCRYPT_MODE, secret)之前,我尝试添加new IvParameterSpec(iv)到cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));.否则,它只返回一个NullPointerException我想知道是否可以在方法中执行此操作而不是一直写入它.我真的不能想到一个解决方案,这就是为什么我在这里.加密工作正常但不解密.
项目运行: JRE 7
加密代码:
public static String encrypt(String str) {
try {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(str.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret); //<--- Need to do this before writing IvPerameterSpec,
// But I think that it's not possible if …Run Code Online (Sandbox Code Playgroud) java ×10
aes ×6
encryption ×6
android ×2
jce ×2
bouncycastle ×1
coldfusion ×1
cryptography ×1
jsf ×1
jsf-2.2 ×1
jsse ×1
policyfiles ×1
security ×1
tomcat ×1
websphere ×1
websphere-8 ×1