私は暗号化と解読の新しいものです。そして、私はそれについて良い材料やチュートリアルを見つけることは本当に難しいと思っています。そして私はStackOverflowでそれに関連する質問を行って、私のために良い答えを見つけませんでした。私たちはENCRYPT_MODEとDECRYPT_MODEのために異なるCIpherを初期化できますか?
public DaoEncryptionResult<byte[]> getEncryptionResult(final ByteBuffer bufferToEncrypt) {
try {
final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
final SecretKey secretKey = new SecretKeySpec(BinaryKey, "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, mSecureRandom);
final byte[] bytesToEncrypt = bufferToEncrypt.array();
final byte[] cipherText = cipher.doFinal(bytesToEncrypt,
bufferToEncrypt.arrayOffset(), bufferToEncrypt.limit());
final byte[] iv = cipher.getIV();
return new DaoEncryptionResult<>(cipherText, iv);
} catch (final GeneralSecurityException securityException) {
throw new RuntimeException("Could not encrypt data", securityException);
}
}
public byte[] getDecryptionResult(final byte[] encodedData, final byte[] encodedIv) {
try {
final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
final SecretKey secretKey = new SecretKeySpec(BinaryKey, "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(encodedIv));
return cipher.doFinal(encodedData);
} catch (final GeneralSecurityException securityException) {
throw new RuntimeException("Could not decrypt data", securityException);
}
}
:
"AES/GCM/NoPadding"
が、私は以下の何かができる:私は変態で暗号化および復号化する場合はここで
は、私の問題ですmSecureRandomは、アプリケーションライフサイクル全体で1回呼び出され、
新規のSecureRandom()
BinaryKeyはencodedDataとencodedIvがgetEncryptionResult方法で返された結果と同じになりBase64.DEFAULT
と文字列からのBase64でデコードされたバイト[]であります。
これを行うことで、私はいつも解読の問題に関連する例外を見つけることができます。たとえば、次のようにコメント答えるため、長さやフォーマットへの答えとして提供
javax.crypto.BadPaddingException: mac check in GCM failed
at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:854)
at javax.crypto.Cipher.doFinal(Cipher.java:1340)
at com.amazon.rabbit.android.data.dao.DaoEncryptionManagerImpl.getDecryptionResult(DaoEncryptionManagerImpl.java:250)
誰でもこの暗号化と解読に関する優れた情報を知っていれば、ここでリンクや名前を共有できますか? – user2989950
このコードで問題が発生しましたか?あなたの質問は何ですか? –
私は見つけた例外を更新しました。 – user2989950