Kiểm tra xem chuỗi có phải là Base64 Java không

Vì tất cả các Dịch vụ web REST và SOAP, tải trọng của Dịch vụ web tôi đang sử dụng, phân phối tải trọng dưới dạng tệp văn bản có cấu trúc nhưng đơn giản. Điều này có nghĩa là, tất cả dữ liệu được gửi ở định dạng Văn bản. Nhưng để có thể, ví dụ, lưu trữ thông tin về giá trong cơ sở dữ liệu và sử dụng nó trong các phép tính toán học, cần phải chuyển đổi giá trị từ Văn bản sang Số thập phân. Người tiêu dùng Dịch vụ web của tôi có thể đọc tải trọng, để xác định và tự động chuyển đổi các Kiểu dữ liệu. Nhưng ngay sau khi thông tin nhị phân được gửi trong tải trọng, người tiêu dùng của tôi sẽ đạt đến giới hạn vì thông tin nhị phân sẽ được lưu trữ dưới dạng Văn bản

May mắn thay, tất cả thông tin nhị phân của dịch vụ REST hoặc SOAP sẽ được mã hóa ở định dạng Base64, do đó, việc xác định xem đó là giá trị văn bản thông thường [như tên công ty hoặc hướng dẫn gửi] hay thông tin nhị phân là một nhiệm vụ dễ dàng.

Dữ liệu được mã hóa sẽ luôn có đặc điểm sau

  • Độ dài của chuỗi được mã hóa Base64 luôn là bội số của 4
  • Chỉ những ký tự này được sử dụng bởi mã hóa. “A” đến “Z”, “a” đến “z”, “0” đến “9”, “+” và “/”
  • Phần cuối của một chuỗi có thể được đệm tối đa hai lần bằng cách sử dụng ký tự “=” [ký tự này chỉ được phép ở cuối chuỗi]


Chỉnh sửa ngày 23 tháng 4 năm 2021.
Tôi đã viết bài đăng trên blog này dựa trên công việc của tôi trong ứng dụng ERP Microsoft Dynamics 365 Business Central có ngôn ngữ lập trình dành riêng cho miền [ngôn ngữ này rất cũ và giống với Pascal]. Tuy nhiên, một số công cụ tìm kiếm đã quyết định sử dụng bài đăng trên blog này làm kết quả tìm kiếm tự nhiên số 1. Nếu dự án hiện tại của bạn là bằng Java, C++, JavaScript hoặc Python, thì vui lòng xem trong chủ đề Stackoverflow này cũng liên quan đến chủ đề này. https. // stackoverflow. com/questions/8571501/how-to-check-whether-a-string-is-base64-encoded-or-not

Nếu bạn quan tâm đến mã C/AL, vui lòng kiểm tra mã bên dưới

Xác định Base64 trong C/AL

Dựa trên thông tin này, có thể xây dựng một hàm có thể xác định giá trị của chuỗi dưới dạng được mã hóa Base64

LOCAL PROCEDURE isBase64@1000000002[pVariant@1000000000 : Variant] : Boolean;
VAR
 i@1000000001 : Integer;
BEGIN
 // the length of a base64 is always a multiple of 4
 // allowed characters in a base64 are: 'A'..'Z', 'a'..'z', '0'..'9', '+', '/'
 // the end can be padded with up to 2 '='

 // check if the length is a multiple of 4
 IF [STRLEN[FORMAT[pVariant,0,9]] / 4 MOD 1]  0 THEN
   EXIT[FALSE];

 // check each character if allowed
 FOR i := 1 TO STRLEN[FORMAT[pVariant,0,9]] DO
 IF NOT [COPYSTR[FORMAT[pVariant,0,9],i,1] IN ['A'..'Z', 'a'..'z', '0'..'9', '+', '/']] THEN
   BEGIN
   // if not allowed character found, check if already at end of string [and check for '=']
     IF [i = STRLEN[FORMAT[pVariant,0,9]]-1] OR
        [i = STRLEN[FORMAT[pVariant,0,9]]]
     THEN
       BEGIN
         IF COPYSTR[FORMAT[pVariant,0,9],i,1]  '=' THEN
           EXIT[FALSE];
       END
     ELSE
       EXIT[FALSE];
     END;

   EXIT[TRUE];
END;

Ngoài ra, tôi đã tạo một hàm giải mã chuỗi Base64 và lưu trữ nó trong trường BLOB

