java 算法移植_Java:Chacha20算法(从openssl移植)
使用示例
String str = "hello world";
int key[] = {-123,-456,789,123,456,7890,456,456};
int noc[] = {-123,-456,789,123};
byte[] encryptData = Chacha20.crytpoCounter32(str.getBytes(),key,noc);
byte[] decryptData = Chacha20.crytpoCounter32(encryptData,key,noc);
String str2 = new String(decryptData);
boolean equal = str.equals(str2);
具体代码
package com.zxs.zl;
/*
* Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
public class Chacha20 {
/*
* ChaCha20_ctr32 encrypts |len| bytes from |inp| with the given key and
* nonce and writes the result to |out|, which may be equal to |inp|.
* The |key| is not 32 bytes of verbatim key material though, but the
* said material collected into 8 32-bit elements array in host byte
* order. Same approach applies to nonce: the |counter| argument is
* pointer to concatenated nonce and counter values collected into 4
* 32-bit elements. This, passing crypto material collected into 32-bit
* elements as opposite to passing verbatim byte vectors, is chosen for
* efficiency in multi-call scenarios.
*/
/**
* function same ,encrypt and decrypt
* @param inp source data (encrypt data or decrypt data)
* @param key int[8] array
* @param counter int[4] array
* @return decrypt data or encrypt data ; see inp param.
*/
public static byte[] crytpoCounter32(byte[] inp, int[] key,int[] counter)
{
byte[] out = new byte[inp.length];
int[] input = new int[16];
byte[] buf = new byte[64];
int todo, i;
/* sigma constant "expand 32-byte k" in little-endian encoding */
input[0] = ((int)'e') | ((int)'x'<<8) | ((int)'p'<<16) | ((int)'a'<<24);
input[1] = ((int)'n') | ((int)'d'<<8) | ((int)' '<<16) | ((int)'3'<<24);
input[2] = ((int)'2') | ((int)'-'<<8) | ((int)'b'<<16) | ((int)'y'<<24);
input[3] = ((int)'t') | ((int)'e'<<8) | ((int)' '<<16) | ((int)'k'<<24);
input[4] = key[0];
input[5] = key[1];
input[6] = key[2];
input[7] = key[3];
input[8] = key[4];
input[9] = key[5];
input[10] = key[6];
input[11] = key[7];
input[12] = counter[0];
input[13] = counter[1];
input[14] = counter[2];
input[15] = counter[3];
int len = inp.length;
int offset = 0;
while (len > 0) {
todo = 64;// equal to buf.length;
if (len < todo)
todo = len;
chacha20_core(buf, input);
for (i = 0; i < todo; i++){
out[i+offset] = (byte) (inp[i+offset] ^ buf[i]);
}
offset += todo;
len -= todo;
/*
* Advance 32-bit counter. Note that as subroutine is so to
* say nonce-agnostic, this limited counter width doesn't
* prevent caller from implementing wider counter. It would
* simply take two calls split on counter overflow...
*/
input[12]++;
}
return out;
}
/* chacha_core performs 20 rounds of ChaCha on the input words in
* |input| and writes the 64 output bytes to |output|. */
private static void chacha20_core(byte[] output, int[] input)
{
//output byte[64]
long[] x = new long[16];
for (int i = 0 ; i < 16 ;i++){
x[i] = (input[i] & 0xFFFFFFFFL );
}
for (int i = 20; i > 0; i -= 2) {
QUARTERROUND(x,0, 4, 8, 12);
QUARTERROUND(x,1, 5, 9, 13);
QUARTERROUND(x,2, 6, 10, 14);
QUARTERROUND(x,3, 7, 11, 15);
QUARTERROUND(x,0, 5, 10, 15);
QUARTERROUND(x,1, 6, 11, 12);
QUARTERROUND(x,2, 7, 8, 13);
QUARTERROUND(x,3, 4, 9, 14);
}
for (int i = 0; i < 16; ++i) {
long v = (x[i] + input[i]) & 0xFFFFFFFFL;
output[i<<2] = (byte) (v & 0xFF);
output[(i<<2) + 1] = (byte) ((v>>8) & 0xFF);
output[(i<<2) + 2] = (byte) ((v>>16) & 0xFF);
output[(i<<2) + 3] = (byte) ((v>>24) & 0xFF);
}
}
/* QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. */
private static void QUARTERROUND(long[]x, int a,int b,int c,int d){
x[a] = (x[a] + x[b]) & 0xFFFFFFFFL;
x[d] = ROTATE((x[d] ^ x[a]),16) & 0xFFFFFFFFL;
x[c] = (x[c] + x[d]) & 0xFFFFFFFFL;
x[b] = ROTATE((x[b] ^ x[c]),12) & 0xFFFFFFFFL;
x[a] = (x[a] + x[b]) & 0xFFFFFFFFL;
x[d] = ROTATE((x[d] ^ x[a]), 8) & 0xFFFFFFFFL;
x[c] = (x[c] + x[d]) & 0xFFFFFFFFL;
x[b] = ROTATE((x[b] ^ x[c]), 7) & 0xFFFFFFFFL;
}
private static long ROTATE(long v,int n)
{
v = v & 0xFFFFFFFFL;
return (((v) << (n)) | ((v) >> (32 - (n))));
}
}
java 算法移植_Java:Chacha20算法(从openssl移植)相关推荐
- java基础排序_Java排序算法-Java入门|Java基础课程
1. 课程目标排序是任何语言都会使用到的功能之一,然成果排序的算法有很多,对空间的要求及其时间效率也不尽相同. 本文章以Java语言示例,通过对空间要求.时间效率要求,来对比各种排序算法的使用场景 2 ...
- java java算法题目_java常见算法题目
1: JAVA经典算法40题 2: [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 3 ...
- Java算法大全_java贪心算法几个经典例子
Java经典问题算法大全 /*[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? ...
- java cas 实现_Java CAS算法简介及简单模拟CAS算法
CAS(Compare-And-Swap:比较并替换) CAS是英文单词CompareAndSwap的缩写,意思就是:比较并替换.简单来说就是比较之后再看情况是否需要替换.CAS是乐观锁思想的一种实现 ...
- java椭圆 类_java 椭圆算法
展开全部 以下代码,将输出一个椭圆,再有问题,我可远程助你.如下:/** *(300,100)(400,100) * */ import java.awt.*; import javax.swing. ...
- java程序算法实例_java编程算法经典案例
编程经典案例(持续更新中,敬请期待): 一.购物问题 小明的女朋友最喜欢在网上买买买了,可是钱包里钞票有限,不能想买啥就买啥.面对琳琅满目的物品,她想买尽可能多的种类,每种只买一件,同时总价格还不能超 ...
- java归并排序代码_Java归并排序算法
[java]代码库import java.util.Arrays; public class mergingSort { int a[]={49,38,65,97,76,13,27,49,78,34, ...
- java文本压缩算法_java 什么算法压缩文件最小
展开全部 有三种方式实现java压缩: 1.jdk自带的包java.util.zip.ZipOutputStream,不足之处,文件(夹)名称32313133353236313431303231363 ...
- java工作两年可以转算法吗_java转算法后平淡的一年(迷茫2019)
不想再写悲惨了,还是迷茫更可怕一点.为啥感觉不到惨了呢?可能是麻木了吧,也可能是这一年实在是没啥起伏.每天都差不多是吃饭.上班.吃饭.下班.吃饭.轻微锻炼.节假日激烈的锻炼锻炼.集中相亲又败了一小波, ...
- java经典算法大全_JAVA 经典算法大全:PageRank算法
public class PageRank { private static double alpha = 0.85; /** * PageRank vector q is defined as q ...
最新文章
- cisco vPC技术
- 九个月可以做成什么事?
- 提高篇 第一部分 基础算法 第4章 广搜的优化技巧
- Stm32:地址空间划分
- 696.计数二进制子串(力扣leetcode) 博主可答疑该问题
- ghostscript windows mac 下安装和 C++ 程序调用
- JAVA自学之路 来自尚学堂马士兵老师
- 资料:高等数学学习笔记——高等数学(二)学习笔记汇总
- 测试人,测试魂,3年测遍32个城,主管的一句话给予了他源源不断的动力
- 开心网kaixin001状告kaixin,停用“开心网”名称,赔偿1000万元
- 智能暖风机——8.云端控制
- 定积分的基本性质5 区间可加性
- 创建微服务架构的步骤_如何快速搭建一个微服务架构?
- 微型计算机原理...,微型计算机原理
- echarts 盒须图配置
- 默克尔树 Merkle树之Go语言实现
- Socket 【网络通信 - Socket】
- 【Spire.PDF】Spire.PDF破解
- 《C++语言基础》实践参考——考了语文数学的学生
- 友盟消息推送服务器demo,umeng友盟消息推送功能集成