我已经将iv和SecretKey存储在单独的文件中,这样它们就可以在单独的进程中重复用于解密,而与加密何时完成无关。
示例iv值:在加密期间:encrypt iv创建:[-8,95,-47,-35,77,25,113,-110,95,51,71,-110,-92,-63,-95,-17]secretkey:javax.crypto.spec.secretkeyspec@16c11
解密时:读四:[-8,95,-47,-35,77,25,113,-110,95,51,71,-110,-92,-63,-95,-17]secretkey:javax.crypto.spec.secretkeyspec@16c11
蒂亚需要帮助,维杰
class AESCodec extends ... {
public static final String IV_FILE="C:/keystore/iv-file"
private static final String RANDOM_ALGORITHM = "SHA1PRNG";
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
static encode = { String target ->
def cipher = getCipher(Cipher.ENCRYPT_MODE)
return cipher.doFinal(target.bytes).encodeBase64()
}
static decode = { String target ->
//println "target:"+target
println "enter decode with target:"+target
def cipher = getCipher(Cipher.DECRYPT_MODE)
println "decode cipher:"+cipher
return new String(cipher.doFinal(target.decodeBase64()))//<============== failing here when running decode independently
}
private String secretPassword
private String getSecretKey() {
return "secret12"
}
private static getPassword() { new AESCodec().getSecretKey().getChars() }
private static byte[] iv
static SecretKey secretKey
/*
* Get key from Keystore where it is stored after creation
*/
private static SecretKey createKey(Integer mode) {
println "createKey() mode values 1=encrypt,2=decrypt mode:"+mode
if (secretKey == null) {
if (mode== Cipher.ENCRYPT_MODE) {
println "inside encrypt mode in createKey()"
byte[] salt = "DYKSalt".getBytes()
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
KeySpec spec = new PBEKeySpec(getPassword(), salt, 65536, 256)
SecretKey tmp = factory.generateSecret(spec)
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES")
secretKey = secret
println 'inside encrypt secretKey:'+secretKey
//store it in keystore
KeyStore ks = KeyStore.getInstance("JCEKS")
ks.load(null, null);
KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(secretKey)
//key alias and passwd
ks.setEntry("alias", skEntry,
new KeyStore.PasswordProtection("fggd".toCharArray()))
FileOutputStream fos = null
try {
fos = new FileOutputStream(AsymmetricCipher.KEYSTORE_DIR+"aes.keystore")
//keystore passwd
ks.store(fos, "fggd".toCharArray())
} finally {
fos?.close()
}
} else if (mode== Cipher.DECRYPT_MODE) {
InputStream inputStream = getKeystoreAsStream(AsymmetricCipher.KEYSTORE_DIR+"aes.keystore")
BufferedInputStream fis = null
try {
fis = new BufferedInputStream(inputStream)
println "fis:"+fis
//keystore passwd
KeyStore ks = KeyStore.getInstance("JCEKS")
//get key store
ks.load(fis, "fggd".toCharArray())
//get key from keystore
Entry entry = ks.getEntry("alias", new KeyStore.PasswordProtection("fggd".toCharArray()))
KeyStore.SecretKeyEntry secretKeystoreEntry = (KeyStore.SecretKeyEntry)entry
secretKey = secretKeystoreEntry.getSecretKey()
println " returned secretKey from decrypt mode"
} finally {
fis?.close()
}
}
} else {
}
return secretKey
}
private SecretKey getKey() {
return secretKey
}
private static getCipher(mode) {
SecretKey secret = createKey(mode)
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
println "secret:"+secret.getEncoded()
println "cipher:"+cipher
if (mode == Cipher.DECRYPT_MODE) {
//get iv
iv = readIvFromFile(IV_FILE)
println "decrypt iv created from file:"+iv
IvParameterSpec ivspec = new IvParameterSpec(iv);
println "ivspec:"+ivspec
cipher.init(mode, secret,ivspec)
//cipher.init(mode, secret)
} else {
//save that to IV_FILE
byte[] iv = generateIv();
println "encrypt iv created:"+iv
IvParameterSpec ivspec = new IvParameterSpec(iv);
println "ivspec:"+ivspec
cipher.init(mode, secret,ivspec)
//iv = cipher.getIV()
AlgorithmParameters params = cipher.getParameters()
iv = params.getParameterSpec(IvParameterSpec.class).getIV()
saveToFile(IV_FILE,iv)
}
return cipher
}
private static byte[] generateIv() throws NoSuchAlgorithmException {
SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM);
byte[] iv = new byte[16];
random.nextBytes(iv);
return iv;
}
public static void saveToFile(String fileName,
byte[] iv) throws IOException {
println "saveToFile() fileName:"+fileName
FileOutputStream oout =
new FileOutputStream(fileName);
try {
oout.write(iv);
} catch (Exception e) {
throw new IOException("Unexpected error", e);
} finally {
oout?.flush()
oout?.close()
}
}
private static byte[] readIvFromFile(String keyFileName)
throws IOException {
println "readIvFromFile() keyFileName:"+keyFileName
InputStream inputStream = AsymmetricCipher.getFile(keyFileName)
try {
iv = IOUtils.toByteArray(inputStream);
println "read iv:"+iv
return iv;
} catch (Exception e) {
throw new RuntimeException("Spurious serialisation error ", e);
} finally {
inputStream = null
//oin?.close();
}
}
static void main(String[] args) {
String message="This is just an example";
if(args) {
def encryptedValue = encode(args[0])
println "encryptedValue:"+encryptedValue //byte[]
String encryptedValue1=(String)(encryptedValue)
println "encryptedValue1:"+encryptedValue1
def decryptedValue = decode(encryptedValue1)
println "decryptedValue:"+decryptedValue
def decryptedValueStr = (String)decryptedValue
}
//
}
}
问题内容: 我需要有关此错误的帮助:给最终块未正确填充。从标题中可以看到,我正在使用AES。 这是错误的行代码: 这是完整的代码: } 问题答案: 根据您的评论,您几乎可以使加密工作。 您需要将IV生成代码从您的加密/解密方法移动到其他地方,就像这样 然后将该ivspec传递到加密和解密方法中(使它们看起来像),这样您就可以为加密和解密使用相同的iv。 另外,不要调用decryptedByteAr
我想加密EnteredDetails(java bean)类型的arraylist,并将其序列化到一个文件中。我正在关注AES-128位加密的链接:http://www . code 2 learn . com/2011/06/encryption-and-decryption-of-data-using . html 要使用aes class的encrypt方法,我必须将arrarylist转换
首先,我会告诉你我的主要目标是什么。在客户端使用AES加密部分内容,然后使用RSA公钥加密重要的AES规范,并将AES加密数据和RSA加密的AES规范发送到服务器端。所以在服务器端,我将使用RSA私钥解密AES密钥规范,然后使用这些AES规范,我将解密AES加密数据。通过测试加密和解密,我成功地使RSA部分工作。在此实现RSa之前,我必须使AES art工作。 对于客户端,我使用crypto-js
我正在尝试编写方法来加密或解密字符串(大部分是数字)。它适用于某些文本(例如-'1010000011'、'1010000012'、'1010000013'),但也适用于其他文本(例如-'1010000014'、'1010000018'): javax.crypto.BadPadding异常:给定最后一个块没有正确填充 这是我的代码: 要加密的字符串从文件中读取,并在加密后写入其他文件。这些加密的文
如果公司营业额大于50亿卢比,GST理事会已批准将企业对企业(B2B)发票的“电子发票”或“电子发票”实施到GST系统。参考商品及服务税门户 API :einv-apisandbox.nic.in/index.html 我必须使用APP密钥对加密的SEK进行解密,并使用解密的SEK对json数据进行编码,以便发布Einvoice Generation,我找到了java和C#的示例代码,我已经用PH
问题内容: 我正在尝试实现基于密码的加密算法,但出现此异常: javax.crypto.BadPaddingException:给定的最终块未正确填充 可能是什么问题? 这是我的代码: (JUnit测试) 问题答案: 如果尝试使用错误的密钥解密填充了PKCS5的数据,然后取消填充(由Cipher类自动完成),则很可能会收到BadPaddingException(可能略小于255/256,约为99.