[TryFunction]
LOCAL PROCEDURE TryToStoreVariantAsBLOB@1000000001[pVariant@1000000000 : Variant;VAR pTempBLOB@1000000001 : TEMPORARY Record 99008535];
VAR
 BlobWriter@1000000004 : OutStream;
 Convert@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
 MemoryStream@1000000002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream";
BEGIN
 MemoryStream := MemoryStream.MemoryStream[Convert.FromBase64String[pVariant]];
 pTempBLOB.Blob.CREATEOUTSTREAM[BlobWriter];
 MemoryStream.CopyTo[BlobWriter];
END;

Tôi đã biên dịch hai chức năng thành Codeunit [bao gồm cả ví dụ] và xuất bản để tải xuống trên mibuso. com

Trong các biểu mẫu HTML, chúng tôi thường yêu cầu xác thực các loại khác nhau. Xác thực email hiện có, xác thực độ dài mật khẩu, xác thực xác nhận mật khẩu, xác thực để chỉ cho phép nhập số nguyên, đây là một số ví dụ về xác thực. Nếu trong một trường đầu vào nhất định, chỉ cho phép chuỗi được mã hóa cơ sở 64, tôi. e không cho phép bất kỳ dạng chuỗi nào khác không cấu thành chuỗi được mã hóa base64. Chúng tôi cũng có thể xác thực các trường đầu vào này để chỉ chấp nhận chuỗi được mã hóa cơ sở 64 bằng cách sử dụng phần mềm trung gian xác thực nhanh

Lệnh cài đặt express-validator

npm install express-validator

Các bước sử dụng express-validator để triển khai logic

  • Cài đặt phần mềm trung gian xác nhận nhanh
  • Tạo trình xác thực. js để mã hóa tất cả logic xác thực
  • Xác thực đầu vào bằng validateInputField. kiểm tra [tên trường đầu vào] và chuỗi xác thực isBase64[] với ‘. ‘
  • Sử dụng tên xác thực [validateInputField] trong các tuyến dưới dạng phần mềm trung gian dưới dạng một mảng xác thực
  • Hủy cấu trúc chức năng 'validationResult' từ trình xác nhận nhanh để sử dụng nó để tìm bất kỳ lỗi nào
  • Nếu xảy ra lỗi, chuyển hướng đến cùng một trang chuyển thông tin lỗi
  • Nếu danh sách lỗi trống, hãy cấp quyền truy cập cho người dùng cho yêu cầu tiếp theo

Ghi chú. Ở đây chúng tôi sử dụng cơ sở dữ liệu cục bộ hoặc tùy chỉnh để triển khai logic, có thể thực hiện theo các bước tương tự để triển khai logic trong cơ sở dữ liệu thông thường như MongoDB hoặc MySql

Ví dụ. Ví dụ này minh họa cách xác thực trường đầu vào để chỉ chấp nhận chuỗi được mã hóa cơ sở 64

Tên tệp - chỉ mục. js

jav




_______3____4____5

const bodyParser = require['body-parser']

const {validationResult} = require[____10]

const express = require[0____31]

const express = require[3____34]

const express = require[6const express = require[7____5

 

const express = require[9

'express'0

 

'express'1

'express'2____43'express'4

 

'express'5

_______46____47____48

'express'9]0

]1

 

]2

]3

'express'9____55____56

'express'9]8

