如何在Android JAVA代码中使用Camellia 128bit加密解密?
How to encrypt & decrypt using Camellia 128bit in Android JAVA code?
Android JAVA 代码中 Camellia 128 位的提供程序名称语法是什么?我尝试将 Cipher.getInstance("AES/CBC/PKCS7Padding")
更改为 Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC")
,但它说提供商 BC 不提供“Camellia/CBC/PKCS7Padding”。下面是我的代码
try {
String keyString = "Potatoman55@";//length of key is 16
Cipher desCipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC");
byte[] key = new byte[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
key = keyString.getBytes(StandardCharsets.UTF_8);
}
}
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
key = Arrays.copyOf(key, 16); // use only first 128 bit
}
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
desCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
String plainText = "hello wolrd";
System.out.println("plaintext: "+plainText);
byte[] text = plainText.getBytes("UTF-8");
byte[] textencrypted = desCipher.doFinal(text);
String textEnc = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
System.out.println("encrypted: " + Base64.getEncoder().encodeToString(textencrypted));
textEnc = Base64.getEncoder().encodeToString(textencrypted);
}
result.success(textEnc);
} catch (Exception ex) {
System.out.println(ex.toString());
}
我通过此代码 new org.bouncycastle.jce.provider.BouncyCastleProvider());
成功 运行 作为提供者的代码。非常感谢 评论。以下是完整语法
Cipher desCipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding",new org.bouncycastle.jce.provider.BouncyCastleProvider());
Android JAVA 代码中 Camellia 128 位的提供程序名称语法是什么?我尝试将 Cipher.getInstance("AES/CBC/PKCS7Padding")
更改为 Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC")
,但它说提供商 BC 不提供“Camellia/CBC/PKCS7Padding”。下面是我的代码
try {
String keyString = "Potatoman55@";//length of key is 16
Cipher desCipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC");
byte[] key = new byte[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
key = keyString.getBytes(StandardCharsets.UTF_8);
}
}
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
key = Arrays.copyOf(key, 16); // use only first 128 bit
}
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
desCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
String plainText = "hello wolrd";
System.out.println("plaintext: "+plainText);
byte[] text = plainText.getBytes("UTF-8");
byte[] textencrypted = desCipher.doFinal(text);
String textEnc = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
System.out.println("encrypted: " + Base64.getEncoder().encodeToString(textencrypted));
textEnc = Base64.getEncoder().encodeToString(textencrypted);
}
result.success(textEnc);
} catch (Exception ex) {
System.out.println(ex.toString());
}
我通过此代码 new org.bouncycastle.jce.provider.BouncyCastleProvider());
成功 运行 作为提供者的代码。非常感谢
Cipher desCipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding",new org.bouncycastle.jce.provider.BouncyCastleProvider());