【发布时间】:2022-01-18 13:05:12
【问题描述】:
我的要求是将下面的 java 加密代码转换为颤振,因为我需要在发送到 api 之前加密几个字段值。 下面是我需要转换成java的java加密代码
public static String encrypt(String text, String algo, Key key) {
byte[] cipherText;
String output;
try {
if("RSA".equals(algo)) {
throw new IllegalArgumentException("Do not pass just algo pass with padding and blocking stuff!");
}
if(BuildConfig.DEBUG) {
Log.d(TAG, "encrypt in: "+text);
Log.d(TAG, "algo: "+algo);
Log.d(TAG, "key: "+key);
}
final Cipher cipher = Cipher.getInstance(algo);
if(algo.contains("AES")) {
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
} else {
cipher.init(Cipher.ENCRYPT_MODE, key);
}
cipherText = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
output = new String(Base64.encode(cipherText));
if(BuildConfig.DEBUG) {
Log.d(TAG, "encrypt out: "+output);
}
return output;
} catch (Exception e) {
Log.e(TAG, SDKConstants.STRING_ERROR + e, e);
return null;
}
}
char[] encPwd = Objects.requireNonNull(CryptoHelper.encrypt(Objects.requireNonNull(binding.textIPassword.getEditText()).getText().toString(), CryptoHelper.ALGO_FOR_RSA, key)).toCharArray();
请帮助我将上面的 java 代码转换为颤振,因为我需要在将一个字段发送到 api 调用之前对其进行加密。
感谢任何帮助!
【问题讨论】:
-
@Richard Heap 请帮忙把上面的java代码转换成flutter,谢谢
-
Java 中的这个常量是什么?
CryptoHelper.ALGO_FOR_RSA是RSA/ECB/PKCS1吗?如果不是,那是什么? -
请注意 Stackoverflow 不是免费的代码转换服务
-
公共静态最终字符串 ALGO_FOR_RSA = "RSA/ECB/PKCS1Padding";
标签: flutter