'express'9const bodyParser = require[0

const bodyParser = require[1const bodyParser = require[2

const bodyParser = require[1const bodyParser = require[4const bodyParser = require[5

const bodyParser = require[6const bodyParser = require[7 const bodyParser = require[8

const bodyParser = require[1'body-parser'0

const bodyParser = require[1'body-parser'2

const bodyParser = require[1'body-parser'4

const bodyParser = require[6'body-parser'6

const bodyParser = require[6'body-parser'8

const bodyParser = require[1]1

 

]1____52]3

_______49____55____56____5

]1

 

]9

const {validationResult} = require[0

'express'9const {validationResult} = require[2

]1

Tên tệp - kho lưu trữ. js. Tệp này chứa tất cả logic để tạo cơ sở dữ liệu cục bộ và tương tác với nó

jav




const {validationResult} = require[4

const {validationResult} = require[5____96____5

_______98____99____100

 

[TryFunction]
LOCAL PROCEDURE TryToStoreVariantAsBLOB@1000000001[pVariant@1000000000 : Variant;VAR pTempBLOB@1000000001 : TEMPORARY Record 99008535];
VAR
 BlobWriter@1000000004 : OutStream;
 Convert@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
 MemoryStream@1000000002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream";
BEGIN
 MemoryStream := MemoryStream.MemoryStream[Convert.FromBase64String[pVariant]];
 pTempBLOB.Blob.CREATEOUTSTREAM[BlobWriter];
 MemoryStream.CopyTo[BlobWriter];
END;
01

'express'9

[TryFunction]
LOCAL PROCEDURE TryToStoreVariantAsBLOB@1000000001[pVariant@1000000000 : Variant;VAR pTempBLOB@1000000001 : TEMPORARY Record 99008535];
VAR
 BlobWriter@1000000004 : OutStream;
 Convert@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
 MemoryStream@1000000002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream";
BEGIN
 MemoryStream := MemoryStream.MemoryStream[Convert.FromBase64String[pVariant]];
 pTempBLOB.Blob.CREATEOUTSTREAM[BlobWriter];
 MemoryStream.CopyTo[BlobWriter];
END;
03

 

const bodyParser = require[1

[TryFunction]
LOCAL PROCEDURE TryToStoreVariantAsBLOB@1000000001[pVariant@1000000000 : Variant;VAR pTempBLOB@1000000001 : TEMPORARY Record 99008535];
VAR
 BlobWriter@1000000004 : OutStream;
 Convert@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
 MemoryStream@1000000002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream";
BEGIN
 MemoryStream := MemoryStream.MemoryStream[Convert.FromBase64String[pVariant]];
 pTempBLOB.Blob.CREATEOUTSTREAM[BlobWriter];
 MemoryStream.CopyTo[BlobWriter];
END;
05

const bodyParser = require[1const bodyParser = require[4

[TryFunction]
LOCAL PROCEDURE TryToStoreVariantAsBLOB@1000000001[pVariant@1000000000 : Variant;VAR pTempBLOB@1000000001 : TEMPORARY Record 99008535];
VAR
 BlobWriter@1000000004 : OutStream;
 Convert@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
 MemoryStream@1000000002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream";
BEGIN
 MemoryStream := MemoryStream.MemoryStream[Convert.FromBase64String[pVariant]];
 pTempBLOB.Blob.CREATEOUTSTREAM[BlobWriter];
 MemoryStream.CopyTo[BlobWriter];
END;
08

const bodyParser = require[6]0 ]1 ]2

]3____5

const bodyParser = require[1'body-parser'0

 

const bodyParser = require[1____58]9

 

const bodyParser = require[1const express = require[01 const express = require[02

const bodyParser = require[6______304____58const express = require[06

const bodyParser = require[1'body-parser'0const express = require[09const express = require[10

 

const bodyParser = require[6____312

const bodyParser = require[6____314

const bodyParser = require[6______316____58const express = require[18const express = require[19]

const bodyParser = require[1'body-parser'0

'express'9'body-parser'0

 

_______49____326

'express'9const express = require[28

const bodyParser = require[1const bodyParser = require[7 const express = require[31

const bodyParser = require[6______333____58const express = require[35

const express = require[36____337____338

const bodyParser = require[6]1

const bodyParser = require[1]

'express'9'body-parser'0

'express'9

'express'9const express = require[47

'express'9const express = require[49

const bodyParser = require[1const express = require[51

const bodyParser = require[1const express = require[53____58const express = require[55

const bodyParser = require[1const express = require[57

const bodyParser = require[1const express = require[59

const bodyParser = require[1const express = require[61

const bodyParser = require[1const express = require[63

const bodyParser = require[6const express = require[65

const bodyParser = require[6const express = require[67

const bodyParser = require[1'body-parser'0

 

const bodyParser = require[1const express = require[71

const bodyParser = require[1const express = require[73

const bodyParser = require[1const express = require[75

const bodyParser = require[1const express = require[77

const bodyParser = require[6____58____318

_______66____382____383const express = require[84

const bodyParser = require[1]

const bodyParser = require[1const bodyParser = require[7 const express = require[89

'express'9'body-parser'0

'body-parser'0

 

const express = require[93

const express = require[94

const express = require[95

const express = require[96]1 const express = require[98const express = require[99]

Tên tệp – biểu mẫu. js. Tệp này chứa logic để hiển thị biểu mẫu HTML

jav




'express'01

'express'9const express = require[01 const express = require[02

const bodyParser = require[1const bodyParser = require[7 'express'07

'express'9'body-parser'0const express = require[09 'express'11

const bodyParser = require[1const bodyParser = require[7 'express'14

'express'9'body-parser'0

'body-parser'0

 

'express'18

'express'9const bodyParser = require[7 'express'21

_______61____423

_______61____425

const bodyParser = require[6____427

_______336____429____430

_______431____432____433

const express = require[36'express'35

_______436____437

'express'38'express'39

_______436____70

_______436____443

'express'38'express'45

_______436____70

const express = require[36'express'49

const bodyParser = require[6____451

const bodyParser = require[6____453

const express = require[36'express'55____456'express'33

'express'36______455'express'60'express'33

'express'38'express'55____464'express'33

'express'66'express'67]5 'express'69'express'70'express'71

'express'72'express'73

'express'74'express'73

'express'76'express'77'express'78 'express'79'express'80'express'33

_______482____483

'express'76'express'85

'express'74'express'87

'express'74'express'89'express'90 'express'91'express'92

_______493____494____480

'express'93____497'express'98

'express'93____500]01'express'80'express'33

'express'72'express'87

'express'72'express'73

'express'74'express'73

'express'76'express'77'express'78 'express'79]14'express'33

'express'82]17

'express'76'express'85

'express'74'express'87

'express'74'express'89'express'90 'express'91'express'92

'express'93____494]14

'express'93____497]32

'express'93____500]01]14'express'33

'express'74]39____540'express'33

_______476____543____514]45

_______474____547

 

 

'express'72'express'87

'express'72'express'73

_______474____553____554'express'33

_______476____557

]58]59

'express'72'express'87

'express'66]63

'express'38'express'87

'express'36____487

const express = require[36'express'87

const bodyParser = require[6]71

const bodyParser = require[1____573

'express'9'express'21

'body-parser'0

Tên tệp - trình xác thực. js. Tệp này chứa tất cả logic xác thực [Logic để xác thực trường đầu vào chỉ chấp nhận chuỗi 64 được mã hóa cơ sở]

jav




]77

[TryFunction]
LOCAL PROCEDURE TryToStoreVariantAsBLOB@1000000001[pVariant@1000000000 : Variant;VAR pTempBLOB@1000000001 : TEMPORARY Record 99008535];
VAR
 BlobWriter@1000000004 : OutStream;
 Convert@1000000003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Convert";
 MemoryStream@1000000002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.MemoryStream";
BEGIN
 MemoryStream := MemoryStream.MemoryStream[Convert.FromBase64String[pVariant]];
 pTempBLOB.Blob.CREATEOUTSTREAM[BlobWriter];
 MemoryStream.CopyTo[BlobWriter];
END;
0____5

const express = require[0____31]

]83

'express'9

_______49____586____514]

const bodyParser = require[1]90

_______61____592

const bodyParser = require[1]94

_______61____596

const bodyParser = require[1]98

const bodyParser = require[1const bodyParser = require[00const bodyParser = require[01]

'body-parser'0

Tên tệp - gói. json

bưu kiện. tập tin json

cơ sở dữ liệu

cơ sở dữ liệu

Trang web trực tuyến chuyển đổi một chuỗi bình thường thành chuỗi base64 [để hiển thị chuỗi nào chúng tôi cung cấp đầu vào base64]

Làm cách nào để kiểm tra xem một chuỗi có được mã hóa Base64 trong Java không?

Kể từ Java 8, bạn có thể chỉ cần sử dụng java. sử dụng. Base64 để thử và giải mã chuỗi . Chuỗi someString = ". "; cơ sở64.

== trong Base64 là gì?

Dấu bằng "=" biểu thị phần đệm , thường thấy ở cuối chuỗi được mã hóa Base64. Kích thước tính bằng byte chia hết cho ba [bit chia hết cho 24]. Tất cả các bit được mã hóa bình thường.

Tại sao Base64 có == ở cuối?

Q Tại sao dấu = được thêm vào cuối? . Như một câu trả lời ngắn. Ký tự cuối cùng [ = sign] chỉ được thêm vào dưới dạng phần bổ sung [phần đệm] trong quá trình mã hóa thư cuối cùng với một số ký tự đặc biệt .

Base64 có phải là một chuỗi không?

Base64 là sơ đồ mã hóa nhị phân thành văn bản biểu thị dữ liệu nhị phân ở định dạng chuỗi ASCII . base64 được thiết kế để mang dữ liệu được lưu trữ ở định dạng nhị phân trên các kênh. Nó lấy bất kỳ dạng dữ liệu nào và biến nó thành một chuỗi dài văn bản thuần túy.

Chủ Đề