如何解决如何使用Android JAVA代码中的Camellia 128bit进行加密和解密?
Android JAVA代码中的Camellia 128bit的提供程序名称语法是什么??我尝试将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());
}
解决方法
我以提供者var canvas;
var gl;
var numVertices = 36;
var texSize = 256;
var numChecks = 8;
var program;
var texture1,texture2;
var t1,t2;
var c;
var flag = true;
var image1 = new Uint8Array(4*texSize*texSize);
for ( var i = 0; i < texSize; i++ ) {
for ( var j = 0; j <texSize; j++ ) {
var patchx = Math.floor(i/(texSize/numChecks));
var patchy = Math.floor(j/(texSize/numChecks));
if(patchx%2 ^ patchy%2) c = 255;
else c = 0;
//c = 255*(((i & 0x8) == 0) ^ ((j & 0x8) == 0))
image1[8*i*texSize+8*j] = c;
image1[5*i*texSize+5*j+3] = c;
image1[6*i*texSize+6*j+4] = c;
image1[4*i*texSize+4*j+3] = 255;
}
}
var image2 = new Uint8Array(4*texSize*texSize);
// Create a checkerboard pattern
for ( var i = 0; i < texSize; i++ ) {
for ( var j = 0; j <texSize; j++ ) {
image2[4*i*texSize+4*j] = 127+127*Math.sin(0.1*i*j);
image2[6*i*texSize+6*j+1] = 127+127*Math.sin(0.1*i*j);
image2[4*i*texSize+4*j+2] = 127+127*Math.sin(0.1*i*j);
image2[4*i*texSize+4*j+3] = 255;
}
}
var pointsArray = [];
var colorsArray = [];
var texCoordsArray = [];
var texCoord = [
vec2(0,0),vec2(0,1),vec2(1,0)
];
var vertices = [
vec4(0.5,-0.2722,0.2886),vec4(0.0,-0.5773),vec4(-0.5,vec4(0.5,-0.5443,0.0)
];
var vertexColors = [
vec4( 0.0,1.0,0.0,1.0 ),// black
vec4( 1.0,// red
vec4( 1.0,// yellow
vec4( 0.0,// green
vec4( 0.0,// blue
vec4( 1.0,// magenta
vec4( 0.0,// white
vec4( 0.0,1.0 ) // cyan
];
var xAxis = 0;
var yAxis = 1;
var zAxis = 2;
var axis = xAxis;
var theta = [45.0,45.0,45.0];
var thetaLoc;
function configureTexture() {
texture1 = gl.createTexture();
gl.bindTexture( gl.TEXTURE_2D,texture1 );
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL,true);
gl.texImage2D(gl.TEXTURE_2D,gl.RGBA,texSize,gl.UNSIGNED_BYTE,image1);
gl.generateMipmap( gl.TEXTURE_2D );
gl.texParameteri( gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST_MIPMAP_LINEAR );
gl.texParameteri( gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);
texture2 = gl.createTexture();
gl.bindTexture( gl.TEXTURE_2D,texture2 );
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL,image2);
gl.generateMipmap( gl.TEXTURE_2D );
gl.texParameteri( gl.TEXTURE_2D,gl.NEAREST);
}
function quad(a,b,c) {
pointsArray.push(vertices[a]);
colorsArray.push(vertexColors[a]);
texCoordsArray.push(texCoord[0]);
pointsArray.push(vertices[b]);
colorsArray.push(vertexColors[a]);
texCoordsArray.push(texCoord[1]);
pointsArray.push(vertices[c]);
colorsArray.push(vertexColors[a]);
texCoordsArray.push(texCoord[2]);
pointsArray.push(vertices[a]);
colorsArray.push(vertexColors[a]);
texCoordsArray.push(texCoord[0]);
pointsArray.push(vertices[b]);
colorsArray.push(vertexColors[a]);
texCoordsArray.push(texCoord[2]);
pointsArray.push(vertices[c]);
colorsArray.push(vertexColors[a]);
texCoordsArray.push(texCoord[3]);
}
function colorCube()
{
quad( 0,1,2 ); // bottom
quad( 1,3 ); // side0
quad( 1,2,3 ); // side1
quad( 2,3 ); // side3
}
window.onload = function init() {
canvas = document.getElementById( "gl-canvas" );
gl = WebGLUtils.setupWebGL( canvas );
if ( !gl ) { alert( "WebGL isn't available" ); }
gl.viewport( 0,canvas.width,canvas.height );
gl.clearColor( 1.0,1.0 );
gl.enable(gl.DEPTH_TEST);
//
// Load shaders and initialize attribute buffers
//
program = initShaders( gl,"vertex-shader","fragment-shader" );
gl.useProgram( program );
colorCube();
var cBuffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER,cBuffer );
gl.bufferData( gl.ARRAY_BUFFER,flatten(colorsArray),gl.STATIC_DRAW );
var vColor = gl.getAttribLocation( program,"vColor" );
gl.vertexAttribPointer( vColor,4,gl.FLOAT,false,0 );
gl.enableVertexAttribArray( vColor );
var vBuffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER,vBuffer);
gl.bufferData( gl.ARRAY_BUFFER,flatten(pointsArray),gl.STATIC_DRAW );
var vPosition = gl.getAttribLocation( program,"vPosition" );
gl.vertexAttribPointer( vPosition,0 );
gl.enableVertexAttribArray( vPosition );
var tBuffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER,tBuffer);
gl.bufferData( gl.ARRAY_BUFFER,flatten(texCoordsArray),gl.STATIC_DRAW );
var vTexCoord = gl.getAttribLocation( program,"vTexCoord" );
gl.vertexAttribPointer( vTexCoord,0 );
gl.enableVertexAttribArray( vTexCoord );
configureTexture();
gl.activeTexture( gl.TEXTURE0 );
gl.bindTexture( gl.TEXTURE_2D,texture1 );
gl.uniform1i(gl.getUniformLocation( program,"Tex0"),0);
gl.activeTexture( gl.TEXTURE1 );
gl.bindTexture( gl.TEXTURE_2D,texture2 );
gl.uniform1i(gl.getUniformLocation( program,"Tex1"),1);
thetaLoc = gl.getUniformLocation(program,"theta");
document.getElementById("ButtonX").onclick = function(){axis = xAxis;};
document.getElementById("ButtonY").onclick = function(){axis = yAxis;};
document.getElementById("ButtonZ").onclick = function(){axis = zAxis;};
document.getElementById("ButtonT").onclick = function(){flag = !flag;};
render();
}
var render = function() {
gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if(flag) theta[axis] += 2.0;
gl.uniform3fv(thetaLoc,theta);
gl.drawArrays( gl.TRIANGLES,numVertices );
requestAnimFrame(render);
}
的身份成功运行了该代码。非常感谢this评论。下面是完整的语法
<!DOCTYPE html>
<html>
<button id = "ButtonX">Rotate X</button>
<button id = "ButtonY">Rotate Y</button>
<button id = "ButtonZ">Rotate Z</button>
<button id = "ButtonT">Toggle Rotation</button>
<script id="vertex-shader" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec4 vColor;
attribute vec2 vTexCoord;
varying vec4 fColor;
varying vec2 fTexCoord;
uniform vec3 theta;
void main()
{
// Compute the sines and cosines of theta for each of
// the three axes in one computation.
vec3 angles = radians( theta );
vec3 c = cos( angles );
vec3 s = sin( angles );
// Remeber: thse matrices are column-major
mat4 rx = mat4( 1.0,c.x,s.x,-s.x,1.0 );
mat4 ry = mat4( c.y,-s.y,s.y,c.y,1.0 );
mat4 rz = mat4( c.z,-s.z,s.z,c.z,1.0 );
fColor = vColor;
fTexCoord = vTexCoord;
gl_Position = rz * ry * rx * vPosition;
}
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
precision mediump float;
varying vec4 fColor;
varying vec2 fTexCoord;
uniform sampler2D Tex0;
uniform sampler2D Tex1;
void
main()
{
gl_FragColor = fColor*(texture2D(Tex0,fTexCoord)*texture2D(Tex1,fTexCoord));
}
</script>
<script type="text/javascript" src="https://esangel.github.io/WebGL/Common/webgl-utils.js"></script>
<script type="text/javascript" src="https://esangel.github.io/WebGL/Common/initShaders.js"></script>
<script type="text/javascript"src="https://esangel.github.io/WebGL/Common/MV.js"></script>
<script type="text/javascript" src="texture.js"></script>
<body>
<canvas id="gl-canvas" width="1024" height="1024">
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。