将PrivateKey保存到本地

在做文件加密时,需要将私钥保存到本地,我在使用writeNewObject是老是报错,说是没有序列化

网上百度了一下,发现在Java中要保存对象的话,必须是序列化,但是找了一下PrivateKey好像已经是序列化了,但是就是不行,

没办法只好采取存二进制数据流的办法了

以下是解决的代码

filename是我自己的参数,不用管它。

/**
	 * 读privatekey对象
	 */
	public PrivateKey getPrivateKey(String filename) {
		PrivateKey privateKey=null;
		SharedPreferences preferences = context.getSharedPreferences("base64",Context.MODE_PRIVATE);
		String privateKeyBase64 = preferences.getString(filename, "");
		//Log.d("get",filename+" ; "+privateKeyBase64 );
		if (privateKeyBase64 == "") {
			return null;
		}
		//读取字节
		byte[] base64 = Base64.decodeBase64(privateKeyBase64.getBytes());
		KeyFactory keyFactory;
		try {
			keyFactory = KeyFactory.getInstance("RSA");
			PKCS8EncodedKeySpec pKCS8EncodedKeySpec =new PKCS8EncodedKeySpec(base64);
			privateKey = keyFactory.generatePrivate(pKCS8EncodedKeySpec);
			//Log.d("get",filename+" ; "+privateKey.toString() );
		} catch (InvalidKeySpecException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 指定算法RSA,得到一个KeyFactory的实例
		
			
		return privateKey;
	}
	/**
	 * 设置privatekey对象
	 */
	public void savePrivateKey(String filename,PrivateKey privateKey) {
		SharedPreferences preferences = context.getSharedPreferences("base64",Context.MODE_PRIVATE);
		//将字节流编码成base64的字符窜
		String privateKeyBase64 = new String(Base64.encodeBase64(privateKey.getEncoded()));
		Editor editor = preferences.edit();
		editor.putString(filename, privateKeyBase64);
		editor.commit();

	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中,使用 MapReduce 输出数据并保存到本地可以使用 `FileOutputFormat` 类。 首先,需要设置输出路径,可以使用 `FileOutputFormat.setOutputPath(job, new Path(outputPath))` 方法来设置。其中,`job` 是一个 `Job` 对象,表示 MapReduce 任务,`outputPath` 是输出路径的字符串。 然后,在 MapReduce 程序的 Reducer 类中,可以使用 `context.write(key, value)` 方法将结果输出。其中,`context` 是一个 `Reducer.Context` 对象,`key` 和 `value` 是输出的键值对。 例如,以下是一个简单的 MapReduce 程序,使用 MapReduce 计算文本文件中单词出现的次数,并将结果保存到本地文件中: ```java import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static class WordCountMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { String[] words = value.toString().split(" "); for (String w : words) { word.set(w); context.write(word, one); } } } public static class WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public void reduce ### 回答2: MapReduce是一种用于处理大规模数据集的并行计算框架,它可以帮助我们高效地处理和分析海量数据。而要将Java程序输出的MapReduce结果保存到本地,我们可以采用以下方法: 首先在Java程序中,我们需要按照MapReduce的结构编写Mapper和Reducer。Mapper负责将输入数据分割成可供处理的小块,然后生成键值对序列作为中间结果。而Reducer则负责将Mapper生成的中间结果进行合并、计算和输出。设置好Mapper和Reducer之后,我们可以通过Java的MapReduce库来进行调用。 在MapReduce的输出过程中,我们可以将结果保存到本地的文件系统中。在Java程序中,我们可以使用FileWriter或者BufferedWriter等类来创建一个输出文件,并将输出结果写入到文件中。在Reducer中,每当一个键值对完成处理时,将结果写入到输出文件中。 同时,为了方便读取结果文件,我们可以将输出结果保存在本地的特定路径下。通常情况下,输出路径可以直接指定为运行程序时的命令行参数,或者根据程序需要进行配置。设置好输出路径后,在MapReduce程序运行结束之后,我们就可以在本地文件系统中找到输出结果文件了。 总结起来,要使用Java的MapReduce框架输出数据并保存到本地,我们需要在程序中编写Mapper和Reducer,并设置输出路径来保存结果文件。通过以上步骤,我们就能够在本地成功保存MapReduce的输出结果了。 ### 回答3: Java中使用MapReduce框架进行数据处理和分析,并将结果保存到本地可以通过以下步骤实现: 1. 导入必要的Java库和MapReduce相关的类,如`org.apache.hadoop.mapreduce`和`org.apache.hadoop.conf.Configuration`等。 2. 创建一个`Configuration`对象,并设置相关的Hadoop配置,如输入和输出路径、文件系统等。 3. 创建一个`Job`对象,将配置对象传递给它,并设置相关的作业信息,如作业名称、输入路径、输出路径等。 4. 设置MapReduce作业的输入格式和输出格式,可以使用Hadoop的`TextInputFormat`和`TextOutputFormat`等类。 5. 设置Mapper和Reducer类,分别实现`Mapper`和`Reducer`接口,并在`job`对象中进行相关设置。 6. 如果需要使用Combiner,可以设置相关设置,并创建一个实现`Reducer`接口的Combiner类。 7. 提交作业给Hadoop集群,等待作业完成。 8. 在作业完成后,通过`job.waitForCompletion(true)`来检查作业的执行状态。 9. 在主函数中使用`FileSystem`来读取MapReduce作业的输出文件,并将数据保存到本地。 总结起来,使用Java的MapReduce框架进行数据处理并将结果保存到本地的关键步骤包括:配置Hadoop环境、设置作业信息、实现Mapper和Reducer类、设置输入输出格式和路径、提交作业并等待作业完成、读取输出文件并保存到本地

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值