解密数据中的java AES差异
我开发了一个实用程序,用于使用AES加密和解密文件,接收方和发送方之间的握手是在RSA PKI加密下完成的
现在,我有以下一系列步骤:
- 上传CSV李>
- 使用发件人的私钥生成内容哈希李>
- 使用发送方的私钥对哈希进行签名李>
- 生成对称密钥和IV
- 使用接收方的公钥使用IV.使用对称密钥加密实际csv李>
- 加密对称密钥和IV
现在在接收器端:
- 使用发送方的公钥解密哈希李>
- 使用发送方的公钥验证签名李>
- 使用接收方的私钥解密对称密钥和IV李>
- 使用对称密钥和iv解密实际csv李>
- 重新生成解密文件内容的哈希,并与解密的哈希匹配李>
现在,我的所有步骤都按照要求运行,但哈希不匹配。。。 当我区分纯文本csv和解密csv时。它们只有一线的区别
纯文本CSV的第一行
ICI,ICI-DIV1,,,,,,,,,,[Space]
[Next Line]
解密CSV的第一行
ICIŽICI-DIV1,,,,,,,,,,[Content of second line and so on..]
现在你可以看到,只有第一行的区别,即行尾的空格和行分隔符,第四个字符在这两行中都有很大的不同
我认为这是因为IV或任何需要的线路分隔。。 然而,第四个字符是某种编码问题
有人能识别并提出建议吗
用于使用对称密钥进行文件加密的加密代码
// Read content from CSV file..
public String readCsv ( String file ) {
// String builder to read all lines in one string..
StringBuilder sb = new StringBuilder();
// Line variable to hold each line data..
String line = EMPTY_STRING;
try {
// Read CSV file..
BufferedReader br = new BufferedReader( new FileReader( file ) );
// Read each line and append it to String builder
while ( ( line = br.readLine() ) != null ) {
sb.append( line );
sb.append( "\n" );
}
br.close();
}
// In case of exception..
catch ( Exception e ) {
System.out.println( "Exception in reading csv = " + e );
}
return sb.toString();
}
String encryptedPaymentFile = AESEncryptFile( contentOfFile.getBytes( "UTF-8" ) , AESParams.getKey(), AESParams.getIV() );
String contentOfFile = readCsv( encryptedFilePath ) ;
// Function to encrypt the file with AES Algorithm..
public static String AESEncryptFile( byte[] input, SecretKeySpec key, byte[] initVector ) {
// Initialize Cipher variable and encryption variable..
Cipher cipher = null;
byte[] cipherInBytes = null;
try {
/* 1. Set the cipher to AES Algorithm, CBC Mode and PKC5 Padding scheme..
* 2. Set the Cipher mode to encryption using key and initVector..
* 3. Get the encrypted text
* 4. return the cipher text with base64 encoding..
*/
cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
IvParameterSpec ivSpec = new IvParameterSpec( initVector );
cipher.init( Cipher.ENCRYPT_MODE, key, ivSpec );
cipherInBytes = cipher.doFinal( input );
return Base64.getEncoder().encodeToString( cipherInBytes );
}
// In case of exception..
catch ( Exception e ) {
System.out.println( "Exception in Main File Encryption " + e );
}
return Base64.getEncoder().encodeToString( cipherInBytes );
}
解密代码
public static String decrypt ( byte[] cipherText, SecretKeySpec key, byte[] IV ) throws Exception
{
//Get Cipher Instance
Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
//Create IvParameterSpec
IvParameterSpec ivSpec = new IvParameterSpec( IV );
//Initialize Cipher for DECRYPT_MODE
cipher.init( Cipher.DECRYPT_MODE, key, ivSpec );
byte[] decodedData = Base64.getDecoder().decode( cipherText );
String decryptedData = new String( cipher.doFinal( decodedData ) );
return decryptedData;
}
symmetricKey = RSADecrypt( keyFileInBytes, "Key");
initializationVector = RSADecrypt( ivFileInBytes, "Key");
if ( symmetricKey.equalsIgnoreCase( EMPTY_STRING ) ||
initializationVector.equalsIgnoreCase( EMPTY_STRING ) ) {
System.out.println( "Decryption of IV / KEY Failed .. " );
generateOutput( "Decryption of .Key files Failed.." , true);
return;
}
System.out.println( "Key and IV has been decrypted..");
SecretKeySpec symmKey = new SecretKeySpec( symmetricKey.getBytes(), "AES" );
decryptedMainFile = decrypt( encryptedFileInBytes, symmKey, initializationVector.getBytes() );
共 (0) 个答案